blob: 480f214b4a54c91550d921e38b434bfe796ed0e3 [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 Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static 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 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static 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 +0200460static 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 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100671static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100691#ifdef FEAT_CRYPT
692static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
693#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000694static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200695static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200699static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000702static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000703static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
707static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
709#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000710static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200711static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712#ifdef HAVE_STRFTIME
713static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
715static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200721static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200722static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000728static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200729static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000732static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000733static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000734static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000735static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000737static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200738#ifdef FEAT_FLOAT
739static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745#ifdef FEAT_FLOAT
746static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200749static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200750static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static 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 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static 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 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000811static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static 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 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200841
842#ifdef EBCDIC
843static int compare_func_name __ARGS((const void *s1, const void *s2));
844static void sortFunctions __ARGS(());
845#endif
846
847
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848/* Character used as separated in autoload function/variable names. */
849#define AUTOLOAD_CHAR '#'
850
Bram Moolenaar33570922005-01-25 22:26:29 +0000851/*
852 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000853 */
854 void
855eval_init()
856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 int i;
858 struct vimvar *p;
859
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200860 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
861 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200862 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865
866 for (i = 0; i < VV_LEN; ++i)
867 {
868 p = &vimvars[i];
869 STRCPY(p->vv_di.di_key, p->vv_name);
870 if (p->vv_flags & VV_RO)
871 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
872 else if (p->vv_flags & VV_RO_SBX)
873 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
874 else
875 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000876
877 /* add to v: scope dict, unless the value is not always available */
878 if (p->vv_type != VAR_UNKNOWN)
879 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000881 /* add to compat scope dict */
882 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000884 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200885 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886
887#ifdef EBCDIC
888 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100889 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890 */
891 sortFunctions();
892#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000893}
894
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895#if defined(EXITFREE) || defined(PROTO)
896 void
897eval_clear()
898{
899 int i;
900 struct vimvar *p;
901
902 for (i = 0; i < VV_LEN; ++i)
903 {
904 p = &vimvars[i];
905 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000907 vim_free(p->vv_str);
908 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000909 }
910 else if (p->vv_di.di_tv.v_type == VAR_LIST)
911 {
912 list_unref(p->vv_list);
913 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 }
916 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000917 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918 hash_clear(&compat_hashtab);
919
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100921# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200922 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100923# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924
925 /* global variables */
926 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000927
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000928 /* autoloaded script names */
929 ga_clear_strings(&ga_loaded);
930
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 /* script-local variables */
932 for (i = 1; i <= ga_scripts.ga_len; ++i)
933 {
934 vars_clear(&SCRIPT_VARS(i));
935 vim_free(SCRIPT_SV(i));
936 }
937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1161 FALSE, FALSE, FALSE, FNE_CHECK_START);
1162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001864 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001867 /*
1868 * ":let" without "=": list variables
1869 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 if (*arg == '[')
1871 EMSG(_(e_invarg));
1872 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_glob_vars(&first);
1879 list_buf_vars(&first);
1880 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001883#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001884 list_script_vars(&first);
1885 list_func_vars(&first);
1886 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 eap->nextcmd = check_nextcmd(arg);
1889 }
1890 else
1891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 op[0] = '=';
1893 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 {
1896 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1897 op[0] = expr[-1]; /* +=, -= or .= */
1898 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
2250 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
2530 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002531 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 */
2534 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002535get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 typval_T *rettv;
2538 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 int unlet;
2540 int skip;
2541 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 char_u *expr_start, *expr_end;
2546 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 dictitem_T *v;
2548 typval_T var1;
2549 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558
2559 if (skip)
2560 {
2561 /* When skipping just find the end of the name. */
2562 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002563 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 }
2565
2566 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002567 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (expr_start != NULL)
2569 {
2570 /* Don't expand the name when we already know there is an error. */
2571 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2572 && *p != '[' && *p != '.')
2573 {
2574 EMSG(_(e_trailing));
2575 return NULL;
2576 }
2577
2578 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2579 if (lp->ll_exp_name == NULL)
2580 {
2581 /* Report an invalid expression in braces, unless the
2582 * expression evaluation has been cancelled due to an
2583 * aborting error, an interrupt, or an exception. */
2584 if (!aborting() && !quiet)
2585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 EMSG2(_(e_invarg2), name);
2588 return NULL;
2589 }
2590 }
2591 lp->ll_name = lp->ll_exp_name;
2592 }
2593 else
2594 lp->ll_name = name;
2595
2596 /* Without [idx] or .key we are done. */
2597 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2598 return p;
2599
2600 cc = *p;
2601 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002602 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (v == NULL && !quiet)
2604 EMSG2(_(e_undefvar), lp->ll_name);
2605 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 if (v == NULL)
2607 return NULL;
2608
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /*
2610 * Loop until no more [idx] or .key is following.
2611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002612 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2616 && !(lp->ll_tv->v_type == VAR_DICT
2617 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_("E689: Can only index a List or Dictionary"));
2621 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_("E708: [:] must come last"));
2627 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 len = -1;
2631 if (*p == '.')
2632 {
2633 key = p + 1;
2634 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2635 ;
2636 if (len == 0)
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!quiet)
2639 EMSG(_(e_emptykey));
2640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642 p = key + len;
2643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 else
2645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 if (*p == ':')
2649 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 else
2651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 empty1 = FALSE;
2653 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 if (get_tv_string_chk(&var1) == NULL)
2656 {
2657 /* not a number or string */
2658 clear_tv(&var1);
2659 return NULL;
2660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
2662
2663 /* Optionally get the second index [ :expr]. */
2664 if (*p == ':')
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 if (!empty1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (rettv != NULL && (rettv->v_type != VAR_LIST
2675 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = skipwhite(p + 1);
2684 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
2687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 if (get_tv_string_chk(&var2) == NULL)
2696 {
2697 /* not a number or string */
2698 if (!empty1)
2699 clear_tv(&var1);
2700 clear_tv(&var2);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719
2720 /* Skip to past ']'. */
2721 ++p;
2722 }
2723
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
2726 if (len == -1)
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (*key == NUL)
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
2733 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 lp->ll_list = NULL;
2739 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002740 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 /* When assigning to a scope dictionary check that a function and
2743 * variable name is valid (only variable name unless it is l: or
2744 * g: dictionary). Disallow overwriting a builtin function. */
2745 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 int prevval;
2748 int wrong;
2749
2750 if (len != -1)
2751 {
2752 prevval = key[len];
2753 key[len] = NUL;
2754 }
2755 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2756 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002758 || !valid_varname(key);
2759 if (len != -1)
2760 key[len] = prevval;
2761 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 return NULL;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* Can't add "v:" variable. */
2768 if (lp->ll_dict == &vimvardict)
2769 {
2770 EMSG2(_(e_illvar), name);
2771 return NULL;
2772 }
2773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (len == -1)
2780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 p = NULL;
2791 break;
2792 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 /* existing variable, need to check if it can be changed */
2794 else if (var_check_ro(lp->ll_di->di_flags, name))
2795 return NULL;
2796
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 }
2801 else
2802 {
2803 /*
2804 * Get the number and item for the only or first index of the List.
2805 */
2806 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 else
2809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002810 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var1);
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_list = lp->ll_tv->vval.v_list;
2815 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2816 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002818 if (lp->ll_n1 < 0)
2819 {
2820 lp->ll_n1 = 0;
2821 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2822 }
2823 }
2824 if (lp->ll_li == NULL)
2825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002828 if (!quiet)
2829 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832
2833 /*
2834 * May need to find the item or absolute index for the second
2835 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 * When no index given: "lp->ll_empty2" is TRUE.
2837 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 {
2848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2856 if (lp->ll_n1 < 0)
2857 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2858 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 {
2860 if (!quiet)
2861 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002868 }
2869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return p;
2871}
2872
2873/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 */
2876 static void
2877clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879{
2880 vim_free(lp->ll_exp_name);
2881 vim_free(lp->ll_newkey);
2882}
2883
2884/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002887 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 */
2889 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 listitem_T *ri;
2899 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900
2901 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 cc = *endp;
2906 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 if (op != NULL && *op != '=')
2908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002912 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 {
2915 if (tv_op(&tv, rettv, op) == OK)
2916 set_var(lp->ll_name, &tv, FALSE);
2917 clear_tv(&tv);
2918 }
2919 }
2920 else
2921 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 else if (tv_check_lock(lp->ll_newkey == NULL
2926 ? lp->ll_tv->v_lock
2927 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2928 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 else if (lp->ll_range)
2930 {
2931 /*
2932 * Assign the List values to the list items.
2933 */
2934 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2938 else
2939 {
2940 clear_tv(&lp->ll_li->li_tv);
2941 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 ri = ri->li_next;
2944 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2945 break;
2946 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002949 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 ri = NULL;
2952 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002953 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 lp->ll_li = lp->ll_li->li_next;
2956 ++lp->ll_n1;
2957 }
2958 if (ri != NULL)
2959 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 else if (lp->ll_empty2
2961 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 : lp->ll_n1 != lp->ll_n2)
2963 EMSG(_("E711: List value has not enough items"));
2964 }
2965 else
2966 {
2967 /*
2968 * Assign to a List or Dictionary item.
2969 */
2970 if (lp->ll_newkey != NULL)
2971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972 if (op != NULL && *op != '=')
2973 {
2974 EMSG2(_(e_letwrong), op);
2975 return;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002979 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 if (di == NULL)
2981 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002982 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2983 {
2984 vim_free(di);
2985 return;
2986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 else if (op != NULL && *op != '=')
2990 {
2991 tv_op(lp->ll_tv, rettv, op);
2992 return;
2993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 /*
2998 * Assign the value to the variable or list item.
2999 */
3000 if (copy)
3001 copy_tv(rettv, lp->ll_tv);
3002 else
3003 {
3004 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003005 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 }
3008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003009}
3010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3013 * Returns OK or FAIL.
3014 */
3015 static int
3016tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 typval_T *tv1;
3018 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 char_u *op;
3020{
3021 long n;
3022 char_u numbuf[NUMBUFLEN];
3023 char_u *s;
3024
3025 /* Can't do anything with a Funcref or a Dict on the right. */
3026 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3027 {
3028 switch (tv1->v_type)
3029 {
3030 case VAR_DICT:
3031 case VAR_FUNC:
3032 break;
3033
3034 case VAR_LIST:
3035 if (*op != '+' || tv2->v_type != VAR_LIST)
3036 break;
3037 /* List += List */
3038 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3039 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3040 return OK;
3041
3042 case VAR_NUMBER:
3043 case VAR_STRING:
3044 if (tv2->v_type == VAR_LIST)
3045 break;
3046 if (*op == '+' || *op == '-')
3047 {
3048 /* nr += nr or nr -= nr*/
3049 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#ifdef FEAT_FLOAT
3051 if (tv2->v_type == VAR_FLOAT)
3052 {
3053 float_T f = n;
3054
3055 if (*op == '+')
3056 f += tv2->vval.v_float;
3057 else
3058 f -= tv2->vval.v_float;
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_FLOAT;
3061 tv1->vval.v_float = f;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064#endif
3065 {
3066 if (*op == '+')
3067 n += get_tv_number(tv2);
3068 else
3069 n -= get_tv_number(tv2);
3070 clear_tv(tv1);
3071 tv1->v_type = VAR_NUMBER;
3072 tv1->vval.v_number = n;
3073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 }
3075 else
3076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (tv2->v_type == VAR_FLOAT)
3078 break;
3079
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 /* str .= str */
3081 s = get_tv_string(tv1);
3082 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3083 clear_tv(tv1);
3084 tv1->v_type = VAR_STRING;
3085 tv1->vval.v_string = s;
3086 }
3087 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088
3089#ifdef FEAT_FLOAT
3090 case VAR_FLOAT:
3091 {
3092 float_T f;
3093
3094 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3095 && tv2->v_type != VAR_NUMBER
3096 && tv2->v_type != VAR_STRING))
3097 break;
3098 if (tv2->v_type == VAR_FLOAT)
3099 f = tv2->vval.v_float;
3100 else
3101 f = get_tv_number(tv2);
3102 if (*op == '+')
3103 tv1->vval.v_float += f;
3104 else
3105 tv1->vval.v_float -= f;
3106 }
3107 return OK;
3108#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 }
3110 }
3111
3112 EMSG2(_(e_letwrong), op);
3113 return FAIL;
3114}
3115
3116/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 * Add a watcher to a list.
3118 */
3119 static void
3120list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 list_T *l;
3122 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123{
3124 lw->lw_next = l->lv_watch;
3125 l->lv_watch = lw;
3126}
3127
3128/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003129 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 * No warning when it isn't found...
3131 */
3132 static void
3133list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 list_T *l;
3135 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136{
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138
3139 lwp = &l->lv_watch;
3140 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3141 {
3142 if (lw == lwrem)
3143 {
3144 *lwp = lw->lw_next;
3145 break;
3146 }
3147 lwp = &lw->lw_next;
3148 }
3149}
3150
3151/*
3152 * Just before removing an item from a list: advance watchers to the next
3153 * item.
3154 */
3155 static void
3156list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 list_T *l;
3158 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159{
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003161
3162 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3163 if (lw->lw_item == item)
3164 lw->lw_item = item->li_next;
3165}
3166
3167/*
3168 * Evaluate the expression used in a ":for var in expr" command.
3169 * "arg" points to "var".
3170 * Set "*errp" to TRUE for an error, FALSE otherwise;
3171 * Return a pointer that holds the info. Null when there is an error.
3172 */
3173 void *
3174eval_for_line(arg, errp, nextcmdp, skip)
3175 char_u *arg;
3176 int *errp;
3177 char_u **nextcmdp;
3178 int skip;
3179{
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 typval_T tv;
3183 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184
3185 *errp = TRUE; /* default: there is an error */
3186
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 if (fi == NULL)
3189 return NULL;
3190
3191 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3192 if (expr == NULL)
3193 return fi;
3194
3195 expr = skipwhite(expr);
3196 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3197 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003198 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 return fi;
3200 }
3201
3202 if (skip)
3203 ++emsg_skip;
3204 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3205 {
3206 *errp = FALSE;
3207 if (!skip)
3208 {
3209 l = tv.vval.v_list;
3210 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003211 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213 clear_tv(&tv);
3214 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 else
3216 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003217 /* No need to increment the refcount, it's already set for the
3218 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 fi->fi_list = l;
3220 list_add_watch(l, &fi->fi_lw);
3221 fi->fi_lw.lw_item = l->lv_first;
3222 }
3223 }
3224 }
3225 if (skip)
3226 --emsg_skip;
3227
3228 return fi;
3229}
3230
3231/*
3232 * Use the first item in a ":for" list. Advance to the next.
3233 * Assign the values to the variable (list). "arg" points to the first one.
3234 * Return TRUE when a valid item was found, FALSE when at end of list or
3235 * something wrong.
3236 */
3237 int
3238next_for_item(fi_void, arg)
3239 void *fi_void;
3240 char_u *arg;
3241{
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 item = fi->fi_lw.lw_item;
3247 if (item == NULL)
3248 result = FALSE;
3249 else
3250 {
3251 fi->fi_lw.lw_item = item->li_next;
3252 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3253 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3254 }
3255 return result;
3256}
3257
3258/*
3259 * Free the structure used to store info used by ":for".
3260 */
3261 void
3262free_for_info(fi_void)
3263 void *fi_void;
3264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003267 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 list_unref(fi->fi_list);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 vim_free(fi);
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3276
3277 void
3278set_context_for_expression(xp, arg, cmdidx)
3279 expand_T *xp;
3280 char_u *arg;
3281 cmdidx_T cmdidx;
3282{
3283 int got_eq = FALSE;
3284 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (cmdidx == CMD_let)
3288 {
3289 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003290 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 {
3292 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003293 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 {
3295 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (vim_iswhite(*p))
3298 break;
3299 }
3300 return;
3301 }
3302 }
3303 else
3304 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3305 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 while ((xp->xp_pattern = vim_strpbrk(arg,
3307 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3308 {
3309 c = *xp->xp_pattern;
3310 if (c == '&')
3311 {
3312 c = xp->xp_pattern[1];
3313 if (c == '&')
3314 {
3315 ++xp->xp_pattern;
3316 xp->xp_context = cmdidx != CMD_let || got_eq
3317 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3318 }
3319 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003322 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3323 xp->xp_pattern += 2;
3324
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 else if (c == '$')
3328 {
3329 /* environment variable */
3330 xp->xp_context = EXPAND_ENV_VARS;
3331 }
3332 else if (c == '=')
3333 {
3334 got_eq = TRUE;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else if (c == '<'
3338 && xp->xp_context == EXPAND_FUNCTIONS
3339 && vim_strchr(xp->xp_pattern, '(') == NULL)
3340 {
3341 /* Function name can start with "<SNR>" */
3342 break;
3343 }
3344 else if (cmdidx != CMD_let || got_eq)
3345 {
3346 if (c == '"') /* string */
3347 {
3348 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3349 if (c == '\\' && xp->xp_pattern[1] != NUL)
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_NOTHING;
3352 }
3353 else if (c == '\'') /* literal string */
3354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3357 /* skip */ ;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '|')
3361 {
3362 if (xp->xp_pattern[1] == '|')
3363 {
3364 ++xp->xp_pattern;
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
3367 else
3368 xp->xp_context = EXPAND_COMMANDS;
3369 }
3370 else
3371 xp->xp_context = EXPAND_EXPRESSION;
3372 }
3373 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 /* Doesn't look like something valid, expand as an expression
3375 * anyway. */
3376 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 arg = xp->xp_pattern;
3378 if (*arg != NUL)
3379 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3380 /* skip */ ;
3381 }
3382 xp->xp_pattern = arg;
3383}
3384
3385#endif /* FEAT_CMDL_COMPL */
3386
3387/*
3388 * ":1,25call func(arg1, arg2)" function call.
3389 */
3390 void
3391ex_call(eap)
3392 exarg_T *eap;
3393{
3394 char_u *arg = eap->arg;
3395 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003397 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 linenr_T lnum;
3401 int doesrange;
3402 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003405 if (eap->skip)
3406 {
3407 /* trans_function_name() doesn't work well when skipping, use eval0()
3408 * instead to skip to any following command, e.g. for:
3409 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003410 ++emsg_skip;
3411 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3412 clear_tv(&rettv);
3413 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003414 return;
3415 }
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003418 if (fudi.fd_newkey != NULL)
3419 {
3420 /* Still need to give an error message for missing key. */
3421 EMSG2(_(e_dictkey), fudi.fd_newkey);
3422 vim_free(fudi.fd_newkey);
3423 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 if (tofree == NULL)
3425 return;
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 /* Increase refcount on dictionary, it could get deleted when evaluating
3428 * the arguments. */
3429 if (fudi.fd_dict != NULL)
3430 ++fudi.fd_dict->dv_refcount;
3431
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003432 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003433 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar532c7802005-01-27 14:44:31 +00003436 /* Skip white space to allow ":call func ()". Not good, but required for
3437 * backward compatibility. */
3438 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 if (*startarg != '(')
3442 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003443 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 goto end;
3445 }
3446
3447 /*
3448 * When skipping, evaluate the function once, to find the end of the
3449 * arguments.
3450 * When the function takes a range, this is discovered after the first
3451 * call, and the loop is broken.
3452 */
3453 if (eap->skip)
3454 {
3455 ++emsg_skip;
3456 lnum = eap->line2; /* do it once, also with an invalid range */
3457 }
3458 else
3459 lnum = eap->line1;
3460 for ( ; lnum <= eap->line2; ++lnum)
3461 {
3462 if (!eap->skip && eap->addr_count > 0)
3463 {
3464 curwin->w_cursor.lnum = lnum;
3465 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003466#ifdef FEAT_VIRTUALEDIT
3467 curwin->w_cursor.coladd = 0;
3468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 eap->line1, eap->line2, &doesrange,
3473 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 failed = TRUE;
3476 break;
3477 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
3479 /* Handle a function returning a Funcref, Dictionary or List. */
3480 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3481 {
3482 failed = TRUE;
3483 break;
3484 }
3485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (doesrange || eap->skip)
3488 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (aborting())
3495 break;
3496 }
3497 if (eap->skip)
3498 --emsg_skip;
3499
3500 if (!failed)
3501 {
3502 /* Check for trailing illegal characters and a following command. */
3503 if (!ends_excmd(*arg))
3504 {
3505 emsg_severe = TRUE;
3506 EMSG(_(e_trailing));
3507 }
3508 else
3509 eap->nextcmd = check_nextcmd(arg);
3510 }
3511
3512end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003514 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515}
3516
3517/*
3518 * ":unlet[!] var1 ... " command.
3519 */
3520 void
3521ex_unlet(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 ex_unletlock(eap, eap->arg, 0);
3525}
3526
3527/*
3528 * ":lockvar" and ":unlockvar" commands
3529 */
3530 void
3531ex_lockvar(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 int deep = 2;
3536
3537 if (eap->forceit)
3538 deep = -1;
3539 else if (vim_isdigit(*arg))
3540 {
3541 deep = getdigits(&arg);
3542 arg = skipwhite(arg);
3543 }
3544
3545 ex_unletlock(eap, arg, deep);
3546}
3547
3548/*
3549 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3550 */
3551 static void
3552ex_unletlock(eap, argstart, deep)
3553 exarg_T *eap;
3554 char_u *argstart;
3555 int deep;
3556{
3557 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 do
3563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003565 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3566 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 if (lv.ll_name == NULL)
3568 error = TRUE; /* error but continue parsing */
3569 if (name_end == NULL || (!vim_iswhite(*name_end)
3570 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 if (name_end != NULL)
3573 {
3574 emsg_severe = TRUE;
3575 EMSG(_(e_trailing));
3576 }
3577 if (!(eap->skip || error))
3578 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 break;
3580 }
3581
3582 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 {
3584 if (eap->cmdidx == CMD_unlet)
3585 {
3586 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3587 error = TRUE;
3588 }
3589 else
3590 {
3591 if (do_lock_var(&lv, name_end, deep,
3592 eap->cmdidx == CMD_lockvar) == FAIL)
3593 error = TRUE;
3594 }
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 if (!eap->skip)
3598 clear_lval(&lv);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 arg = skipwhite(name_end);
3601 } while (!ends_excmd(*arg));
3602
3603 eap->nextcmd = check_nextcmd(arg);
3604}
3605
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 int forceit;
3611{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 int ret = OK;
3613 int cc;
3614
3615 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 cc = *name_end;
3618 *name_end = NUL;
3619
3620 /* Normal name or expanded name. */
3621 if (check_changedtick(lp->ll_name))
3622 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3628 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 else if (lp->ll_range)
3630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632
3633 /* Delete a range of List items. */
3634 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3635 {
3636 li = lp->ll_li->li_next;
3637 listitem_remove(lp->ll_list, lp->ll_li);
3638 lp->ll_li = li;
3639 ++lp->ll_n1;
3640 }
3641 }
3642 else
3643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 /* unlet a List item. */
3646 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003649 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 }
3651
3652 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653}
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655/*
3656 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
3659 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hashtab_T *ht;
3665 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003667 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar33570922005-01-25 22:26:29 +00003669 ht = find_var_ht(name, &varname);
3670 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003672 hi = hash_find(ht, varname);
3673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003675 di = HI2DI(hi);
3676 if (var_check_fixed(di->di_flags, name)
3677 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
3679 delete_var(ht, hi);
3680 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 if (forceit)
3684 return OK;
3685 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687}
3688
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689/*
3690 * Lock or unlock variable indicated by "lp".
3691 * "deep" is the levels to go (-1 for unlimited);
3692 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3693 */
3694 static int
3695do_lock_var(lp, name_end, deep, lock)
3696 lval_T *lp;
3697 char_u *name_end;
3698 int deep;
3699 int lock;
3700{
3701 int ret = OK;
3702 int cc;
3703 dictitem_T *di;
3704
3705 if (deep == 0) /* nothing to do */
3706 return OK;
3707
3708 if (lp->ll_tv == NULL)
3709 {
3710 cc = *name_end;
3711 *name_end = NUL;
3712
3713 /* Normal name or expanded name. */
3714 if (check_changedtick(lp->ll_name))
3715 ret = FAIL;
3716 else
3717 {
3718 di = find_var(lp->ll_name, NULL);
3719 if (di == NULL)
3720 ret = FAIL;
3721 else
3722 {
3723 if (lock)
3724 di->di_flags |= DI_FLAGS_LOCK;
3725 else
3726 di->di_flags &= ~DI_FLAGS_LOCK;
3727 item_lock(&di->di_tv, deep, lock);
3728 }
3729 }
3730 *name_end = cc;
3731 }
3732 else if (lp->ll_range)
3733 {
3734 listitem_T *li = lp->ll_li;
3735
3736 /* (un)lock a range of List items. */
3737 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3738 {
3739 item_lock(&li->li_tv, deep, lock);
3740 li = li->li_next;
3741 ++lp->ll_n1;
3742 }
3743 }
3744 else if (lp->ll_list != NULL)
3745 /* (un)lock a List item. */
3746 item_lock(&lp->ll_li->li_tv, deep, lock);
3747 else
3748 /* un(lock) a Dictionary item. */
3749 item_lock(&lp->ll_di->di_tv, deep, lock);
3750
3751 return ret;
3752}
3753
3754/*
3755 * Lock or unlock an item. "deep" is nr of levels to go.
3756 */
3757 static void
3758item_lock(tv, deep, lock)
3759 typval_T *tv;
3760 int deep;
3761 int lock;
3762{
3763 static int recurse = 0;
3764 list_T *l;
3765 listitem_T *li;
3766 dict_T *d;
3767 hashitem_T *hi;
3768 int todo;
3769
3770 if (recurse >= DICT_MAXNEST)
3771 {
3772 EMSG(_("E743: variable nested too deep for (un)lock"));
3773 return;
3774 }
3775 if (deep == 0)
3776 return;
3777 ++recurse;
3778
3779 /* lock/unlock the item itself */
3780 if (lock)
3781 tv->v_lock |= VAR_LOCKED;
3782 else
3783 tv->v_lock &= ~VAR_LOCKED;
3784
3785 switch (tv->v_type)
3786 {
3787 case VAR_LIST:
3788 if ((l = tv->vval.v_list) != NULL)
3789 {
3790 if (lock)
3791 l->lv_lock |= VAR_LOCKED;
3792 else
3793 l->lv_lock &= ~VAR_LOCKED;
3794 if (deep < 0 || deep > 1)
3795 /* recursive: lock/unlock the items the List contains */
3796 for (li = l->lv_first; li != NULL; li = li->li_next)
3797 item_lock(&li->li_tv, deep - 1, lock);
3798 }
3799 break;
3800 case VAR_DICT:
3801 if ((d = tv->vval.v_dict) != NULL)
3802 {
3803 if (lock)
3804 d->dv_lock |= VAR_LOCKED;
3805 else
3806 d->dv_lock &= ~VAR_LOCKED;
3807 if (deep < 0 || deep > 1)
3808 {
3809 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3812 {
3813 if (!HASHITEM_EMPTY(hi))
3814 {
3815 --todo;
3816 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3817 }
3818 }
3819 }
3820 }
3821 }
3822 --recurse;
3823}
3824
Bram Moolenaara40058a2005-07-11 22:42:07 +00003825/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003826 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3827 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003828 */
3829 static int
3830tv_islocked(tv)
3831 typval_T *tv;
3832{
3833 return (tv->v_lock & VAR_LOCKED)
3834 || (tv->v_type == VAR_LIST
3835 && tv->vval.v_list != NULL
3836 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3837 || (tv->v_type == VAR_DICT
3838 && tv->vval.v_dict != NULL
3839 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3843/*
3844 * Delete all "menutrans_" variables.
3845 */
3846 void
3847del_menutrans_vars()
3848{
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003853 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 {
3856 if (!HASHITEM_EMPTY(hi))
3857 {
3858 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3860 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 }
3862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864}
3865#endif
3866
3867#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3868
3869/*
3870 * Local string buffer for the next two functions to store a variable name
3871 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3872 * get_user_var_name().
3873 */
3874
3875static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3876
3877static char_u *varnamebuf = NULL;
3878static int varnamebuflen = 0;
3879
3880/*
3881 * Function to concatenate a prefix and a variable name.
3882 */
3883 static char_u *
3884cat_prefix_varname(prefix, name)
3885 int prefix;
3886 char_u *name;
3887{
3888 int len;
3889
3890 len = (int)STRLEN(name) + 3;
3891 if (len > varnamebuflen)
3892 {
3893 vim_free(varnamebuf);
3894 len += 10; /* some additional space */
3895 varnamebuf = alloc(len);
3896 if (varnamebuf == NULL)
3897 {
3898 varnamebuflen = 0;
3899 return NULL;
3900 }
3901 varnamebuflen = len;
3902 }
3903 *varnamebuf = prefix;
3904 varnamebuf[1] = ':';
3905 STRCPY(varnamebuf + 2, name);
3906 return varnamebuf;
3907}
3908
3909/*
3910 * Function given to ExpandGeneric() to obtain the list of user defined
3911 * (global/buffer/window/built-in) variable names.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u *
3914get_user_var_name(xp, idx)
3915 expand_T *xp;
3916 int idx;
3917{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003918 static long_u gdone;
3919 static long_u bdone;
3920 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 static long_u tdone;
3923#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003924 static int vidx;
3925 static hashitem_T *hi;
3926 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003930 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 tdone = 0;
3933#endif
3934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* Global variables */
3937 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 else
3942 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 while (HASHITEM_EMPTY(hi))
3944 ++hi;
3945 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3946 return cat_prefix_varname('g', hi->hi_key);
3947 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949
3950 /* b: variables */
3951 ht = &curbuf->b_vars.dv_hashtab;
3952 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003956 else
3957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 while (HASHITEM_EMPTY(hi))
3959 ++hi;
3960 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return (char_u *)"b:changedtick";
3966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967
3968 /* w: variables */
3969 ht = &curwin->w_vars.dv_hashtab;
3970 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003972 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003974 else
3975 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981#ifdef FEAT_WINDOWS
3982 /* t: variables */
3983 ht = &curtab->tp_vars.dv_hashtab;
3984 if (tdone < ht->ht_used)
3985 {
3986 if (tdone++ == 0)
3987 hi = ht->ht_array;
3988 else
3989 ++hi;
3990 while (HASHITEM_EMPTY(hi))
3991 ++hi;
3992 return cat_prefix_varname('t', hi->hi_key);
3993 }
3994#endif
3995
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 /* v: variables */
3997 if (vidx < VV_LEN)
3998 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 vim_free(varnamebuf);
4001 varnamebuf = NULL;
4002 varnamebuflen = 0;
4003 return NULL;
4004}
4005
4006#endif /* FEAT_CMDL_COMPL */
4007
4008/*
4009 * types for expressions.
4010 */
4011typedef enum
4012{
4013 TYPE_UNKNOWN = 0
4014 , TYPE_EQUAL /* == */
4015 , TYPE_NEQUAL /* != */
4016 , TYPE_GREATER /* > */
4017 , TYPE_GEQUAL /* >= */
4018 , TYPE_SMALLER /* < */
4019 , TYPE_SEQUAL /* <= */
4020 , TYPE_MATCH /* =~ */
4021 , TYPE_NOMATCH /* !~ */
4022} exptype_T;
4023
4024/*
4025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4028 */
4029
4030/*
4031 * Handle zero level expression.
4032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004034 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * Return OK or FAIL.
4036 */
4037 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 char_u **nextcmd;
4042 int evaluate;
4043{
4044 int ret;
4045 char_u *p;
4046
4047 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (ret == FAIL || !ends_excmd(*p))
4050 {
4051 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /*
4054 * Report the invalid expression unless the expression evaluation has
4055 * been cancelled due to an aborting error, an interrupt, or an
4056 * exception.
4057 */
4058 if (!aborting())
4059 EMSG2(_(e_invexpr2), arg);
4060 ret = FAIL;
4061 }
4062 if (nextcmd != NULL)
4063 *nextcmd = check_nextcmd(p);
4064
4065 return ret;
4066}
4067
4068/*
4069 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004070 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 *
4072 * "arg" must point to the first non-white of the expression.
4073 * "arg" is advanced to the next non-white after the recognized expression.
4074 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004075 * Note: "rettv.v_lock" is not set.
4076 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
4085 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /*
4089 * Get the first variable.
4090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093
4094 if ((*arg)[0] == '?')
4095 {
4096 result = FALSE;
4097 if (evaluate)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 int error = FALSE;
4100
4101 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (error)
4105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107
4108 /*
4109 * Get the second variable.
4110 */
4111 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114
4115 /*
4116 * Check for the ":".
4117 */
4118 if ((*arg)[0] != ':')
4119 {
4120 EMSG(_("E109: Missing ':' after '?'"));
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125
4126 /*
4127 * Get the third variable.
4128 */
4129 *arg = skipwhite(*arg + 1);
4130 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4131 {
4132 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135 }
4136 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139
4140 return OK;
4141}
4142
4143/*
4144 * Handle first level expression:
4145 * expr2 || expr2 || expr2 logical OR
4146 *
4147 * "arg" must point to the first non-white of the expression.
4148 * "arg" is advanced to the next non-white after the recognized expression.
4149 *
4150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
Bram Moolenaar33570922005-01-25 22:26:29 +00004158 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 long result;
4160 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163 /*
4164 * Get the first variable.
4165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168
4169 /*
4170 * Repeat until there is no following "||".
4171 */
4172 first = TRUE;
4173 result = FALSE;
4174 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4175 {
4176 if (evaluate && first)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 if (error)
4182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 first = FALSE;
4184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 2);
4190 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4191 return FAIL;
4192
4193 /*
4194 * Compute the result.
4195 */
4196 if (evaluate && !result)
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (error)
4202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 if (evaluate)
4205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 rettv->v_type = VAR_NUMBER;
4207 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209 }
4210
4211 return OK;
4212}
4213
4214/*
4215 * Handle second level expression:
4216 * expr3 && expr3 && expr3 logical AND
4217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 long result;
4231 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat until there is no following "&&".
4242 */
4243 first = TRUE;
4244 result = TRUE;
4245 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4246 {
4247 if (evaluate && first)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 first = FALSE;
4255 }
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 2);
4261 if (eval4(arg, &var2, evaluate && result) == FAIL)
4262 return FAIL;
4263
4264 /*
4265 * Compute the result.
4266 */
4267 if (evaluate && result)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 if (evaluate)
4276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle third level expression:
4287 * var1 == var2
4288 * var1 =~ var2
4289 * var1 != var2
4290 * var1 !~ var2
4291 * var1 > var2
4292 * var1 >= var2
4293 * var1 < var2
4294 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 * var1 is var2
4296 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int evaluate;
4308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 char_u *p;
4311 int i;
4312 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004313 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int len = 2;
4315 long n1, n2;
4316 char_u *s1, *s2;
4317 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4318 regmatch_T regmatch;
4319 int ic;
4320 char_u *save_cpo;
4321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 p = *arg;
4329 switch (p[0])
4330 {
4331 case '=': if (p[1] == '=')
4332 type = TYPE_EQUAL;
4333 else if (p[1] == '~')
4334 type = TYPE_MATCH;
4335 break;
4336 case '!': if (p[1] == '=')
4337 type = TYPE_NEQUAL;
4338 else if (p[1] == '~')
4339 type = TYPE_NOMATCH;
4340 break;
4341 case '>': if (p[1] != '=')
4342 {
4343 type = TYPE_GREATER;
4344 len = 1;
4345 }
4346 else
4347 type = TYPE_GEQUAL;
4348 break;
4349 case '<': if (p[1] != '=')
4350 {
4351 type = TYPE_SMALLER;
4352 len = 1;
4353 }
4354 else
4355 type = TYPE_SEQUAL;
4356 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004357 case 'i': if (p[1] == 's')
4358 {
4359 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4360 len = 5;
4361 if (!vim_isIDc(p[len]))
4362 {
4363 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4364 type_is = TRUE;
4365 }
4366 }
4367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369
4370 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 if (type != TYPE_UNKNOWN)
4374 {
4375 /* extra question mark appended: ignore case */
4376 if (p[len] == '?')
4377 {
4378 ic = TRUE;
4379 ++len;
4380 }
4381 /* extra '#' appended: match case */
4382 else if (p[len] == '#')
4383 {
4384 ic = FALSE;
4385 ++len;
4386 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 ic = p_ic;
4390
4391 /*
4392 * Get the second variable.
4393 */
4394 *arg = skipwhite(p + len);
4395 if (eval5(arg, &var2, evaluate) == FAIL)
4396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 return FAIL;
4399 }
4400
4401 if (evaluate)
4402 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 if (type_is && rettv->v_type != var2.v_type)
4404 {
4405 /* For "is" a different type always means FALSE, for "notis"
4406 * it means TRUE. */
4407 n1 = (type == TYPE_NEQUAL);
4408 }
4409 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4410 {
4411 if (type_is)
4412 {
4413 n1 = (rettv->v_type == var2.v_type
4414 && rettv->vval.v_list == var2.vval.v_list);
4415 if (type == TYPE_NEQUAL)
4416 n1 = !n1;
4417 }
4418 else if (rettv->v_type != var2.v_type
4419 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4420 {
4421 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004422 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004424 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
4429 else
4430 {
4431 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004432 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4433 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 }
4438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004439 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4440 {
4441 if (type_is)
4442 {
4443 n1 = (rettv->v_type == var2.v_type
4444 && rettv->vval.v_dict == var2.vval.v_dict);
4445 if (type == TYPE_NEQUAL)
4446 n1 = !n1;
4447 }
4448 else if (rettv->v_type != var2.v_type
4449 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4450 {
4451 if (rettv->v_type != var2.v_type)
4452 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4453 else
4454 EMSG(_("E736: Invalid operation for Dictionary"));
4455 clear_tv(rettv);
4456 clear_tv(&var2);
4457 return FAIL;
4458 }
4459 else
4460 {
4461 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004462 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4463 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004464 if (type == TYPE_NEQUAL)
4465 n1 = !n1;
4466 }
4467 }
4468
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4470 {
4471 if (rettv->v_type != var2.v_type
4472 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4473 {
4474 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004475 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004477 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 clear_tv(rettv);
4479 clear_tv(&var2);
4480 return FAIL;
4481 }
4482 else
4483 {
4484 /* Compare two Funcrefs for being equal or unequal. */
4485 if (rettv->vval.v_string == NULL
4486 || var2.vval.v_string == NULL)
4487 n1 = FALSE;
4488 else
4489 n1 = STRCMP(rettv->vval.v_string,
4490 var2.vval.v_string) == 0;
4491 if (type == TYPE_NEQUAL)
4492 n1 = !n1;
4493 }
4494 }
4495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496#ifdef FEAT_FLOAT
4497 /*
4498 * If one of the two variables is a float, compare as a float.
4499 * When using "=~" or "!~", always compare as string.
4500 */
4501 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4502 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4503 {
4504 float_T f1, f2;
4505
4506 if (rettv->v_type == VAR_FLOAT)
4507 f1 = rettv->vval.v_float;
4508 else
4509 f1 = get_tv_number(rettv);
4510 if (var2.v_type == VAR_FLOAT)
4511 f2 = var2.vval.v_float;
4512 else
4513 f2 = get_tv_number(&var2);
4514 n1 = FALSE;
4515 switch (type)
4516 {
4517 case TYPE_EQUAL: n1 = (f1 == f2); break;
4518 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4519 case TYPE_GREATER: n1 = (f1 > f2); break;
4520 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4521 case TYPE_SMALLER: n1 = (f1 < f2); break;
4522 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4523 case TYPE_UNKNOWN:
4524 case TYPE_MATCH:
4525 case TYPE_NOMATCH: break; /* avoid gcc warning */
4526 }
4527 }
4528#endif
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /*
4531 * If one of the two variables is a number, compare as a number.
4532 * When using "=~" or "!~", always compare as string.
4533 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 n1 = get_tv_number(rettv);
4538 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 switch (type)
4540 {
4541 case TYPE_EQUAL: n1 = (n1 == n2); break;
4542 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4543 case TYPE_GREATER: n1 = (n1 > n2); break;
4544 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4545 case TYPE_SMALLER: n1 = (n1 < n2); break;
4546 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4547 case TYPE_UNKNOWN:
4548 case TYPE_MATCH:
4549 case TYPE_NOMATCH: break; /* avoid gcc warning */
4550 }
4551 }
4552 else
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 s1 = get_tv_string_buf(rettv, buf1);
4555 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4557 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4558 else
4559 i = 0;
4560 n1 = FALSE;
4561 switch (type)
4562 {
4563 case TYPE_EQUAL: n1 = (i == 0); break;
4564 case TYPE_NEQUAL: n1 = (i != 0); break;
4565 case TYPE_GREATER: n1 = (i > 0); break;
4566 case TYPE_GEQUAL: n1 = (i >= 0); break;
4567 case TYPE_SMALLER: n1 = (i < 0); break;
4568 case TYPE_SEQUAL: n1 = (i <= 0); break;
4569
4570 case TYPE_MATCH:
4571 case TYPE_NOMATCH:
4572 /* avoid 'l' flag in 'cpoptions' */
4573 save_cpo = p_cpo;
4574 p_cpo = (char_u *)"";
4575 regmatch.regprog = vim_regcomp(s2,
4576 RE_MAGIC + RE_STRING);
4577 regmatch.rm_ic = ic;
4578 if (regmatch.regprog != NULL)
4579 {
4580 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4581 vim_free(regmatch.regprog);
4582 if (type == TYPE_NOMATCH)
4583 n1 = !n1;
4584 }
4585 p_cpo = save_cpo;
4586 break;
4587
4588 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4589 }
4590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597
4598 return OK;
4599}
4600
4601/*
4602 * Handle fourth level expression:
4603 * + number addition
4604 * - number subtraction
4605 * . string concatenation
4606 *
4607 * "arg" must point to the first non-white of the expression.
4608 * "arg" is advanced to the next non-white after the recognized expression.
4609 *
4610 * Return OK or FAIL.
4611 */
4612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 int evaluate;
4617{
Bram Moolenaar33570922005-01-25 22:26:29 +00004618 typval_T var2;
4619 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int op;
4621 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622#ifdef FEAT_FLOAT
4623 float_T f1 = 0, f2 = 0;
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u *s1, *s2;
4626 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4627 char_u *p;
4628
4629 /*
4630 * Get the first variable.
4631 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004632 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return FAIL;
4634
4635 /*
4636 * Repeat computing, until no '+', '-' or '.' is following.
4637 */
4638 for (;;)
4639 {
4640 op = **arg;
4641 if (op != '+' && op != '-' && op != '.')
4642 break;
4643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004644 if ((op != '+' || rettv->v_type != VAR_LIST)
4645#ifdef FEAT_FLOAT
4646 && (op == '.' || rettv->v_type != VAR_FLOAT)
4647#endif
4648 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 {
4650 /* For "list + ...", an illegal use of the first operand as
4651 * a number cannot be determined before evaluating the 2nd
4652 * operand: if this is also a list, all is ok.
4653 * For "something . ...", "something - ..." or "non-list + ...",
4654 * we know that the first operand needs to be a string or number
4655 * without evaluating the 2nd operand. So check before to avoid
4656 * side effects after an error. */
4657 if (evaluate && get_tv_string_chk(rettv) == NULL)
4658 {
4659 clear_tv(rettv);
4660 return FAIL;
4661 }
4662 }
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /*
4665 * Get the second variable.
4666 */
4667 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004668 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return FAIL;
4672 }
4673
4674 if (evaluate)
4675 {
4676 /*
4677 * Compute the result.
4678 */
4679 if (op == '.')
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4682 s2 = get_tv_string_buf_chk(&var2, buf2);
4683 if (s2 == NULL) /* type error ? */
4684 {
4685 clear_tv(rettv);
4686 clear_tv(&var2);
4687 return FAIL;
4688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004689 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 rettv->v_type = VAR_STRING;
4692 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004694 else if (op == '+' && rettv->v_type == VAR_LIST
4695 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004696 {
4697 /* concatenate Lists */
4698 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4699 &var3) == FAIL)
4700 {
4701 clear_tv(rettv);
4702 clear_tv(&var2);
4703 return FAIL;
4704 }
4705 clear_tv(rettv);
4706 *rettv = var3;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 else
4709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 int error = FALSE;
4711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 f1 = rettv->vval.v_float;
4716 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 else
4719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721 n1 = get_tv_number_chk(rettv, &error);
4722 if (error)
4723 {
4724 /* This can only happen for "list + non-list". For
4725 * "non-list + ..." or "something - ...", we returned
4726 * before evaluating the 2nd operand. */
4727 clear_tv(rettv);
4728 return FAIL;
4729 }
4730#ifdef FEAT_FLOAT
4731 if (var2.v_type == VAR_FLOAT)
4732 f1 = n1;
4733#endif
4734 }
4735#ifdef FEAT_FLOAT
4736 if (var2.v_type == VAR_FLOAT)
4737 {
4738 f2 = var2.vval.v_float;
4739 n2 = 0;
4740 }
4741 else
4742#endif
4743 {
4744 n2 = get_tv_number_chk(&var2, &error);
4745 if (error)
4746 {
4747 clear_tv(rettv);
4748 clear_tv(&var2);
4749 return FAIL;
4750 }
4751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
4753 f2 = n2;
4754#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757
4758#ifdef FEAT_FLOAT
4759 /* If there is a float on either side the result is a float. */
4760 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4761 {
4762 if (op == '+')
4763 f1 = f1 + f2;
4764 else
4765 f1 = f1 - f2;
4766 rettv->v_type = VAR_FLOAT;
4767 rettv->vval.v_float = f1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#endif
4771 {
4772 if (op == '+')
4773 n1 = n1 + n2;
4774 else
4775 n1 = n1 - n2;
4776 rettv->v_type = VAR_NUMBER;
4777 rettv->vval.v_number = n1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 }
4783 return OK;
4784}
4785
4786/*
4787 * Handle fifth level expression:
4788 * * number multiplication
4789 * / number division
4790 * % number modulo
4791 *
4792 * "arg" must point to the first non-white of the expression.
4793 * "arg" is advanced to the next non-white after the recognized expression.
4794 *
4795 * Return OK or FAIL.
4796 */
4797 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 int op;
4806 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807#ifdef FEAT_FLOAT
4808 int use_float = FALSE;
4809 float_T f1 = 0, f2;
4810#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 /*
4814 * Get the first variable.
4815 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004816 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
4818
4819 /*
4820 * Repeat computing, until no '*', '/' or '%' is following.
4821 */
4822 for (;;)
4823 {
4824 op = **arg;
4825 if (op != '*' && op != '/' && op != '%')
4826 break;
4827
4828 if (evaluate)
4829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#ifdef FEAT_FLOAT
4831 if (rettv->v_type == VAR_FLOAT)
4832 {
4833 f1 = rettv->vval.v_float;
4834 use_float = TRUE;
4835 n1 = 0;
4836 }
4837 else
4838#endif
4839 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 if (error)
4842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 n1 = 0;
4846
4847 /*
4848 * Get the second variable.
4849 */
4850 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004851 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return FAIL;
4853
4854 if (evaluate)
4855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856#ifdef FEAT_FLOAT
4857 if (var2.v_type == VAR_FLOAT)
4858 {
4859 if (!use_float)
4860 {
4861 f1 = n1;
4862 use_float = TRUE;
4863 }
4864 f2 = var2.vval.v_float;
4865 n2 = 0;
4866 }
4867 else
4868#endif
4869 {
4870 n2 = get_tv_number_chk(&var2, &error);
4871 clear_tv(&var2);
4872 if (error)
4873 return FAIL;
4874#ifdef FEAT_FLOAT
4875 if (use_float)
4876 f2 = n2;
4877#endif
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884#ifdef FEAT_FLOAT
4885 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 if (op == '*')
4888 f1 = f1 * f2;
4889 else if (op == '/')
4890 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891# ifdef VMS
4892 /* VMS crashes on divide by zero, work around it */
4893 if (f2 == 0.0)
4894 {
4895 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004896 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004898 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004899 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004900 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901 }
4902 else
4903 f1 = f1 / f2;
4904# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 /* We rely on the floating point library to handle divide
4906 * by zero to result in "inf" and not a crash. */
4907 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004912 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 return FAIL;
4914 }
4915 rettv->v_type = VAR_FLOAT;
4916 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 n1 = n1 * n2;
4923 else if (op == '/')
4924 {
4925 if (n2 == 0) /* give an error message? */
4926 {
4927 if (n1 == 0)
4928 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4929 else if (n1 < 0)
4930 n1 = -0x7fffffffL;
4931 else
4932 n1 = 0x7fffffffL;
4933 }
4934 else
4935 n1 = n1 / n2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938 {
4939 if (n2 == 0) /* give an error message? */
4940 n1 = 0;
4941 else
4942 n1 = n1 % n2;
4943 }
4944 rettv->v_type = VAR_NUMBER;
4945 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 }
4949
4950 return OK;
4951}
4952
4953/*
4954 * Handle sixth level expression:
4955 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004956 * "string" string constant
4957 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 * &option-name option value
4959 * @r register contents
4960 * identifier variable value
4961 * function() function call
4962 * $VAR environment variable
4963 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004964 * [expr, expr] List
4965 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 *
4967 * Also handle:
4968 * ! in front logical NOT
4969 * - in front unary minus
4970 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * trailing [] subscript in String or List
4972 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * "arg" must point to the first non-white of the expression.
4975 * "arg" is advanced to the next non-white after the recognized expression.
4976 *
4977 * Return OK or FAIL.
4978 */
4979 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004980eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004984 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 long n;
4987 int len;
4988 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u *start_leader, *end_leader;
4990 int ret = OK;
4991 char_u *alias;
4992
4993 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 /*
5000 * Skip '!' and '-' characters. They are handled later.
5001 */
5002 start_leader = *arg;
5003 while (**arg == '!' || **arg == '-' || **arg == '+')
5004 *arg = skipwhite(*arg + 1);
5005 end_leader = *arg;
5006
5007 switch (**arg)
5008 {
5009 /*
5010 * Number constant.
5011 */
5012 case '0':
5013 case '1':
5014 case '2':
5015 case '3':
5016 case '4':
5017 case '5':
5018 case '6':
5019 case '7':
5020 case '8':
5021 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 {
5023#ifdef FEAT_FLOAT
5024 char_u *p = skipdigits(*arg + 1);
5025 int get_float = FALSE;
5026
5027 /* We accept a float when the format matches
5028 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005029 * strict to avoid backwards compatibility problems.
5030 * Don't look for a float after the "." operator, so that
5031 * ":let vers = 1.2.3" doesn't fail. */
5032 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 get_float = TRUE;
5035 p = skipdigits(p + 2);
5036 if (*p == 'e' || *p == 'E')
5037 {
5038 ++p;
5039 if (*p == '-' || *p == '+')
5040 ++p;
5041 if (!vim_isdigit(*p))
5042 get_float = FALSE;
5043 else
5044 p = skipdigits(p + 1);
5045 }
5046 if (ASCII_ISALPHA(*p) || *p == '.')
5047 get_float = FALSE;
5048 }
5049 if (get_float)
5050 {
5051 float_T f;
5052
5053 *arg += string2float(*arg, &f);
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_FLOAT;
5057 rettv->vval.v_float = f;
5058 }
5059 }
5060 else
5061#endif
5062 {
5063 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5064 *arg += len;
5065 if (evaluate)
5066 {
5067 rettv->v_type = VAR_NUMBER;
5068 rettv->vval.v_number = n;
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * String constant: "string".
5076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079
5080 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 break;
5085
5086 /*
5087 * List: [expr, expr]
5088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Dictionary: {key: val, key: val}
5094 */
5095 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5096 break;
5097
5098 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005099 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005101 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /*
5105 * Environment variable: $VAR.
5106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /*
5111 * Register contents: @r.
5112 */
5113 case '@': ++*arg;
5114 if (evaluate)
5115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005117 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 if (**arg != NUL)
5120 ++*arg;
5121 break;
5122
5123 /*
5124 * nested expression: (expression).
5125 */
5126 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (**arg == ')')
5129 ++*arg;
5130 else if (ret == OK)
5131 {
5132 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 ret = FAIL;
5135 }
5136 break;
5137
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141
5142 if (ret == NOTDONE)
5143 {
5144 /*
5145 * Must be a variable or function name.
5146 * Can also be a curly-braces kind of name: {expr}.
5147 */
5148 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 if (alias != NULL)
5151 s = alias;
5152
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 ret = FAIL;
5155 else
5156 {
5157 if (**arg == '(') /* recursive! */
5158 {
5159 /* If "s" is the name of a variable of type VAR_FUNC
5160 * use its contents. */
5161 s = deref_func_name(s, &len);
5162
5163 /* Invoke the function. */
5164 ret = get_func_tv(s, len, rettv, arg,
5165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 /* Stop the expression evaluation when immediately
5168 * aborting on error, or when an interrupt occurred or
5169 * an exception was thrown but not caught. */
5170 if (aborting())
5171 {
5172 if (ret == OK)
5173 clear_tv(rettv);
5174 ret = FAIL;
5175 }
5176 }
5177 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005178 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005179 else
5180 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005182 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 }
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 *arg = skipwhite(*arg);
5186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5188 * expr(expr). */
5189 if (ret == OK)
5190 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192 /*
5193 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5194 */
5195 if (ret == OK && evaluate && end_leader > start_leader)
5196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 int val = 0;
5199#ifdef FEAT_FLOAT
5200 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 if (rettv->v_type == VAR_FLOAT)
5203 f = rettv->vval.v_float;
5204 else
5205#endif
5206 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 clear_tv(rettv);
5210 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 else
5213 {
5214 while (end_leader > start_leader)
5215 {
5216 --end_leader;
5217 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 {
5219#ifdef FEAT_FLOAT
5220 if (rettv->v_type == VAR_FLOAT)
5221 f = !f;
5222 else
5223#endif
5224 val = !val;
5225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005227 {
5228#ifdef FEAT_FLOAT
5229 if (rettv->v_type == VAR_FLOAT)
5230 f = -f;
5231 else
5232#endif
5233 val = -val;
5234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005236#ifdef FEAT_FLOAT
5237 if (rettv->v_type == VAR_FLOAT)
5238 {
5239 clear_tv(rettv);
5240 rettv->vval.v_float = f;
5241 }
5242 else
5243#endif
5244 {
5245 clear_tv(rettv);
5246 rettv->v_type = VAR_NUMBER;
5247 rettv->vval.v_number = val;
5248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251
5252 return ret;
5253}
5254
5255/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005256 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5257 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5259 */
5260 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266{
5267 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 long len = -1;
5271 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005275 if (rettv->v_type == VAR_FUNC
5276#ifdef FEAT_FLOAT
5277 || rettv->v_type == VAR_FLOAT
5278#endif
5279 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005281 if (verbose)
5282 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 return FAIL;
5284 }
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 /*
5289 * dict.name
5290 */
5291 key = *arg + 1;
5292 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5293 ;
5294 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 }
5298 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 /*
5301 * something[idx]
5302 *
5303 * Get the (first) variable from inside the [].
5304 */
5305 *arg = skipwhite(*arg + 1);
5306 if (**arg == ':')
5307 empty1 = TRUE;
5308 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5309 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5311 {
5312 /* not a number or string */
5313 clear_tv(&var1);
5314 return FAIL;
5315 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005316
5317 /*
5318 * Get the second variable from inside the [:].
5319 */
5320 if (**arg == ':')
5321 {
5322 range = TRUE;
5323 *arg = skipwhite(*arg + 1);
5324 if (**arg == ']')
5325 empty2 = TRUE;
5326 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 if (!empty1)
5329 clear_tv(&var1);
5330 return FAIL;
5331 }
5332 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5333 {
5334 /* not a number or string */
5335 if (!empty1)
5336 clear_tv(&var1);
5337 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 return FAIL;
5339 }
5340 }
5341
5342 /* Check for the ']'. */
5343 if (**arg != ']')
5344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345 if (verbose)
5346 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 clear_tv(&var1);
5348 if (range)
5349 clear_tv(&var2);
5350 return FAIL;
5351 }
5352 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 }
5354
5355 if (evaluate)
5356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 n1 = 0;
5358 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 n1 = get_tv_number(&var1);
5361 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 }
5363 if (range)
5364 {
5365 if (empty2)
5366 n2 = -1;
5367 else
5368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005369 n2 = get_tv_number(&var2);
5370 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 }
5372 }
5373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 {
5376 case VAR_NUMBER:
5377 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 len = (long)STRLEN(s);
5380 if (range)
5381 {
5382 /* The resulting variable is a substring. If the indexes
5383 * are out of range the result is empty. */
5384 if (n1 < 0)
5385 {
5386 n1 = len + n1;
5387 if (n1 < 0)
5388 n1 = 0;
5389 }
5390 if (n2 < 0)
5391 n2 = len + n2;
5392 else if (n2 >= len)
5393 n2 = len;
5394 if (n1 >= len || n2 < 0 || n1 > n2)
5395 s = NULL;
5396 else
5397 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5398 }
5399 else
5400 {
5401 /* The resulting variable is a string of a single
5402 * character. If the index is too big or negative the
5403 * result is empty. */
5404 if (n1 >= len || n1 < 0)
5405 s = NULL;
5406 else
5407 s = vim_strnsave(s + n1, 1);
5408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 clear_tv(rettv);
5410 rettv->v_type = VAR_STRING;
5411 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 break;
5413
5414 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 if (n1 < 0)
5417 n1 = len + n1;
5418 if (!empty1 && (n1 < 0 || n1 >= len))
5419 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005420 /* For a range we allow invalid values and return an empty
5421 * list. A list index out of range is an error. */
5422 if (!range)
5423 {
5424 if (verbose)
5425 EMSGN(_(e_listidx), n1);
5426 return FAIL;
5427 }
5428 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 if (range)
5431 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 list_T *l;
5433 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434
5435 if (n2 < 0)
5436 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005437 else if (n2 >= len)
5438 n2 = len - 1;
5439 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005440 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 l = list_alloc();
5442 if (l == NULL)
5443 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 n1 <= n2; ++n1)
5446 {
5447 if (list_append_tv(l, &item->li_tv) == FAIL)
5448 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005449 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 return FAIL;
5451 }
5452 item = item->li_next;
5453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 clear_tv(rettv);
5455 rettv->v_type = VAR_LIST;
5456 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005457 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 else
5460 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005461 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 clear_tv(rettv);
5463 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 }
5465 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466
5467 case VAR_DICT:
5468 if (range)
5469 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 if (verbose)
5471 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 if (len == -1)
5473 clear_tv(&var1);
5474 return FAIL;
5475 }
5476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478
5479 if (len == -1)
5480 {
5481 key = get_tv_string(&var1);
5482 if (*key == NUL)
5483 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005484 if (verbose)
5485 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486 clear_tv(&var1);
5487 return FAIL;
5488 }
5489 }
5490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005491 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005493 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005494 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495 if (len == -1)
5496 clear_tv(&var1);
5497 if (item == NULL)
5498 return FAIL;
5499
5500 copy_tv(&item->di_tv, &var1);
5501 clear_tv(rettv);
5502 *rettv = var1;
5503 }
5504 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 }
5506 }
5507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 return OK;
5509}
5510
5511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * Get an option value.
5513 * "arg" points to the '&' or '+' before the option name.
5514 * "arg" is advanced to character after the option name.
5515 * Return OK or FAIL.
5516 */
5517 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 int evaluate;
5522{
5523 char_u *option_end;
5524 long numval;
5525 char_u *stringval;
5526 int opt_type;
5527 int c;
5528 int working = (**arg == '+'); /* has("+option") */
5529 int ret = OK;
5530 int opt_flags;
5531
5532 /*
5533 * Isolate the option name and find its value.
5534 */
5535 option_end = find_option_end(arg, &opt_flags);
5536 if (option_end == NULL)
5537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 EMSG2(_("E112: Option name missing: %s"), *arg);
5540 return FAIL;
5541 }
5542
5543 if (!evaluate)
5544 {
5545 *arg = option_end;
5546 return OK;
5547 }
5548
5549 c = *option_end;
5550 *option_end = NUL;
5551 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 if (opt_type == -3) /* invalid name */
5555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 EMSG2(_("E113: Unknown option: %s"), *arg);
5558 ret = FAIL;
5559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 if (opt_type == -2) /* hidden string option */
5563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 rettv->v_type = VAR_STRING;
5565 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 else if (opt_type == -1) /* hidden number option */
5568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 rettv->v_type = VAR_NUMBER;
5570 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 else if (opt_type == 1) /* number option */
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv->v_type = VAR_NUMBER;
5575 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else /* string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 }
5583 else if (working && (opt_type == -2 || opt_type == -1))
5584 ret = FAIL;
5585
5586 *option_end = c; /* put back for error messages */
5587 *arg = option_end;
5588
5589 return ret;
5590}
5591
5592/*
5593 * Allocate a variable for a string constant.
5594 * Return OK or FAIL.
5595 */
5596 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int evaluate;
5601{
5602 char_u *p;
5603 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 int extra = 0;
5605
5606 /*
5607 * Find the end of the string, skipping backslashed characters.
5608 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005609 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 {
5611 if (*p == '\\' && p[1] != NUL)
5612 {
5613 ++p;
5614 /* A "\<x>" form occupies at least 4 characters, and produces up
5615 * to 6 characters: reserve space for 2 extra */
5616 if (*p == '<')
5617 extra += 2;
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620
5621 if (*p != '"')
5622 {
5623 EMSG2(_("E114: Missing quote: %s"), *arg);
5624 return FAIL;
5625 }
5626
5627 /* If only parsing, set *arg and return here */
5628 if (!evaluate)
5629 {
5630 *arg = p + 1;
5631 return OK;
5632 }
5633
5634 /*
5635 * Copy the string into allocated memory, handling backslashed
5636 * characters.
5637 */
5638 name = alloc((unsigned)(p - *arg + extra));
5639 if (name == NULL)
5640 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 rettv->v_type = VAR_STRING;
5642 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 if (*p == '\\')
5647 {
5648 switch (*++p)
5649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 case 'b': *name++ = BS; ++p; break;
5651 case 'e': *name++ = ESC; ++p; break;
5652 case 'f': *name++ = FF; ++p; break;
5653 case 'n': *name++ = NL; ++p; break;
5654 case 'r': *name++ = CAR; ++p; break;
5655 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 case 'X': /* hex: "\x1", "\x12" */
5658 case 'x':
5659 case 'u': /* Unicode: "\u0023" */
5660 case 'U':
5661 if (vim_isxdigit(p[1]))
5662 {
5663 int n, nr;
5664 int c = toupper(*p);
5665
5666 if (c == 'X')
5667 n = 2;
5668 else
5669 n = 4;
5670 nr = 0;
5671 while (--n >= 0 && vim_isxdigit(p[1]))
5672 {
5673 ++p;
5674 nr = (nr << 4) + hex2nr(*p);
5675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#ifdef FEAT_MBYTE
5678 /* For "\u" store the number according to
5679 * 'encoding'. */
5680 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 else
5683#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687
5688 /* octal: "\1", "\12", "\123" */
5689 case '0':
5690 case '1':
5691 case '2':
5692 case '3':
5693 case '4':
5694 case '5':
5695 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 case '7': *name = *p++ - '0';
5697 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name = (*name << 3) + *p++ - '0';
5700 if (*p >= '0' && *p <= '7')
5701 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 break;
5705
5706 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (extra != 0)
5709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712 }
5713 /* FALLTHROUGH */
5714
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 break;
5717 }
5718 }
5719 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 *arg = p + 1;
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 return OK;
5727}
5728
5729/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 * Return OK or FAIL.
5732 */
5733 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 int evaluate;
5738{
5739 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 int reduce = 0;
5742
5743 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 */
5746 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5747 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 break;
5752 ++reduce;
5753 ++p;
5754 }
5755 }
5756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 return FAIL;
5761 }
5762
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 if (!evaluate)
5765 {
5766 *arg = p + 1;
5767 return OK;
5768 }
5769
5770 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 */
5773 str = alloc((unsigned)((p - *arg) - reduce));
5774 if (str == NULL)
5775 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 rettv->v_type = VAR_STRING;
5777 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 break;
5785 ++p;
5786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 *arg = p + 1;
5791
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 return OK;
5793}
5794
5795/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 * Allocate a variable for a List and fill it from "*arg".
5797 * Return OK or FAIL.
5798 */
5799 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005800get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 int evaluate;
5804{
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 list_T *l = NULL;
5806 typval_T tv;
5807 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808
5809 if (evaluate)
5810 {
5811 l = list_alloc();
5812 if (l == NULL)
5813 return FAIL;
5814 }
5815
5816 *arg = skipwhite(*arg + 1);
5817 while (**arg != ']' && **arg != NUL)
5818 {
5819 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5820 goto failret;
5821 if (evaluate)
5822 {
5823 item = listitem_alloc();
5824 if (item != NULL)
5825 {
5826 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005827 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 list_append(l, item);
5829 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005830 else
5831 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 }
5833
5834 if (**arg == ']')
5835 break;
5836 if (**arg != ',')
5837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839 goto failret;
5840 }
5841 *arg = skipwhite(*arg + 1);
5842 }
5843
5844 if (**arg != ']')
5845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847failret:
5848 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005849 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 return FAIL;
5851 }
5852
5853 *arg = skipwhite(*arg + 1);
5854 if (evaluate)
5855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005856 rettv->v_type = VAR_LIST;
5857 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 ++l->lv_refcount;
5859 }
5860
5861 return OK;
5862}
5863
5864/*
5865 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005866 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005868 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869list_alloc()
5870{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005871 list_T *l;
5872
5873 l = (list_T *)alloc_clear(sizeof(list_T));
5874 if (l != NULL)
5875 {
5876 /* Prepend the list to the list of lists for garbage collection. */
5877 if (first_list != NULL)
5878 first_list->lv_used_prev = l;
5879 l->lv_used_prev = NULL;
5880 l->lv_used_next = first_list;
5881 first_list = l;
5882 }
5883 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884}
5885
5886/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005887 * Allocate an empty list for a return value.
5888 * Returns OK or FAIL.
5889 */
5890 static int
5891rettv_list_alloc(rettv)
5892 typval_T *rettv;
5893{
5894 list_T *l = list_alloc();
5895
5896 if (l == NULL)
5897 return FAIL;
5898
5899 rettv->vval.v_list = l;
5900 rettv->v_type = VAR_LIST;
5901 ++l->lv_refcount;
5902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Unreference a list: decrement the reference count and free it when it
5907 * becomes zero.
5908 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005909 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005913 if (l != NULL && --l->lv_refcount <= 0)
5914 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915}
5916
5917/*
5918 * Free a list, including all items it points to.
5919 * Ignores the reference count.
5920 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005921 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005922list_free(l, recurse)
5923 list_T *l;
5924 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005928 /* Remove the list from the list of lists for garbage collection. */
5929 if (l->lv_used_prev == NULL)
5930 first_list = l->lv_used_next;
5931 else
5932 l->lv_used_prev->lv_used_next = l->lv_used_next;
5933 if (l->lv_used_next != NULL)
5934 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5935
Bram Moolenaard9fba312005-06-26 22:34:35 +00005936 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005938 /* Remove the item before deleting it. */
5939 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005940 if (recurse || (item->li_tv.v_type != VAR_LIST
5941 && item->li_tv.v_type != VAR_DICT))
5942 clear_tv(&item->li_tv);
5943 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 }
5945 vim_free(l);
5946}
5947
5948/*
5949 * Allocate a list item.
5950 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005951 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952listitem_alloc()
5953{
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955}
5956
5957/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005958 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 */
5960 static void
5961listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005962 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 vim_free(item);
5966}
5967
5968/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005969 * Remove a list item from a List and free it. Also clears the value.
5970 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005971 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 list_T *l;
5974 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975{
5976 list_remove(l, item, item);
5977 listitem_free(item);
5978}
5979
5980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 * Get the number of items in a list.
5982 */
5983 static long
5984list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 if (l == NULL)
5988 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005989 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990}
5991
5992/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993 * Return TRUE when two lists have exactly the same values.
5994 */
5995 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005997 list_T *l1;
5998 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001{
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006004 if (l1 == NULL || l2 == NULL)
6005 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006006 if (l1 == l2)
6007 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008 if (list_len(l1) != list_len(l2))
6009 return FALSE;
6010
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 for (item1 = l1->lv_first, item2 = l2->lv_first;
6012 item1 != NULL && item2 != NULL;
6013 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015 return FALSE;
6016 return item1 == NULL && item2 == NULL;
6017}
6018
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006019#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6020 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006021/*
6022 * Return the dictitem that an entry in a hashtable points to.
6023 */
6024 dictitem_T *
6025dict_lookup(hi)
6026 hashitem_T *hi;
6027{
6028 return HI2DI(hi);
6029}
6030#endif
6031
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006032/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 * Return TRUE when two dictionaries have exactly the same key/values.
6034 */
6035 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 dict_T *d1;
6038 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041{
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 hashitem_T *hi;
6043 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006044 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006046 if (d1 == NULL || d2 == NULL)
6047 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006048 if (d1 == d2)
6049 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006050 if (dict_len(d1) != dict_len(d2))
6051 return FALSE;
6052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006053 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 if (!HASHITEM_EMPTY(hi))
6057 {
6058 item2 = dict_find(d2, hi->hi_key, -1);
6059 if (item2 == NULL)
6060 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006062 return FALSE;
6063 --todo;
6064 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 }
6066 return TRUE;
6067}
6068
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006069static int tv_equal_recurse_limit;
6070
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006072 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006073 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006074 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 */
6076 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 typval_T *tv1;
6079 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080 int ic; /* ignore case */
6081 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082{
6083 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006084 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006086 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006088 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006089 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092 * recursiveness to a limit. We guess they are equal then.
6093 * A fixed limit has the problem of still taking an awful long time.
6094 * Reduce the limit every time running into it. That should work fine for
6095 * deeply linked structures that are not recursively linked and catch
6096 * recursiveness quickly. */
6097 if (!recursive)
6098 tv_equal_recurse_limit = 1000;
6099 if (recursive_cnt >= tv_equal_recurse_limit)
6100 {
6101 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006102 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104
6105 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 ++recursive_cnt;
6109 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6110 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 ++recursive_cnt;
6115 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6116 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006117 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006118
6119 case VAR_FUNC:
6120 return (tv1->vval.v_string != NULL
6121 && tv2->vval.v_string != NULL
6122 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6123
6124 case VAR_NUMBER:
6125 return tv1->vval.v_number == tv2->vval.v_number;
6126
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006127#ifdef FEAT_FLOAT
6128 case VAR_FLOAT:
6129 return tv1->vval.v_float == tv2->vval.v_float;
6130#endif
6131
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_STRING:
6133 s1 = get_tv_string_buf(tv1, buf1);
6134 s2 = get_tv_string_buf(tv2, buf2);
6135 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 return TRUE;
6140}
6141
6142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 * Locate item with index "n" in list "l" and return it.
6144 * A negative index is counted from the end; -1 is the last item.
6145 * Returns NULL when "n" is out of range.
6146 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006147 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 long n;
6151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 long idx;
6154
6155 if (l == NULL)
6156 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006157
6158 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160 n = l->lv_len + n;
6161
6162 /* Check for index out of range. */
6163 if (n < 0 || n >= l->lv_len)
6164 return NULL;
6165
6166 /* When there is a cached index may start search from there. */
6167 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006169 if (n < l->lv_idx / 2)
6170 {
6171 /* closest to the start of the list */
6172 item = l->lv_first;
6173 idx = 0;
6174 }
6175 else if (n > (l->lv_idx + l->lv_len) / 2)
6176 {
6177 /* closest to the end of the list */
6178 item = l->lv_last;
6179 idx = l->lv_len - 1;
6180 }
6181 else
6182 {
6183 /* closest to the cached index */
6184 item = l->lv_idx_item;
6185 idx = l->lv_idx;
6186 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006187 }
6188 else
6189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006190 if (n < l->lv_len / 2)
6191 {
6192 /* closest to the start of the list */
6193 item = l->lv_first;
6194 idx = 0;
6195 }
6196 else
6197 {
6198 /* closest to the end of the list */
6199 item = l->lv_last;
6200 idx = l->lv_len - 1;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006203
6204 while (n > idx)
6205 {
6206 /* search forward */
6207 item = item->li_next;
6208 ++idx;
6209 }
6210 while (n < idx)
6211 {
6212 /* search backward */
6213 item = item->li_prev;
6214 --idx;
6215 }
6216
6217 /* cache the used index */
6218 l->lv_idx = idx;
6219 l->lv_idx_item = item;
6220
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 return item;
6222}
6223
6224/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006225 * Get list item "l[idx]" as a number.
6226 */
6227 static long
6228list_find_nr(l, idx, errorp)
6229 list_T *l;
6230 long idx;
6231 int *errorp; /* set to TRUE when something wrong */
6232{
6233 listitem_T *li;
6234
6235 li = list_find(l, idx);
6236 if (li == NULL)
6237 {
6238 if (errorp != NULL)
6239 *errorp = TRUE;
6240 return -1L;
6241 }
6242 return get_tv_number_chk(&li->li_tv, errorp);
6243}
6244
6245/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006246 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6247 */
6248 char_u *
6249list_find_str(l, idx)
6250 list_T *l;
6251 long idx;
6252{
6253 listitem_T *li;
6254
6255 li = list_find(l, idx - 1);
6256 if (li == NULL)
6257 {
6258 EMSGN(_(e_listidx), idx);
6259 return NULL;
6260 }
6261 return get_tv_string(&li->li_tv);
6262}
6263
6264/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006265 * Locate "item" list "l" and return its index.
6266 * Returns -1 when "item" is not in the list.
6267 */
6268 static long
6269list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 list_T *l;
6271 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006272{
6273 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275
6276 if (l == NULL)
6277 return -1;
6278 idx = 0;
6279 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6280 ++idx;
6281 if (li == NULL)
6282 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006283 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006284}
6285
6286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 * Append item "item" to the end of list "l".
6288 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 list_T *l;
6292 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293{
6294 if (l->lv_last == NULL)
6295 {
6296 /* empty list */
6297 l->lv_first = item;
6298 l->lv_last = item;
6299 item->li_prev = NULL;
6300 }
6301 else
6302 {
6303 l->lv_last->li_next = item;
6304 item->li_prev = l->lv_last;
6305 l->lv_last = item;
6306 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 item->li_next = NULL;
6309}
6310
6311/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006315 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 list_T *l;
6318 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321
Bram Moolenaar05159a02005-02-26 23:04:13 +00006322 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006324 copy_tv(tv, &li->li_tv);
6325 list_append(l, li);
6326 return OK;
6327}
6328
6329/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006330 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006331 * Return FAIL when out of memory.
6332 */
6333 int
6334list_append_dict(list, dict)
6335 list_T *list;
6336 dict_T *dict;
6337{
6338 listitem_T *li = listitem_alloc();
6339
6340 if (li == NULL)
6341 return FAIL;
6342 li->li_tv.v_type = VAR_DICT;
6343 li->li_tv.v_lock = 0;
6344 li->li_tv.vval.v_dict = dict;
6345 list_append(list, li);
6346 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 return OK;
6348}
6349
6350/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353 * Returns FAIL when out of memory.
6354 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006355 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006356list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006357 list_T *l;
6358 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006359 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006360{
6361 listitem_T *li = listitem_alloc();
6362
6363 if (li == NULL)
6364 return FAIL;
6365 list_append(l, li);
6366 li->li_tv.v_type = VAR_STRING;
6367 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006368 if (str == NULL)
6369 li->li_tv.vval.v_string = NULL;
6370 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006371 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 return FAIL;
6373 return OK;
6374}
6375
6376/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * Append "n" to list "l".
6378 * Returns FAIL when out of memory.
6379 */
6380 static int
6381list_append_number(l, n)
6382 list_T *l;
6383 varnumber_T n;
6384{
6385 listitem_T *li;
6386
6387 li = listitem_alloc();
6388 if (li == NULL)
6389 return FAIL;
6390 li->li_tv.v_type = VAR_NUMBER;
6391 li->li_tv.v_lock = 0;
6392 li->li_tv.vval.v_number = n;
6393 list_append(l, li);
6394 return OK;
6395}
6396
6397/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399 * If "item" is NULL append at the end.
6400 * Return FAIL when out of memory.
6401 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006402 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 list_T *l;
6405 typval_T *tv;
6406 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407{
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409
6410 if (ni == NULL)
6411 return FAIL;
6412 copy_tv(tv, &ni->li_tv);
6413 if (item == NULL)
6414 /* Append new item at end of list. */
6415 list_append(l, ni);
6416 else
6417 {
6418 /* Insert new item before existing item. */
6419 ni->li_prev = item->li_prev;
6420 ni->li_next = item;
6421 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006422 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006424 ++l->lv_idx;
6425 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006429 l->lv_idx_item = NULL;
6430 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 }
6434 return OK;
6435}
6436
6437/*
6438 * Extend "l1" with "l2".
6439 * If "bef" is NULL append at the end, otherwise insert before this item.
6440 * Returns FAIL when out of memory.
6441 */
6442 static int
6443list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 list_T *l1;
6445 list_T *l2;
6446 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447{
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006449 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006451 /* We also quit the loop when we have inserted the original item count of
6452 * the list, avoid a hang when we extend a list with itself. */
6453 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6455 return FAIL;
6456 return OK;
6457}
6458
6459/*
6460 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6461 * Return FAIL when out of memory.
6462 */
6463 static int
6464list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l1;
6466 list_T *l2;
6467 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468{
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006471 if (l1 == NULL || l2 == NULL)
6472 return FAIL;
6473
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006475 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476 if (l == NULL)
6477 return FAIL;
6478 tv->v_type = VAR_LIST;
6479 tv->vval.v_list = l;
6480
6481 /* append all items from the second list */
6482 return list_extend(l, l2, NULL);
6483}
6484
6485/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489 * Returns NULL when out of memory.
6490 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 list_T *copy;
6498 listitem_T *item;
6499 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500
6501 if (orig == NULL)
6502 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503
6504 copy = list_alloc();
6505 if (copy != NULL)
6506 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 if (copyID != 0)
6508 {
6509 /* Do this before adding the items, because one of the items may
6510 * refer back to this list. */
6511 orig->lv_copyID = copyID;
6512 orig->lv_copylist = copy;
6513 }
6514 for (item = orig->lv_first; item != NULL && !got_int;
6515 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 {
6517 ni = listitem_alloc();
6518 if (ni == NULL)
6519 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006521 {
6522 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6523 {
6524 vim_free(ni);
6525 break;
6526 }
6527 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006529 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 list_append(copy, ni);
6531 }
6532 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533 if (item != NULL)
6534 {
6535 list_unref(copy);
6536 copy = NULL;
6537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 }
6539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 return copy;
6541}
6542
6543/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006545 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006547 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006548list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 list_T *l;
6550 listitem_T *item;
6551 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552{
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 /* notify watchers */
6556 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006558 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 list_fix_watch(l, ip);
6560 if (ip == item2)
6561 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
6564 if (item2->li_next == NULL)
6565 l->lv_last = item->li_prev;
6566 else
6567 item2->li_next->li_prev = item->li_prev;
6568 if (item->li_prev == NULL)
6569 l->lv_first = item2->li_next;
6570 else
6571 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006572 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573}
6574
6575/*
6576 * Return an allocated string with the string representation of a list.
6577 * May return NULL.
6578 */
6579 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006580list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006582 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583{
6584 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 if (tv->vval.v_list == NULL)
6587 return NULL;
6588 ga_init2(&ga, (int)sizeof(char), 80);
6589 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 {
6592 vim_free(ga.ga_data);
6593 return NULL;
6594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 ga_append(&ga, ']');
6596 ga_append(&ga, NUL);
6597 return (char_u *)ga.ga_data;
6598}
6599
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006600typedef struct join_S {
6601 char_u *s;
6602 char_u *tofree;
6603} join_T;
6604
6605 static int
6606list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6607 garray_T *gap; /* to store the result in */
6608 list_T *l;
6609 char_u *sep;
6610 int echo_style;
6611 int copyID;
6612 garray_T *join_gap; /* to keep each list item string */
6613{
6614 int i;
6615 join_T *p;
6616 int len;
6617 int sumlen = 0;
6618 int first = TRUE;
6619 char_u *tofree;
6620 char_u numbuf[NUMBUFLEN];
6621 listitem_T *item;
6622 char_u *s;
6623
6624 /* Stringify each item in the list. */
6625 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6626 {
6627 if (echo_style)
6628 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6629 else
6630 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6631 if (s == NULL)
6632 return FAIL;
6633
6634 len = (int)STRLEN(s);
6635 sumlen += len;
6636
6637 ga_grow(join_gap, 1);
6638 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6639 if (tofree != NULL || s != numbuf)
6640 {
6641 p->s = s;
6642 p->tofree = tofree;
6643 }
6644 else
6645 {
6646 p->s = vim_strnsave(s, len);
6647 p->tofree = p->s;
6648 }
6649
6650 line_breakcheck();
6651 }
6652
6653 /* Allocate result buffer with its total size, avoid re-allocation and
6654 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6655 if (join_gap->ga_len >= 2)
6656 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6657 if (ga_grow(gap, sumlen + 2) == FAIL)
6658 return FAIL;
6659
6660 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6661 {
6662 if (first)
6663 first = FALSE;
6664 else
6665 ga_concat(gap, sep);
6666 p = ((join_T *)join_gap->ga_data) + i;
6667
6668 if (p->s != NULL)
6669 ga_concat(gap, p->s);
6670 line_breakcheck();
6671 }
6672
6673 return OK;
6674}
6675
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006686 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006689 garray_T join_ga;
6690 int retval;
6691 join_T *p;
6692 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6695 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6696
6697 /* Dispose each item in join_ga. */
6698 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700 p = (join_T *)join_ga.ga_data;
6701 for (i = 0; i < join_ga.ga_len; ++i)
6702 {
6703 vim_free(p->tofree);
6704 ++p;
6705 }
6706 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006708
6709 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710}
6711
6712/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006713 * Garbage collection for lists and dictionaries.
6714 *
6715 * We use reference counts to be able to free most items right away when they
6716 * are no longer used. But for composite items it's possible that it becomes
6717 * unused while the reference count is > 0: When there is a recursive
6718 * reference. Example:
6719 * :let l = [1, 2, 3]
6720 * :let d = {9: l}
6721 * :let l[1] = d
6722 *
6723 * Since this is quite unusual we handle this with garbage collection: every
6724 * once in a while find out which lists and dicts are not referenced from any
6725 * variable.
6726 *
6727 * Here is a good reference text about garbage collection (refers to Python
6728 * but it applies to all reference-counting mechanisms):
6729 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006730 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731
6732/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 * Do garbage collection for lists and dicts.
6734 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 int
6737garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006739 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 buf_T *buf;
6741 win_T *wp;
6742 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006743 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006744 int did_free;
6745 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006746#ifdef FEAT_WINDOWS
6747 tabpage_T *tp;
6748#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006750 /* Only do this once. */
6751 want_garbage_collect = FALSE;
6752 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006753 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006754
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006755 /* We advance by two because we add one for items referenced through
6756 * previous_funccal. */
6757 current_copyID += COPYID_INC;
6758 copyID = current_copyID;
6759
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 /*
6761 * 1. Go through all accessible variables and mark all lists and dicts
6762 * with copyID.
6763 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764
6765 /* Don't free variables in the previous_funccal list unless they are only
6766 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006767 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6769 {
6770 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6771 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6772 }
6773
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 /* script-local variables */
6775 for (i = 1; i <= ga_scripts.ga_len; ++i)
6776 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6777
6778 /* buffer-local variables */
6779 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6780 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6781
6782 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006783 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6785
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786#ifdef FEAT_WINDOWS
6787 /* tabpage-local variables */
6788 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6789 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6790#endif
6791
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 /* global variables */
6793 set_ref_in_ht(&globvarht, copyID);
6794
6795 /* function-local variables */
6796 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6797 {
6798 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6799 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6800 }
6801
Bram Moolenaard812df62008-11-09 12:46:09 +00006802 /* v: vars */
6803 set_ref_in_ht(&vimvarht, copyID);
6804
Bram Moolenaar1dced572012-04-05 16:54:08 +02006805#ifdef FEAT_LUA
6806 set_ref_in_lua(copyID);
6807#endif
6808
Bram Moolenaardb913952012-06-29 12:54:53 +02006809#ifdef FEAT_PYTHON
6810 set_ref_in_python(copyID);
6811#endif
6812
6813#ifdef FEAT_PYTHON3
6814 set_ref_in_python3(copyID);
6815#endif
6816
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006817 /*
6818 * 2. Free lists and dictionaries that are not referenced.
6819 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006820 did_free = free_unref_items(copyID);
6821
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006822 /*
6823 * 3. Check if any funccal can be freed now.
6824 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006825 for (pfc = &previous_funccal; *pfc != NULL; )
6826 {
6827 if (can_free_funccal(*pfc, copyID))
6828 {
6829 fc = *pfc;
6830 *pfc = fc->caller;
6831 free_funccal(fc, TRUE);
6832 did_free = TRUE;
6833 did_free_funccal = TRUE;
6834 }
6835 else
6836 pfc = &(*pfc)->caller;
6837 }
6838 if (did_free_funccal)
6839 /* When a funccal was freed some more items might be garbage
6840 * collected, so run again. */
6841 (void)garbage_collect();
6842
6843 return did_free;
6844}
6845
6846/*
6847 * Free lists and dictionaries that are no longer referenced.
6848 */
6849 static int
6850free_unref_items(copyID)
6851 int copyID;
6852{
6853 dict_T *dd;
6854 list_T *ll;
6855 int did_free = FALSE;
6856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 */
6860 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006863 /* Free the Dictionary and ordinary items it contains, but don't
6864 * recurse into Lists and Dictionaries, they will be in the list
6865 * of dicts or list of lists. */
6866 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 did_free = TRUE;
6868
6869 /* restart, next dict may also have been freed */
6870 dd = first_dict;
6871 }
6872 else
6873 dd = dd->dv_used_next;
6874
6875 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 * Go through the list of lists and free items without the copyID.
6877 * But don't free a list that has a watcher (used in a for loop), these
6878 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 */
6880 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6882 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006884 /* Free the List and ordinary items it contains, but don't recurse
6885 * into Lists and Dictionaries, they will be in the list of dicts
6886 * or list of lists. */
6887 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888 did_free = TRUE;
6889
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006890 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 ll = first_list;
6892 }
6893 else
6894 ll = ll->lv_used_next;
6895
6896 return did_free;
6897}
6898
6899/*
6900 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6901 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006902 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903set_ref_in_ht(ht, copyID)
6904 hashtab_T *ht;
6905 int copyID;
6906{
6907 int todo;
6908 hashitem_T *hi;
6909
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006910 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 for (hi = ht->ht_array; todo > 0; ++hi)
6912 if (!HASHITEM_EMPTY(hi))
6913 {
6914 --todo;
6915 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6916 }
6917}
6918
6919/*
6920 * Mark all lists and dicts referenced through list "l" with "copyID".
6921 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006922 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923set_ref_in_list(l, copyID)
6924 list_T *l;
6925 int copyID;
6926{
6927 listitem_T *li;
6928
6929 for (li = l->lv_first; li != NULL; li = li->li_next)
6930 set_ref_in_item(&li->li_tv, copyID);
6931}
6932
6933/*
6934 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6935 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006936 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937set_ref_in_item(tv, copyID)
6938 typval_T *tv;
6939 int copyID;
6940{
6941 dict_T *dd;
6942 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006943
6944 switch (tv->v_type)
6945 {
6946 case VAR_DICT:
6947 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006948 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 /* Didn't see this dict yet. */
6951 dd->dv_copyID = copyID;
6952 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955
6956 case VAR_LIST:
6957 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006958 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /* Didn't see this list yet. */
6961 ll->lv_copyID = copyID;
6962 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967}
6968
6969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 * Allocate an empty header for a dictionary.
6971 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006973dict_alloc()
6974{
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006976
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006978 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006979 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006980 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 if (first_dict != NULL)
6982 first_dict->dv_used_prev = d;
6983 d->dv_used_next = first_dict;
6984 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006985 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006989 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006991 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994}
6995
6996/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006997 * Allocate an empty dict for a return value.
6998 * Returns OK or FAIL.
6999 */
7000 static int
7001rettv_dict_alloc(rettv)
7002 typval_T *rettv;
7003{
7004 dict_T *d = dict_alloc();
7005
7006 if (d == NULL)
7007 return FAIL;
7008
7009 rettv->vval.v_dict = d;
7010 rettv->v_type = VAR_DICT;
7011 ++d->dv_refcount;
7012 return OK;
7013}
7014
7015
7016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017 * Unreference a Dictionary: decrement the reference count and free it when it
7018 * becomes zero.
7019 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007020 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007024 if (d != NULL && --d->dv_refcount <= 0)
7025 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026}
7027
7028/*
7029 * Free a Dictionary, including all items it contains.
7030 * Ignores the reference count.
7031 */
7032 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007033dict_free(d, recurse)
7034 dict_T *d;
7035 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007039 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 /* Remove the dict from the list of dicts for garbage collection. */
7042 if (d->dv_used_prev == NULL)
7043 first_dict = d->dv_used_next;
7044 else
7045 d->dv_used_prev->dv_used_next = d->dv_used_next;
7046 if (d->dv_used_next != NULL)
7047 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7048
7049 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007051 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 if (!HASHITEM_EMPTY(hi))
7055 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007056 /* Remove the item before deleting it, just in case there is
7057 * something recursive causing trouble. */
7058 di = HI2DI(hi);
7059 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007060 if (recurse || (di->di_tv.v_type != VAR_LIST
7061 && di->di_tv.v_type != VAR_DICT))
7062 clear_tv(&di->di_tv);
7063 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064 --todo;
7065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068 vim_free(d);
7069}
7070
7071/*
7072 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 * The "key" is copied to the new item.
7074 * Note that the value of the item "di_tv" still needs to be initialized!
7075 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007077 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078dictitem_alloc(key)
7079 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080{
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007083 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007087 di->di_flags = 0;
7088 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090}
7091
7092/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 * Make a copy of a Dictionary item.
7094 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098{
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007101 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7102 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 if (di != NULL)
7104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007106 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 copy_tv(&org->di_tv, &di->di_tv);
7108 }
7109 return di;
7110}
7111
7112/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 * Remove item "item" from Dictionary "dict" and free it.
7114 */
7115 static void
7116dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 dict_T *dict;
7118 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119{
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 if (HASHITEM_EMPTY(hi))
7124 EMSG2(_(e_intern2), "dictitem_remove()");
7125 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 dictitem_free(item);
7128}
7129
7130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 * Free a dict item. Also clears the value.
7132 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007133 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 clear_tv(&item->di_tv);
7138 vim_free(item);
7139}
7140
7141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7143 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 * Returns NULL when out of memory.
7146 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007148dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152{
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 dict_T *copy;
7154 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157
7158 if (orig == NULL)
7159 return NULL;
7160
7161 copy = dict_alloc();
7162 if (copy != NULL)
7163 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007164 if (copyID != 0)
7165 {
7166 orig->dv_copyID = copyID;
7167 orig->dv_copydict = copy;
7168 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007169 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 --todo;
7175
7176 di = dictitem_alloc(hi->hi_key);
7177 if (di == NULL)
7178 break;
7179 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 {
7181 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7182 copyID) == FAIL)
7183 {
7184 vim_free(di);
7185 break;
7186 }
7187 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 else
7189 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7190 if (dict_add(copy, di) == FAIL)
7191 {
7192 dictitem_free(di);
7193 break;
7194 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 if (todo > 0)
7200 {
7201 dict_unref(copy);
7202 copy = NULL;
7203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 }
7205
7206 return copy;
7207}
7208
7209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007211 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007213 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 dict_T *d;
7216 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217{
Bram Moolenaar33570922005-01-25 22:26:29 +00007218 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219}
7220
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007222 * Add a number or string entry to dictionary "d".
7223 * When "str" is NULL use number "nr", otherwise use "str".
7224 * Returns FAIL when out of memory and when key already exists.
7225 */
7226 int
7227dict_add_nr_str(d, key, nr, str)
7228 dict_T *d;
7229 char *key;
7230 long nr;
7231 char_u *str;
7232{
7233 dictitem_T *item;
7234
7235 item = dictitem_alloc((char_u *)key);
7236 if (item == NULL)
7237 return FAIL;
7238 item->di_tv.v_lock = 0;
7239 if (str == NULL)
7240 {
7241 item->di_tv.v_type = VAR_NUMBER;
7242 item->di_tv.vval.v_number = nr;
7243 }
7244 else
7245 {
7246 item->di_tv.v_type = VAR_STRING;
7247 item->di_tv.vval.v_string = vim_strsave(str);
7248 }
7249 if (dict_add(d, item) == FAIL)
7250 {
7251 dictitem_free(item);
7252 return FAIL;
7253 }
7254 return OK;
7255}
7256
7257/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007258 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007259 * Returns FAIL when out of memory and when key already exists.
7260 */
7261 int
7262dict_add_list(d, key, list)
7263 dict_T *d;
7264 char *key;
7265 list_T *list;
7266{
7267 dictitem_T *item;
7268
7269 item = dictitem_alloc((char_u *)key);
7270 if (item == NULL)
7271 return FAIL;
7272 item->di_tv.v_lock = 0;
7273 item->di_tv.v_type = VAR_LIST;
7274 item->di_tv.vval.v_list = list;
7275 if (dict_add(d, item) == FAIL)
7276 {
7277 dictitem_free(item);
7278 return FAIL;
7279 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007280 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007281 return OK;
7282}
7283
7284/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285 * Get the number of items in a Dictionary.
7286 */
7287 static long
7288dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 if (d == NULL)
7292 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007293 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294}
7295
7296/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 * Find item "key[len]" in Dictionary "d".
7298 * If "len" is negative use strlen(key).
7299 * Returns NULL when not found.
7300 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007301 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 char_u *key;
7305 int len;
7306{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307#define AKEYLEN 200
7308 char_u buf[AKEYLEN];
7309 char_u *akey;
7310 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 if (len < 0)
7314 akey = key;
7315 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007316 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 tofree = akey = vim_strnsave(key, len);
7318 if (akey == NULL)
7319 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007320 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 else
7322 {
7323 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007324 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 akey = buf;
7326 }
7327
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 vim_free(tofree);
7330 if (HASHITEM_EMPTY(hi))
7331 return NULL;
7332 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333}
7334
7335/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007336 * Get a string item from a dictionary.
7337 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338 * Returns NULL if the entry doesn't exist or out of memory.
7339 */
7340 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007341get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007342 dict_T *d;
7343 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007344 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345{
7346 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007347 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007348
7349 di = dict_find(d, key, -1);
7350 if (di == NULL)
7351 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007352 s = get_tv_string(&di->di_tv);
7353 if (save && s != NULL)
7354 s = vim_strsave(s);
7355 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007356}
7357
7358/*
7359 * Get a number item from a dictionary.
7360 * Returns 0 if the entry doesn't exist or out of memory.
7361 */
7362 long
7363get_dict_number(d, key)
7364 dict_T *d;
7365 char_u *key;
7366{
7367 dictitem_T *di;
7368
7369 di = dict_find(d, key, -1);
7370 if (di == NULL)
7371 return 0;
7372 return get_tv_number(&di->di_tv);
7373}
7374
7375/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 * Return an allocated string with the string representation of a Dictionary.
7377 * May return NULL.
7378 */
7379 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007382 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383{
7384 garray_T ga;
7385 int first = TRUE;
7386 char_u *tofree;
7387 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 return NULL;
7395 ga_init2(&ga, (int)sizeof(char), 80);
7396 ga_append(&ga, '{');
7397
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007398 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 --todo;
7404
7405 if (first)
7406 first = FALSE;
7407 else
7408 ga_concat(&ga, (char_u *)", ");
7409
7410 tofree = string_quote(hi->hi_key, FALSE);
7411 if (tofree != NULL)
7412 {
7413 ga_concat(&ga, tofree);
7414 vim_free(tofree);
7415 }
7416 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418 if (s != NULL)
7419 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 if (s == NULL)
7422 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (todo > 0)
7426 {
7427 vim_free(ga.ga_data);
7428 return NULL;
7429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
7431 ga_append(&ga, '}');
7432 ga_append(&ga, NUL);
7433 return (char_u *)ga.ga_data;
7434}
7435
7436/*
7437 * Allocate a variable for a Dictionary and fill it from "*arg".
7438 * Return OK or FAIL. Returns NOTDONE for {expr}.
7439 */
7440 static int
7441get_dict_tv(arg, rettv, evaluate)
7442 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 int evaluate;
7445{
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dict_T *d = NULL;
7447 typval_T tvkey;
7448 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007449 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453
7454 /*
7455 * First check if it's not a curly-braces thing: {expr}.
7456 * Must do this without evaluating, otherwise a function may be called
7457 * twice. Unfortunately this means we need to call eval1() twice for the
7458 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 if (*start != '}')
7462 {
7463 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7464 return FAIL;
7465 if (*start == '}')
7466 return NOTDONE;
7467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468
7469 if (evaluate)
7470 {
7471 d = dict_alloc();
7472 if (d == NULL)
7473 return FAIL;
7474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007475 tvkey.v_type = VAR_UNKNOWN;
7476 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477
7478 *arg = skipwhite(*arg + 1);
7479 while (**arg != '}' && **arg != NUL)
7480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 goto failret;
7483 if (**arg != ':')
7484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007485 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 goto failret;
7488 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007489 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007491 key = get_tv_string_buf_chk(&tvkey, buf);
7492 if (key == NULL || *key == NUL)
7493 {
7494 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7495 if (key != NULL)
7496 EMSG(_(e_emptykey));
7497 clear_tv(&tvkey);
7498 goto failret;
7499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
7502 *arg = skipwhite(*arg + 1);
7503 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7504 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007505 if (evaluate)
7506 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 goto failret;
7508 }
7509 if (evaluate)
7510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 if (item != NULL)
7513 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007514 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 clear_tv(&tv);
7517 goto failret;
7518 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 item = dictitem_alloc(key);
7520 clear_tv(&tvkey);
7521 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007524 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 if (dict_add(d, item) == FAIL)
7526 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 }
7528 }
7529
7530 if (**arg == '}')
7531 break;
7532 if (**arg != ',')
7533 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007534 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 *arg = skipwhite(*arg + 1);
7538 }
7539
7540 if (**arg != '}')
7541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543failret:
7544 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007545 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 return FAIL;
7547 }
7548
7549 *arg = skipwhite(*arg + 1);
7550 if (evaluate)
7551 {
7552 rettv->v_type = VAR_DICT;
7553 rettv->vval.v_dict = d;
7554 ++d->dv_refcount;
7555 }
7556
7557 return OK;
7558}
7559
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007561 * Return a string with the string representation of a variable.
7562 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007563 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007566 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 */
7568 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007572 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 static int recurse = 0;
7576 char_u *r = NULL;
7577
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007580 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007581 *tofree = NULL;
7582 return NULL;
7583 }
7584 ++recurse;
7585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007586 switch (tv->v_type)
7587 {
7588 case VAR_FUNC:
7589 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007590 r = tv->vval.v_string;
7591 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007594 if (tv->vval.v_list == NULL)
7595 {
7596 *tofree = NULL;
7597 r = NULL;
7598 }
7599 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7600 {
7601 *tofree = NULL;
7602 r = (char_u *)"[...]";
7603 }
7604 else
7605 {
7606 tv->vval.v_list->lv_copyID = copyID;
7607 *tofree = list2string(tv, copyID);
7608 r = *tofree;
7609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 if (tv->vval.v_dict == NULL)
7614 {
7615 *tofree = NULL;
7616 r = NULL;
7617 }
7618 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7619 {
7620 *tofree = NULL;
7621 r = (char_u *)"{...}";
7622 }
7623 else
7624 {
7625 tv->vval.v_dict->dv_copyID = copyID;
7626 *tofree = dict2string(tv, copyID);
7627 r = *tofree;
7628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007631 case VAR_STRING:
7632 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 *tofree = NULL;
7634 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007637#ifdef FEAT_FLOAT
7638 case VAR_FLOAT:
7639 *tofree = NULL;
7640 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7641 r = numbuf;
7642 break;
7643#endif
7644
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649
7650 --recurse;
7651 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007652}
7653
7654/*
7655 * Return a string with the string representation of a variable.
7656 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7657 * "numbuf" is used for a number.
7658 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007659 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 */
7661 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664 char_u **tofree;
7665 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007666 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667{
7668 switch (tv->v_type)
7669 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 case VAR_FUNC:
7671 *tofree = string_quote(tv->vval.v_string, TRUE);
7672 return *tofree;
7673 case VAR_STRING:
7674 *tofree = string_quote(tv->vval.v_string, FALSE);
7675 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007676#ifdef FEAT_FLOAT
7677 case VAR_FLOAT:
7678 *tofree = NULL;
7679 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7680 return numbuf;
7681#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007689 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690}
7691
7692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 * Return string "str" in ' quotes, doubling ' characters.
7694 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 */
7697 static char_u *
7698string_quote(str, function)
7699 char_u *str;
7700 int function;
7701{
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 char_u *p, *r, *s;
7704
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 len = (function ? 13 : 3);
7706 if (str != NULL)
7707 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007708 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 for (p = str; *p != NUL; mb_ptr_adv(p))
7710 if (*p == '\'')
7711 ++len;
7712 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 s = r = alloc(len);
7714 if (r != NULL)
7715 {
7716 if (function)
7717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 r += 10;
7720 }
7721 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 if (str != NULL)
7724 for (p = str; *p != NUL; )
7725 {
7726 if (*p == '\'')
7727 *r++ = '\'';
7728 MB_COPY_CHAR(p, r);
7729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 if (function)
7732 *r++ = ')';
7733 *r++ = NUL;
7734 }
7735 return s;
7736}
7737
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739/*
7740 * Convert the string "text" to a floating point number.
7741 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7742 * this always uses a decimal point.
7743 * Returns the length of the text that was consumed.
7744 */
7745 static int
7746string2float(text, value)
7747 char_u *text;
7748 float_T *value; /* result stored here */
7749{
7750 char *s = (char *)text;
7751 float_T f;
7752
7753 f = strtod(s, &s);
7754 *value = f;
7755 return (int)((char_u *)s - text);
7756}
7757#endif
7758
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 * Get the value of an environment variable.
7761 * "arg" is pointing to the '$'. It is advanced to after the name.
7762 * If the environment variable was not set, silently assume it is empty.
7763 * Always return OK.
7764 */
7765 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 int evaluate;
7770{
7771 char_u *string = NULL;
7772 int len;
7773 int cc;
7774 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007775 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 ++*arg;
7778 name = *arg;
7779 len = get_env_len(arg);
7780 if (evaluate)
7781 {
7782 if (len != 0)
7783 {
7784 cc = name[len];
7785 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007786 /* first try vim_getenv(), fast for normal environment vars */
7787 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 {
7790 if (!mustfree)
7791 string = vim_strsave(string);
7792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 else
7794 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007795 if (mustfree)
7796 vim_free(string);
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 /* next try expanding things like $VIM and ${HOME} */
7799 string = expand_env_save(name - 1);
7800 if (string != NULL && *string == '$')
7801 {
7802 vim_free(string);
7803 string = NULL;
7804 }
7805 }
7806 name[len] = cc;
7807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->v_type = VAR_STRING;
7809 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811
7812 return OK;
7813}
7814
7815/*
7816 * Array with names and number of arguments of all internal functions
7817 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7818 */
7819static struct fst
7820{
7821 char *f_name; /* function name */
7822 char f_min_argc; /* minimal number of arguments */
7823 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007825 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826} functions[] =
7827{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007828#ifdef FEAT_FLOAT
7829 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007830 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007831#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007832 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007833 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"append", 2, 2, f_append},
7835 {"argc", 0, 0, f_argc},
7836 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007837 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007839 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007841 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007844 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"bufexists", 1, 1, f_bufexists},
7846 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7847 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7848 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7849 {"buflisted", 1, 1, f_buflisted},
7850 {"bufloaded", 1, 1, f_bufloaded},
7851 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007852 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"bufwinnr", 1, 1, f_bufwinnr},
7854 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007855 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007857#ifdef FEAT_FLOAT
7858 {"ceil", 1, 1, f_ceil},
7859#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007860 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007861 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007863 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007865#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007866 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007867 {"complete_add", 1, 1, f_complete_add},
7868 {"complete_check", 0, 0, f_complete_check},
7869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
7873 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007874 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007878 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007879 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"delete", 1, 1, f_delete},
7881 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007882 {"diff_filler", 1, 1, f_diff_filler},
7883 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007884 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"eventhandler", 0, 0, f_eventhandler},
7888 {"executable", 1, 1, f_executable},
7889 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007890#ifdef FEAT_FLOAT
7891 {"exp", 1, 1, f_exp},
7892#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007893 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007894 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007895 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7897 {"filereadable", 1, 1, f_filereadable},
7898 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007900 {"finddir", 1, 3, f_finddir},
7901 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#ifdef FEAT_FLOAT
7903 {"float2nr", 1, 1, f_float2nr},
7904 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007905 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007907 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"fnamemodify", 2, 2, f_fnamemodify},
7909 {"foldclosed", 1, 1, f_foldclosed},
7910 {"foldclosedend", 1, 1, f_foldclosedend},
7911 {"foldlevel", 1, 1, f_foldlevel},
7912 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007913 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007916 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007917 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007918 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007919 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"getchar", 0, 1, f_getchar},
7921 {"getcharmod", 0, 0, f_getcharmod},
7922 {"getcmdline", 0, 0, f_getcmdline},
7923 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007924 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007926 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007927 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"getfsize", 1, 1, f_getfsize},
7929 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007930 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007932 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007933 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007934 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007935 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007936 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007937 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007939 {"gettabvar", 2, 3, f_gettabvar},
7940 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"getwinposx", 0, 0, f_getwinposx},
7942 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007943 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007944 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007945 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007948 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007949 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7951 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7952 {"histadd", 2, 2, f_histadd},
7953 {"histdel", 1, 2, f_histdel},
7954 {"histget", 1, 2, f_histget},
7955 {"histnr", 1, 1, f_histnr},
7956 {"hlID", 1, 1, f_hlID},
7957 {"hlexists", 1, 1, f_hlexists},
7958 {"hostname", 0, 0, f_hostname},
7959 {"iconv", 3, 3, f_iconv},
7960 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007962 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007964 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"inputrestore", 0, 0, f_inputrestore},
7966 {"inputsave", 0, 0, f_inputsave},
7967 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007968 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007969 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007971 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"libcall", 3, 3, f_libcall},
7978 {"libcallnr", 3, 3, f_libcallnr},
7979 {"line", 1, 1, f_line},
7980 {"line2byte", 1, 1, f_line2byte},
7981 {"lispindent", 1, 1, f_lispindent},
7982 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007984 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985 {"log10", 1, 1, f_log10},
7986#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007987#ifdef FEAT_LUA
7988 {"luaeval", 1, 2, f_luaeval},
7989#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007991 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007992 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007995 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007996 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007997 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007998 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007999 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008000 {"max", 1, 1, f_max},
8001 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008002#ifdef vim_mkdir
8003 {"mkdir", 1, 3, f_mkdir},
8004#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008005 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008006#ifdef FEAT_MZSCHEME
8007 {"mzeval", 1, 1, f_mzeval},
8008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008010 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008011 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008012 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013#ifdef FEAT_FLOAT
8014 {"pow", 2, 2, f_pow},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008017 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008018 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008019#ifdef FEAT_PYTHON3
8020 {"py3eval", 1, 1, f_py3eval},
8021#endif
8022#ifdef FEAT_PYTHON
8023 {"pyeval", 1, 1, f_pyeval},
8024#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008026 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008027 {"reltime", 0, 2, f_reltime},
8028 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"remote_expr", 2, 3, f_remote_expr},
8030 {"remote_foreground", 1, 1, f_remote_foreground},
8031 {"remote_peek", 1, 2, f_remote_peek},
8032 {"remote_read", 1, 1, f_remote_read},
8033 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008036 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008038 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039#ifdef FEAT_FLOAT
8040 {"round", 1, 1, f_round},
8041#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008042 {"screencol", 0, 0, f_screencol},
8043 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008044 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008045 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008046 {"searchpair", 3, 7, f_searchpair},
8047 {"searchpairpos", 3, 7, f_searchpairpos},
8048 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"server2client", 2, 2, f_server2client},
8050 {"serverlist", 0, 0, f_serverlist},
8051 {"setbufvar", 3, 3, f_setbufvar},
8052 {"setcmdpos", 1, 1, f_setcmdpos},
8053 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008054 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008055 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008056 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008057 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008059 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008060 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008062#ifdef FEAT_CRYPT
8063 {"sha256", 1, 1, f_sha256},
8064#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008065 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008066 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
8069 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008070 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008072 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008073 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008074 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008075 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008076 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"sqrt", 1, 1, f_sqrt},
8079 {"str2float", 1, 1, f_str2float},
8080#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008081 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008082 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008083 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084#ifdef HAVE_STRFTIME
8085 {"strftime", 1, 2, f_strftime},
8086#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008087 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"strlen", 1, 1, f_strlen},
8090 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008091 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008093 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"submatch", 1, 1, f_submatch},
8095 {"substitute", 4, 4, f_substitute},
8096 {"synID", 3, 3, f_synID},
8097 {"synIDattr", 2, 3, f_synIDattr},
8098 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008099 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008100 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008101 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008102 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008103 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008104 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008105 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008106 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107#ifdef FEAT_FLOAT
8108 {"tan", 1, 1, f_tan},
8109 {"tanh", 1, 1, f_tanh},
8110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008112 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"tolower", 1, 1, f_tolower},
8114 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008115 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"trunc", 1, 1, f_trunc},
8118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008120 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008121 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008122 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"virtcol", 1, 1, f_virtcol},
8124 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008125 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"winbufnr", 1, 1, f_winbufnr},
8127 {"wincol", 0, 0, f_wincol},
8128 {"winheight", 1, 1, f_winheight},
8129 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008130 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008132 {"winrestview", 1, 1, f_winrestview},
8133 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008135 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008136 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137};
8138
8139#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8140
8141/*
8142 * Function given to ExpandGeneric() to obtain the list of internal
8143 * or user defined function names.
8144 */
8145 char_u *
8146get_function_name(xp, idx)
8147 expand_T *xp;
8148 int idx;
8149{
8150 static int intidx = -1;
8151 char_u *name;
8152
8153 if (idx == 0)
8154 intidx = -1;
8155 if (intidx < 0)
8156 {
8157 name = get_user_func_name(xp, idx);
8158 if (name != NULL)
8159 return name;
8160 }
8161 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8162 {
8163 STRCPY(IObuff, functions[intidx].f_name);
8164 STRCAT(IObuff, "(");
8165 if (functions[intidx].f_max_argc == 0)
8166 STRCAT(IObuff, ")");
8167 return IObuff;
8168 }
8169
8170 return NULL;
8171}
8172
8173/*
8174 * Function given to ExpandGeneric() to obtain the list of internal or
8175 * user defined variable or function names.
8176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 char_u *
8178get_expr_name(xp, idx)
8179 expand_T *xp;
8180 int idx;
8181{
8182 static int intidx = -1;
8183 char_u *name;
8184
8185 if (idx == 0)
8186 intidx = -1;
8187 if (intidx < 0)
8188 {
8189 name = get_function_name(xp, idx);
8190 if (name != NULL)
8191 return name;
8192 }
8193 return get_user_var_name(xp, ++intidx);
8194}
8195
8196#endif /* FEAT_CMDL_COMPL */
8197
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008198#if defined(EBCDIC) || defined(PROTO)
8199/*
8200 * Compare struct fst by function name.
8201 */
8202 static int
8203compare_func_name(s1, s2)
8204 const void *s1;
8205 const void *s2;
8206{
8207 struct fst *p1 = (struct fst *)s1;
8208 struct fst *p2 = (struct fst *)s2;
8209
8210 return STRCMP(p1->f_name, p2->f_name);
8211}
8212
8213/*
8214 * Sort the function table by function name.
8215 * The sorting of the table above is ASCII dependant.
8216 * On machines using EBCDIC we have to sort it.
8217 */
8218 static void
8219sortFunctions()
8220{
8221 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8222
8223 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8224}
8225#endif
8226
8227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228/*
8229 * Find internal function in table above.
8230 * Return index, or -1 if not found
8231 */
8232 static int
8233find_internal_func(name)
8234 char_u *name; /* name of the function */
8235{
8236 int first = 0;
8237 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8238 int cmp;
8239 int x;
8240
8241 /*
8242 * Find the function name in the table. Binary search.
8243 */
8244 while (first <= last)
8245 {
8246 x = first + ((unsigned)(last - first) >> 1);
8247 cmp = STRCMP(name, functions[x].f_name);
8248 if (cmp < 0)
8249 last = x - 1;
8250 else if (cmp > 0)
8251 first = x + 1;
8252 else
8253 return x;
8254 }
8255 return -1;
8256}
8257
8258/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8260 * name it contains, otherwise return "name".
8261 */
8262 static char_u *
8263deref_func_name(name, lenp)
8264 char_u *name;
8265 int *lenp;
8266{
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 int cc;
8269
8270 cc = name[*lenp];
8271 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008272 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 {
8278 *lenp = 0;
8279 return (char_u *)""; /* just in case */
8280 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008281 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 }
8284
8285 return name;
8286}
8287
8288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 * Allocate a variable for the result of a function.
8290 * Return OK or FAIL.
8291 */
8292 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008293get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8294 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 char_u *name; /* name of the function */
8296 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 char_u **arg; /* argument, pointing to the '(' */
8299 linenr_T firstline; /* first line of range */
8300 linenr_T lastline; /* last line of range */
8301 int *doesrange; /* return: function handled range */
8302 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008303 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304{
8305 char_u *argp;
8306 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008307 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 int argcount = 0; /* number of arguments found */
8309
8310 /*
8311 * Get the arguments.
8312 */
8313 argp = *arg;
8314 while (argcount < MAX_FUNC_ARGS)
8315 {
8316 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8317 if (*argp == ')' || *argp == ',' || *argp == NUL)
8318 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8320 {
8321 ret = FAIL;
8322 break;
8323 }
8324 ++argcount;
8325 if (*argp != ',')
8326 break;
8327 }
8328 if (*argp == ')')
8329 ++argp;
8330 else
8331 ret = FAIL;
8332
8333 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008334 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008335 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 {
8338 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008339 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008341 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
8344 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
8347 *arg = skipwhite(argp);
8348 return ret;
8349}
8350
8351
8352/*
8353 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008354 * Return OK when the function can't be called, FAIL otherwise.
8355 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 */
8357 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008358call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008359 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008360 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008364 typval_T *argvars; /* vars for arguments, must have "argcount"
8365 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 linenr_T firstline; /* first line of range */
8367 linenr_T lastline; /* last line of range */
8368 int *doesrange; /* return: function handled range */
8369 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008370 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#define ERROR_UNKNOWN 0
8374#define ERROR_TOOMANY 1
8375#define ERROR_TOOFEW 2
8376#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008377#define ERROR_DICT 4
8378#define ERROR_NONE 5
8379#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 int error = ERROR_NONE;
8381 int i;
8382 int llen;
8383 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384#define FLEN_FIXED 40
8385 char_u fname_buf[FLEN_FIXED + 1];
8386 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008387 char_u *name;
8388
8389 /* Make a copy of the name, if it comes from a funcref variable it could
8390 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008391 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008392 if (name == NULL)
8393 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 /*
8396 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8397 * Change <SNR>123_name() to K_SNR 123_name().
8398 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 llen = eval_fname_script(name);
8401 if (llen > 0)
8402 {
8403 fname_buf[0] = K_SPECIAL;
8404 fname_buf[1] = KS_EXTRA;
8405 fname_buf[2] = (int)KE_SNR;
8406 i = 3;
8407 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8408 {
8409 if (current_SID <= 0)
8410 error = ERROR_SCRIPT;
8411 else
8412 {
8413 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8414 i = (int)STRLEN(fname_buf);
8415 }
8416 }
8417 if (i + STRLEN(name + llen) < FLEN_FIXED)
8418 {
8419 STRCPY(fname_buf + i, name + llen);
8420 fname = fname_buf;
8421 }
8422 else
8423 {
8424 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8425 if (fname == NULL)
8426 error = ERROR_OTHER;
8427 else
8428 {
8429 mch_memmove(fname, fname_buf, (size_t)i);
8430 STRCPY(fname + i, name + llen);
8431 }
8432 }
8433 }
8434 else
8435 fname = name;
8436
8437 *doesrange = FALSE;
8438
8439
8440 /* execute the function if no errors detected and executing */
8441 if (evaluate && error == ERROR_NONE)
8442 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008443 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8444 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 error = ERROR_UNKNOWN;
8446
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008447 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {
8449 /*
8450 * User defined function.
8451 */
8452 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008455 /* Trigger FuncUndefined event, may load the function. */
8456 if (fp == NULL
8457 && apply_autocmds(EVENT_FUNCUNDEFINED,
8458 fname, fname, TRUE, NULL)
8459 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008461 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 fp = find_func(fname);
8463 }
8464#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008466 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008467 {
8468 /* loaded a package, search for the function again */
8469 fp = find_func(fname);
8470 }
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (fp != NULL)
8473 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008480 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else
8483 {
8484 /*
8485 * Call the user function.
8486 * Save and restore search patterns, script variables and
8487 * redo buffer.
8488 */
8489 save_search_patterns();
8490 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008491 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008493 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008494 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8495 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8496 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008497 /* Function was unreferenced while being used, free it
8498 * now. */
8499 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 restoreRedobuff();
8501 restore_search_patterns();
8502 error = ERROR_NONE;
8503 }
8504 }
8505 }
8506 else
8507 {
8508 /*
8509 * Find the function name in the table, call its implementation.
8510 */
8511 i = find_internal_func(fname);
8512 if (i >= 0)
8513 {
8514 if (argcount < functions[i].f_min_argc)
8515 error = ERROR_TOOFEW;
8516 else if (argcount > functions[i].f_max_argc)
8517 error = ERROR_TOOMANY;
8518 else
8519 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 error = ERROR_NONE;
8523 }
8524 }
8525 }
8526 /*
8527 * The function call (or "FuncUndefined" autocommand sequence) might
8528 * have been aborted by an error, an interrupt, or an explicitly thrown
8529 * exception that has not been caught so far. This situation can be
8530 * tested for by calling aborting(). For an error in an internal
8531 * function or for the "E132" error in call_user_func(), however, the
8532 * throw point at which the "force_abort" flag (temporarily reset by
8533 * emsg()) is normally updated has not been reached yet. We need to
8534 * update that flag first to make aborting() reliable.
8535 */
8536 update_force_abort();
8537 }
8538 if (error == ERROR_NONE)
8539 ret = OK;
8540
8541 /*
8542 * Report an error unless the argument evaluation or function call has been
8543 * cancelled due to an aborting error, an interrupt, or an exception.
8544 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008545 if (!aborting())
8546 {
8547 switch (error)
8548 {
8549 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008550 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008551 break;
8552 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008553 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 break;
8555 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008556 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008557 name);
8558 break;
8559 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 name);
8562 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008564 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 name);
8566 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 }
8568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 if (fname != name && fname != fname_buf)
8571 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008572 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574 return ret;
8575}
8576
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008577/*
8578 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008579 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008580 */
8581 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008582emsg_funcname(ermsg, name)
8583 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584 char_u *name;
8585{
8586 char_u *p;
8587
8588 if (*name == K_SPECIAL)
8589 p = concat_str((char_u *)"<SNR>", name + 3);
8590 else
8591 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008592 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 if (p != name)
8594 vim_free(p);
8595}
8596
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008597/*
8598 * Return TRUE for a non-zero Number and a non-empty String.
8599 */
8600 static int
8601non_zero_arg(argvars)
8602 typval_T *argvars;
8603{
8604 return ((argvars[0].v_type == VAR_NUMBER
8605 && argvars[0].vval.v_number != 0)
8606 || (argvars[0].v_type == VAR_STRING
8607 && argvars[0].vval.v_string != NULL
8608 && *argvars[0].vval.v_string != NUL));
8609}
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611/*********************************************
8612 * Implementation of the built-in functions
8613 */
8614
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008615#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008616static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8617
8618/*
8619 * Get the float value of "argvars[0]" into "f".
8620 * Returns FAIL when the argument is not a Number or Float.
8621 */
8622 static int
8623get_float_arg(argvars, f)
8624 typval_T *argvars;
8625 float_T *f;
8626{
8627 if (argvars[0].v_type == VAR_FLOAT)
8628 {
8629 *f = argvars[0].vval.v_float;
8630 return OK;
8631 }
8632 if (argvars[0].v_type == VAR_NUMBER)
8633 {
8634 *f = (float_T)argvars[0].vval.v_number;
8635 return OK;
8636 }
8637 EMSG(_("E808: Number or Float required"));
8638 return FAIL;
8639}
8640
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008641/*
8642 * "abs(expr)" function
8643 */
8644 static void
8645f_abs(argvars, rettv)
8646 typval_T *argvars;
8647 typval_T *rettv;
8648{
8649 if (argvars[0].v_type == VAR_FLOAT)
8650 {
8651 rettv->v_type = VAR_FLOAT;
8652 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8653 }
8654 else
8655 {
8656 varnumber_T n;
8657 int error = FALSE;
8658
8659 n = get_tv_number_chk(&argvars[0], &error);
8660 if (error)
8661 rettv->vval.v_number = -1;
8662 else if (n > 0)
8663 rettv->vval.v_number = n;
8664 else
8665 rettv->vval.v_number = -n;
8666 }
8667}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008668
8669/*
8670 * "acos()" function
8671 */
8672 static void
8673f_acos(argvars, rettv)
8674 typval_T *argvars;
8675 typval_T *rettv;
8676{
8677 float_T f;
8678
8679 rettv->v_type = VAR_FLOAT;
8680 if (get_float_arg(argvars, &f) == OK)
8681 rettv->vval.v_float = acos(f);
8682 else
8683 rettv->vval.v_float = 0.0;
8684}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685#endif
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008688 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 */
8690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008691f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
Bram Moolenaar33570922005-01-25 22:26:29 +00008695 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008698 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008700 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008701 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008702 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008704 }
8705 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008706 EMSG(_(e_listreq));
8707}
8708
8709/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008710 * "and(expr, expr)" function
8711 */
8712 static void
8713f_and(argvars, rettv)
8714 typval_T *argvars;
8715 typval_T *rettv;
8716{
8717 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8718 & get_tv_number_chk(&argvars[1], NULL);
8719}
8720
8721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722 * "append(lnum, string/list)" function
8723 */
8724 static void
8725f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728{
8729 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 list_T *l = NULL;
8732 listitem_T *li = NULL;
8733 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 long added = 0;
8735
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736 lnum = get_tv_lnum(argvars);
8737 if (lnum >= 0
8738 && lnum <= curbuf->b_ml.ml_line_count
8739 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008742 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 l = argvars[1].vval.v_list;
8744 if (l == NULL)
8745 return;
8746 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008748 for (;;)
8749 {
8750 if (l == NULL)
8751 tv = &argvars[1]; /* append a string */
8752 else if (li == NULL)
8753 break; /* end of list */
8754 else
8755 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008756 line = get_tv_string_chk(tv);
8757 if (line == NULL) /* type error */
8758 {
8759 rettv->vval.v_number = 1; /* Failed */
8760 break;
8761 }
8762 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008763 ++added;
8764 if (l == NULL)
8765 break;
8766 li = li->li_next;
8767 }
8768
8769 appended_lines_mark(lnum, added);
8770 if (curwin->w_cursor.lnum > lnum)
8771 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 else
8774 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
8777/*
8778 * "argc()" function
8779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008782 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786}
8787
8788/*
8789 * "argidx()" function
8790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008793 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797}
8798
8799/*
8800 * "argv(nr)" function
8801 */
8802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 typval_T *argvars;
8805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 int idx;
8808
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008809 if (argvars[0].v_type != VAR_UNKNOWN)
8810 {
8811 idx = get_tv_number_chk(&argvars[0], NULL);
8812 if (idx >= 0 && idx < ARGCOUNT)
8813 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8814 else
8815 rettv->vval.v_string = NULL;
8816 rettv->v_type = VAR_STRING;
8817 }
8818 else if (rettv_list_alloc(rettv) == OK)
8819 for (idx = 0; idx < ARGCOUNT; ++idx)
8820 list_append_string(rettv->vval.v_list,
8821 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822}
8823
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008824#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008825/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008826 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008827 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008828 static void
8829f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008830 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008833 float_T f;
8834
8835 rettv->v_type = VAR_FLOAT;
8836 if (get_float_arg(argvars, &f) == OK)
8837 rettv->vval.v_float = asin(f);
8838 else
8839 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008840}
8841
8842/*
8843 * "atan()" function
8844 */
8845 static void
8846f_atan(argvars, rettv)
8847 typval_T *argvars;
8848 typval_T *rettv;
8849{
8850 float_T f;
8851
8852 rettv->v_type = VAR_FLOAT;
8853 if (get_float_arg(argvars, &f) == OK)
8854 rettv->vval.v_float = atan(f);
8855 else
8856 rettv->vval.v_float = 0.0;
8857}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008858
8859/*
8860 * "atan2()" function
8861 */
8862 static void
8863f_atan2(argvars, rettv)
8864 typval_T *argvars;
8865 typval_T *rettv;
8866{
8867 float_T fx, fy;
8868
8869 rettv->v_type = VAR_FLOAT;
8870 if (get_float_arg(argvars, &fx) == OK
8871 && get_float_arg(&argvars[1], &fy) == OK)
8872 rettv->vval.v_float = atan2(fx, fy);
8873 else
8874 rettv->vval.v_float = 0.0;
8875}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008876#endif
8877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878/*
8879 * "browse(save, title, initdir, default)" function
8880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886#ifdef FEAT_BROWSE
8887 int save;
8888 char_u *title;
8889 char_u *initdir;
8890 char_u *defname;
8891 char_u buf[NUMBUFLEN];
8892 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 save = get_tv_number_chk(&argvars[0], &error);
8896 title = get_tv_string_chk(&argvars[1]);
8897 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8898 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 if (error || title == NULL || initdir == NULL || defname == NULL)
8901 rettv->vval.v_string = NULL;
8902 else
8903 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008904 do_browse(save ? BROWSE_SAVE : 0,
8905 title, defname, NULL, initdir, NULL, curbuf);
8906#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008910}
8911
8912/*
8913 * "browsedir(title, initdir)" function
8914 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008918 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919{
8920#ifdef FEAT_BROWSE
8921 char_u *title;
8922 char_u *initdir;
8923 char_u buf[NUMBUFLEN];
8924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 title = get_tv_string_chk(&argvars[0]);
8926 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 if (title == NULL || initdir == NULL)
8929 rettv->vval.v_string = NULL;
8930 else
8931 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008932 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937}
8938
Bram Moolenaar33570922005-01-25 22:26:29 +00008939static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941/*
8942 * Find a buffer by number or exact name.
8943 */
8944 static buf_T *
8945find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 if (avar->v_type == VAR_NUMBER)
8951 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008952 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008955 if (buf == NULL)
8956 {
8957 /* No full path name match, try a match with a URL or a "nofile"
8958 * buffer, these don't use the full path. */
8959 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8960 if (buf->b_fname != NULL
8961 && (path_with_url(buf->b_fname)
8962#ifdef FEAT_QUICKFIX
8963 || bt_nofile(buf)
8964#endif
8965 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008966 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008967 break;
8968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
8970 return buf;
8971}
8972
8973/*
8974 * "bufexists(expr)" function
8975 */
8976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984/*
8985 * "buflisted(expr)" function
8986 */
8987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 buf_T *buf;
8993
8994 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
8998/*
8999 * "bufloaded(expr)" function
9000 */
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006 buf_T *buf;
9007
9008 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010}
9011
Bram Moolenaar33570922005-01-25 22:26:29 +00009012static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014/*
9015 * Get buffer by number or pattern.
9016 */
9017 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 int save_magic;
9023 char_u *save_cpo;
9024 buf_T *buf;
9025
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 if (tv->v_type == VAR_NUMBER)
9027 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009028 if (tv->v_type != VAR_STRING)
9029 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 if (name == NULL || *name == NUL)
9031 return curbuf;
9032 if (name[0] == '$' && name[1] == NUL)
9033 return lastbuf;
9034
9035 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9036 save_magic = p_magic;
9037 p_magic = TRUE;
9038 save_cpo = p_cpo;
9039 p_cpo = (char_u *)"";
9040
9041 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9042 TRUE, FALSE));
9043
9044 p_magic = save_magic;
9045 p_cpo = save_cpo;
9046
9047 /* If not found, try expanding the name, like done for bufexists(). */
9048 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 return buf;
9052}
9053
9054/*
9055 * "bufname(expr)" function
9056 */
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 buf_T *buf;
9063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 buf = get_buf_tv(&argvars[0]);
9067 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 --emsg_off;
9073}
9074
9075/*
9076 * "bufnr(expr)" function
9077 */
9078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 typval_T *argvars;
9081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009084 int error = FALSE;
9085 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009090 --emsg_off;
9091
9092 /* If the buffer isn't found and the second argument is not zero create a
9093 * new buffer. */
9094 if (buf == NULL
9095 && argvars[1].v_type != VAR_UNKNOWN
9096 && get_tv_number_chk(&argvars[1], &error) != 0
9097 && !error
9098 && (name = get_tv_string_chk(&argvars[0])) != NULL
9099 && !error)
9100 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106}
9107
9108/*
9109 * "bufwinnr(nr)" function
9110 */
9111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 typval_T *argvars;
9114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
9116#ifdef FEAT_WINDOWS
9117 win_T *wp;
9118 int winnr = 0;
9119#endif
9120 buf_T *buf;
9121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009122 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125#ifdef FEAT_WINDOWS
9126 for (wp = firstwin; wp; wp = wp->w_next)
9127 {
9128 ++winnr;
9129 if (wp->w_buffer == buf)
9130 break;
9131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#endif
9136 --emsg_off;
9137}
9138
9139/*
9140 * "byte2line(byte)" function
9141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#else
9150 long boff = 0;
9151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 (linenr_T)0, &boff);
9158#endif
9159}
9160
9161/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009162 * "byteidx()" function
9163 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *argvars;
9167 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009168{
9169#ifdef FEAT_MBYTE
9170 char_u *t;
9171#endif
9172 char_u *str;
9173 long idx;
9174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 str = get_tv_string_chk(&argvars[0]);
9176 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009179 return;
9180
9181#ifdef FEAT_MBYTE
9182 t = str;
9183 for ( ; idx > 0; idx--)
9184 {
9185 if (*t == NUL) /* EOL reached */
9186 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009187 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009188 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009189 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009190#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009191 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009193#endif
9194}
9195
Bram Moolenaardb913952012-06-29 12:54:53 +02009196 int
9197func_call(name, args, selfdict, rettv)
9198 char_u *name;
9199 typval_T *args;
9200 dict_T *selfdict;
9201 typval_T *rettv;
9202{
9203 listitem_T *item;
9204 typval_T argv[MAX_FUNC_ARGS + 1];
9205 int argc = 0;
9206 int dummy;
9207 int r = 0;
9208
9209 for (item = args->vval.v_list->lv_first; item != NULL;
9210 item = item->li_next)
9211 {
9212 if (argc == MAX_FUNC_ARGS)
9213 {
9214 EMSG(_("E699: Too many arguments"));
9215 break;
9216 }
9217 /* Make a copy of each argument. This is needed to be able to set
9218 * v_lock to VAR_FIXED in the copy without changing the original list.
9219 */
9220 copy_tv(&item->li_tv, &argv[argc++]);
9221 }
9222
9223 if (item == NULL)
9224 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9225 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9226 &dummy, TRUE, selfdict);
9227
9228 /* Free the arguments. */
9229 while (argc > 0)
9230 clear_tv(&argv[--argc]);
9231
9232 return r;
9233}
9234
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009235/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009236 * "call(func, arglist)" function
9237 */
9238 static void
9239f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009240 typval_T *argvars;
9241 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009242{
9243 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009245
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246 if (argvars[1].v_type != VAR_LIST)
9247 {
9248 EMSG(_(e_listreq));
9249 return;
9250 }
9251 if (argvars[1].vval.v_list == NULL)
9252 return;
9253
9254 if (argvars[0].v_type == VAR_FUNC)
9255 func = argvars[0].vval.v_string;
9256 else
9257 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 if (*func == NUL)
9259 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009260
Bram Moolenaare9a41262005-01-15 22:18:47 +00009261 if (argvars[2].v_type != VAR_UNKNOWN)
9262 {
9263 if (argvars[2].v_type != VAR_DICT)
9264 {
9265 EMSG(_(e_dictreq));
9266 return;
9267 }
9268 selfdict = argvars[2].vval.v_dict;
9269 }
9270
Bram Moolenaardb913952012-06-29 12:54:53 +02009271 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009272}
9273
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274#ifdef FEAT_FLOAT
9275/*
9276 * "ceil({float})" function
9277 */
9278 static void
9279f_ceil(argvars, rettv)
9280 typval_T *argvars;
9281 typval_T *rettv;
9282{
9283 float_T f;
9284
9285 rettv->v_type = VAR_FLOAT;
9286 if (get_float_arg(argvars, &f) == OK)
9287 rettv->vval.v_float = ceil(f);
9288 else
9289 rettv->vval.v_float = 0.0;
9290}
9291#endif
9292
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009293/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009294 * "changenr()" function
9295 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009296 static void
9297f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009298 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009299 typval_T *rettv;
9300{
9301 rettv->vval.v_number = curbuf->b_u_seq_cur;
9302}
9303
9304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 * "char2nr(string)" function
9306 */
9307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009309 typval_T *argvars;
9310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311{
9312#ifdef FEAT_MBYTE
9313 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009314 {
9315 int utf8 = 0;
9316
9317 if (argvars[1].v_type != VAR_UNKNOWN)
9318 utf8 = get_tv_number_chk(&argvars[1], NULL);
9319
9320 if (utf8)
9321 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9322 else
9323 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 else
9326#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328}
9329
9330/*
9331 * "cindent(lnum)" function
9332 */
9333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338#ifdef FEAT_CINDENT
9339 pos_T pos;
9340 linenr_T lnum;
9341
9342 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9345 {
9346 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 curwin->w_cursor = pos;
9349 }
9350 else
9351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353}
9354
9355/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009356 * "clearmatches()" function
9357 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009358 static void
9359f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009360 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009361 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009362{
9363#ifdef FEAT_SEARCH_EXTRA
9364 clear_matches(curwin);
9365#endif
9366}
9367
9368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 * "col(string)" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376 colnr_T col = 0;
9377 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009378 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009380 fp = var2fpos(&argvars[0], FALSE, &fnum);
9381 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 {
9383 if (fp->col == MAXCOL)
9384 {
9385 /* '> can be MAXCOL, get the length of the line then */
9386 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009387 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 else
9389 col = MAXCOL;
9390 }
9391 else
9392 {
9393 col = fp->col + 1;
9394#ifdef FEAT_VIRTUALEDIT
9395 /* col(".") when the cursor is on the NUL at the end of the line
9396 * because of "coladd" can be seen as an extra column. */
9397 if (virtual_active() && fp == &curwin->w_cursor)
9398 {
9399 char_u *p = ml_get_cursor();
9400
9401 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9402 curwin->w_virtcol - curwin->w_cursor.coladd))
9403 {
9404# ifdef FEAT_MBYTE
9405 int l;
9406
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009407 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 col += l;
9409# else
9410 if (*p != NUL && p[1] == NUL)
9411 ++col;
9412# endif
9413 }
9414 }
9415#endif
9416 }
9417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
9420
Bram Moolenaar572cb562005-08-05 21:35:02 +00009421#if defined(FEAT_INS_EXPAND)
9422/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009423 * "complete()" function
9424 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009425 static void
9426f_complete(argvars, rettv)
9427 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009428 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009429{
9430 int startcol;
9431
9432 if ((State & INSERT) == 0)
9433 {
9434 EMSG(_("E785: complete() can only be used in Insert mode"));
9435 return;
9436 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009437
9438 /* Check for undo allowed here, because if something was already inserted
9439 * the line was already saved for undo and this check isn't done. */
9440 if (!undo_allowed())
9441 return;
9442
Bram Moolenaarade00832006-03-10 21:46:58 +00009443 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9444 {
9445 EMSG(_(e_invarg));
9446 return;
9447 }
9448
9449 startcol = get_tv_number_chk(&argvars[0], NULL);
9450 if (startcol <= 0)
9451 return;
9452
9453 set_completion(startcol - 1, argvars[1].vval.v_list);
9454}
9455
9456/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009457 * "complete_add()" function
9458 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009459 static void
9460f_complete_add(argvars, rettv)
9461 typval_T *argvars;
9462 typval_T *rettv;
9463{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009464 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009465}
9466
9467/*
9468 * "complete_check()" function
9469 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009470 static void
9471f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009472 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009473 typval_T *rettv;
9474{
9475 int saved = RedrawingDisabled;
9476
9477 RedrawingDisabled = 0;
9478 ins_compl_check_keys(0);
9479 rettv->vval.v_number = compl_interrupted;
9480 RedrawingDisabled = saved;
9481}
9482#endif
9483
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484/*
9485 * "confirm(message, buttons[, default [, type]])" function
9486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009489 typval_T *argvars UNUSED;
9490 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9493 char_u *message;
9494 char_u *buttons = NULL;
9495 char_u buf[NUMBUFLEN];
9496 char_u buf2[NUMBUFLEN];
9497 int def = 1;
9498 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 char_u *typestr;
9500 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 message = get_tv_string_chk(&argvars[0]);
9503 if (message == NULL)
9504 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9508 if (buttons == NULL)
9509 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9516 if (typestr == NULL)
9517 error = TRUE;
9518 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 switch (TOUPPER_ASC(*typestr))
9521 {
9522 case 'E': type = VIM_ERROR; break;
9523 case 'Q': type = VIM_QUESTION; break;
9524 case 'I': type = VIM_INFO; break;
9525 case 'W': type = VIM_WARNING; break;
9526 case 'G': type = VIM_GENERIC; break;
9527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529 }
9530 }
9531 }
9532
9533 if (buttons == NULL || *buttons == NUL)
9534 buttons = (char_u *)_("&Ok");
9535
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009536 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009538 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539#endif
9540}
9541
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009542/*
9543 * "copy()" function
9544 */
9545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009550 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009553#ifdef FEAT_FLOAT
9554/*
9555 * "cos()" function
9556 */
9557 static void
9558f_cos(argvars, rettv)
9559 typval_T *argvars;
9560 typval_T *rettv;
9561{
9562 float_T f;
9563
9564 rettv->v_type = VAR_FLOAT;
9565 if (get_float_arg(argvars, &f) == OK)
9566 rettv->vval.v_float = cos(f);
9567 else
9568 rettv->vval.v_float = 0.0;
9569}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009570
9571/*
9572 * "cosh()" function
9573 */
9574 static void
9575f_cosh(argvars, rettv)
9576 typval_T *argvars;
9577 typval_T *rettv;
9578{
9579 float_T f;
9580
9581 rettv->v_type = VAR_FLOAT;
9582 if (get_float_arg(argvars, &f) == OK)
9583 rettv->vval.v_float = cosh(f);
9584 else
9585 rettv->vval.v_float = 0.0;
9586}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009587#endif
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009590 * "count()" function
9591 */
9592 static void
9593f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 typval_T *argvars;
9595 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009597 long n = 0;
9598 int ic = FALSE;
9599
Bram Moolenaare9a41262005-01-15 22:18:47 +00009600 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009602 listitem_T *li;
9603 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009605
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 if ((l = argvars[0].vval.v_list) != NULL)
9607 {
9608 li = l->lv_first;
9609 if (argvars[2].v_type != VAR_UNKNOWN)
9610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 int error = FALSE;
9612
9613 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 if (argvars[3].v_type != VAR_UNKNOWN)
9615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 idx = get_tv_number_chk(&argvars[3], &error);
9617 if (!error)
9618 {
9619 li = list_find(l, idx);
9620 if (li == NULL)
9621 EMSGN(_(e_listidx), idx);
9622 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009624 if (error)
9625 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 }
9627
9628 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009629 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009630 ++n;
9631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 else if (argvars[0].v_type == VAR_DICT)
9634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 int todo;
9636 dict_T *d;
9637 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009639 if ((d = argvars[0].vval.v_dict) != NULL)
9640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 int error = FALSE;
9642
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 if (argvars[2].v_type != VAR_UNKNOWN)
9644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 if (argvars[3].v_type != VAR_UNKNOWN)
9647 EMSG(_(e_invarg));
9648 }
9649
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009650 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009652 {
9653 if (!HASHITEM_EMPTY(hi))
9654 {
9655 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009656 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009657 ++n;
9658 }
9659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009660 }
9661 }
9662 else
9663 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009664 rettv->vval.v_number = n;
9665}
9666
9667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9669 *
9670 * Checks the existence of a cscope connection.
9671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009674 typval_T *argvars UNUSED;
9675 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677#ifdef FEAT_CSCOPE
9678 int num = 0;
9679 char_u *dbpath = NULL;
9680 char_u *prepend = NULL;
9681 char_u buf[NUMBUFLEN];
9682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009683 if (argvars[0].v_type != VAR_UNKNOWN
9684 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 num = (int)get_tv_number(&argvars[0]);
9687 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009688 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 }
9691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#endif
9694}
9695
9696/*
9697 * "cursor(lnum, col)" function
9698 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009699 * Moves the cursor to the specified line and column.
9700 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009704 typval_T *argvars;
9705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009708#ifdef FEAT_VIRTUALEDIT
9709 long coladd = 0;
9710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009712 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009713 if (argvars[1].v_type == VAR_UNKNOWN)
9714 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009715 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009716
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009717 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009718 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009719 line = pos.lnum;
9720 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009721#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009722 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009723#endif
9724 }
9725 else
9726 {
9727 line = get_tv_lnum(argvars);
9728 col = get_tv_number_chk(&argvars[1], NULL);
9729#ifdef FEAT_VIRTUALEDIT
9730 if (argvars[2].v_type != VAR_UNKNOWN)
9731 coladd = get_tv_number_chk(&argvars[2], NULL);
9732#endif
9733 }
9734 if (line < 0 || col < 0
9735#ifdef FEAT_VIRTUALEDIT
9736 || coladd < 0
9737#endif
9738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (line > 0)
9741 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (col > 0)
9743 curwin->w_cursor.col = col - 1;
9744#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009745 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746#endif
9747
9748 /* Make sure the cursor is in a valid position. */
9749 check_cursor();
9750#ifdef FEAT_MBYTE
9751 /* Correct cursor for multi-byte character. */
9752 if (has_mbyte)
9753 mb_adjust_cursor();
9754#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009755
9756 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009757 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009761 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 */
9763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009768 int noref = 0;
9769
9770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009772 if (noref < 0 || noref > 1)
9773 EMSG(_(e_invarg));
9774 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009775 {
9776 current_copyID += COPYID_INC;
9777 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779}
9780
9781/*
9782 * "delete()" function
9783 */
9784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 typval_T *argvars;
9787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
9795/*
9796 * "did_filetype()" function
9797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009800 typval_T *argvars UNUSED;
9801 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802{
9803#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805#endif
9806}
9807
9808/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009809 * "diff_filler()" function
9810 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009813 typval_T *argvars UNUSED;
9814 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009815{
9816#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009818#endif
9819}
9820
9821/*
9822 * "diff_hlID()" function
9823 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009826 typval_T *argvars UNUSED;
9827 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828{
9829#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009831 static linenr_T prev_lnum = 0;
9832 static int changedtick = 0;
9833 static int fnum = 0;
9834 static int change_start = 0;
9835 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009836 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009837 int filler_lines;
9838 int col;
9839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 if (lnum < 0) /* ignore type error in {lnum} arg */
9841 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 if (lnum != prev_lnum
9843 || changedtick != curbuf->b_changedtick
9844 || fnum != curbuf->b_fnum)
9845 {
9846 /* New line, buffer, change: need to get the values. */
9847 filler_lines = diff_check(curwin, lnum);
9848 if (filler_lines < 0)
9849 {
9850 if (filler_lines == -1)
9851 {
9852 change_start = MAXCOL;
9853 change_end = -1;
9854 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9855 hlID = HLF_ADD; /* added line */
9856 else
9857 hlID = HLF_CHD; /* changed line */
9858 }
9859 else
9860 hlID = HLF_ADD; /* added line */
9861 }
9862 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009863 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009864 prev_lnum = lnum;
9865 changedtick = curbuf->b_changedtick;
9866 fnum = curbuf->b_fnum;
9867 }
9868
9869 if (hlID == HLF_CHD || hlID == HLF_TXD)
9870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009872 if (col >= change_start && col <= change_end)
9873 hlID = HLF_TXD; /* changed text */
9874 else
9875 hlID = HLF_CHD; /* changed line */
9876 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009877 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009878#endif
9879}
9880
9881/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009882 * "empty({expr})" function
9883 */
9884 static void
9885f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009886 typval_T *argvars;
9887 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009888{
9889 int n;
9890
9891 switch (argvars[0].v_type)
9892 {
9893 case VAR_STRING:
9894 case VAR_FUNC:
9895 n = argvars[0].vval.v_string == NULL
9896 || *argvars[0].vval.v_string == NUL;
9897 break;
9898 case VAR_NUMBER:
9899 n = argvars[0].vval.v_number == 0;
9900 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009901#ifdef FEAT_FLOAT
9902 case VAR_FLOAT:
9903 n = argvars[0].vval.v_float == 0.0;
9904 break;
9905#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009906 case VAR_LIST:
9907 n = argvars[0].vval.v_list == NULL
9908 || argvars[0].vval.v_list->lv_first == NULL;
9909 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 case VAR_DICT:
9911 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009914 default:
9915 EMSG2(_(e_intern2), "f_empty()");
9916 n = 0;
9917 }
9918
9919 rettv->vval.v_number = n;
9920}
9921
9922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 * "escape({string}, {chars})" function
9924 */
9925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009927 typval_T *argvars;
9928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
9930 char_u buf[NUMBUFLEN];
9931
Bram Moolenaar758711c2005-02-02 23:11:38 +00009932 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9933 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935}
9936
9937/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009938 * "eval()" function
9939 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009940 static void
9941f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 typval_T *argvars;
9943 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009944{
9945 char_u *s;
9946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 s = get_tv_string_chk(&argvars[0]);
9948 if (s != NULL)
9949 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9952 {
9953 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009956 else if (*s != NUL)
9957 EMSG(_(e_trailing));
9958}
9959
9960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 * "eventhandler()" function
9962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969}
9970
9971/*
9972 * "executable()" function
9973 */
9974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982/*
9983 * "exists()" function
9984 */
9985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989{
9990 char_u *p;
9991 char_u *name;
9992 int n = FALSE;
9993 int len = 0;
9994
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009995 no_autoload = TRUE;
9996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 if (*p == '$') /* environment variable */
9999 {
10000 /* first try "normal" environment variables (fast) */
10001 if (mch_getenv(p + 1) != NULL)
10002 n = TRUE;
10003 else
10004 {
10005 /* try expanding things like $VIM and ${HOME} */
10006 p = expand_env_save(p);
10007 if (p != NULL && *p != '$')
10008 n = TRUE;
10009 vim_free(p);
10010 }
10011 }
10012 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010015 if (*skipwhite(p) != NUL)
10016 n = FALSE; /* trailing garbage */
10017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 else if (*p == '*') /* internal or user defined function */
10019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010020 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022 else if (*p == ':')
10023 {
10024 n = cmd_exists(p + 1);
10025 }
10026 else if (*p == '#')
10027 {
10028#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010029 if (p[1] == '#')
10030 n = autocmd_supported(p + 2);
10031 else
10032 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033#endif
10034 }
10035 else /* internal variable */
10036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010037 char_u *tofree;
10038 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010040 /* get_name_len() takes care of expanding curly braces */
10041 name = p;
10042 len = get_name_len(&p, &tofree, TRUE, FALSE);
10043 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010045 if (tofree != NULL)
10046 name = tofree;
10047 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10048 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010050 /* handle d.key, l[idx], f(expr) */
10051 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10052 if (n)
10053 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 }
10055 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010056 if (*p != NUL)
10057 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010059 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010063
10064 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065}
10066
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010067#ifdef FEAT_FLOAT
10068/*
10069 * "exp()" function
10070 */
10071 static void
10072f_exp(argvars, rettv)
10073 typval_T *argvars;
10074 typval_T *rettv;
10075{
10076 float_T f;
10077
10078 rettv->v_type = VAR_FLOAT;
10079 if (get_float_arg(argvars, &f) == OK)
10080 rettv->vval.v_float = exp(f);
10081 else
10082 rettv->vval.v_float = 0.0;
10083}
10084#endif
10085
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086/*
10087 * "expand()" function
10088 */
10089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 typval_T *argvars;
10092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094 char_u *s;
10095 int len;
10096 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010097 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010099 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010100 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010103 if (argvars[1].v_type != VAR_UNKNOWN
10104 && argvars[2].v_type != VAR_UNKNOWN
10105 && get_tv_number_chk(&argvars[2], &error)
10106 && !error)
10107 {
10108 rettv->v_type = VAR_LIST;
10109 rettv->vval.v_list = NULL;
10110 }
10111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 if (*s == '%' || *s == '#' || *s == '<')
10114 {
10115 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010116 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010118 if (rettv->v_type == VAR_LIST)
10119 {
10120 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10121 list_append_string(rettv->vval.v_list, result, -1);
10122 else
10123 vim_free(result);
10124 }
10125 else
10126 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 }
10128 else
10129 {
10130 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010131 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 if (argvars[1].v_type != VAR_UNKNOWN
10133 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010134 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 if (!error)
10136 {
10137 ExpandInit(&xpc);
10138 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010139 if (p_wic)
10140 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010141 if (rettv->v_type == VAR_STRING)
10142 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10143 options, WILD_ALL);
10144 else if (rettv_list_alloc(rettv) != FAIL)
10145 {
10146 int i;
10147
10148 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10149 for (i = 0; i < xpc.xp_numfiles; i++)
10150 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10151 ExpandCleanup(&xpc);
10152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 }
10154 else
10155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 }
10157}
10158
10159/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010160 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010161 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010162 */
10163 static void
10164f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010165 typval_T *argvars;
10166 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010167{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010168 char *arg_errmsg = N_("extend() argument");
10169
Bram Moolenaare9a41262005-01-15 22:18:47 +000010170 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010172 list_T *l1, *l2;
10173 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010174 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010176
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 l1 = argvars[0].vval.v_list;
10178 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010179 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010180 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010181 {
10182 if (argvars[2].v_type != VAR_UNKNOWN)
10183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 before = get_tv_number_chk(&argvars[2], &error);
10185 if (error)
10186 return; /* type error; errmsg already given */
10187
Bram Moolenaar758711c2005-02-02 23:11:38 +000010188 if (before == l1->lv_len)
10189 item = NULL;
10190 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010192 item = list_find(l1, before);
10193 if (item == NULL)
10194 {
10195 EMSGN(_(e_listidx), before);
10196 return;
10197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 }
10199 }
10200 else
10201 item = NULL;
10202 list_extend(l1, l2, item);
10203
Bram Moolenaare9a41262005-01-15 22:18:47 +000010204 copy_tv(&argvars[0], rettv);
10205 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010207 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 dict_T *d1, *d2;
10210 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010211 char_u *action;
10212 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010214 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010215
10216 d1 = argvars[0].vval.v_dict;
10217 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010218 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010219 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 {
10221 /* Check the third argument. */
10222 if (argvars[2].v_type != VAR_UNKNOWN)
10223 {
10224 static char *(av[]) = {"keep", "force", "error"};
10225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 action = get_tv_string_chk(&argvars[2]);
10227 if (action == NULL)
10228 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 for (i = 0; i < 3; ++i)
10230 if (STRCMP(action, av[i]) == 0)
10231 break;
10232 if (i == 3)
10233 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010234 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 return;
10236 }
10237 }
10238 else
10239 action = (char_u *)"force";
10240
10241 /* Go over all entries in the second dict and add them to the
10242 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010243 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010244 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010246 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010247 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010248 --todo;
10249 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010250 if (d1->dv_scope != 0)
10251 {
10252 /* Disallow replacing a builtin function in l: and g:.
10253 * Check the key to be valid when adding to any
10254 * scope. */
10255 if (d1->dv_scope == VAR_DEF_SCOPE
10256 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10257 && var_check_func_name(hi2->hi_key,
10258 di1 == NULL))
10259 break;
10260 if (!valid_varname(hi2->hi_key))
10261 break;
10262 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010263 if (di1 == NULL)
10264 {
10265 di1 = dictitem_copy(HI2DI(hi2));
10266 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10267 dictitem_free(di1);
10268 }
10269 else if (*action == 'e')
10270 {
10271 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10272 break;
10273 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010274 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010275 {
10276 clear_tv(&di1->di_tv);
10277 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10278 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 }
10280 }
10281
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 copy_tv(&argvars[0], rettv);
10283 }
10284 }
10285 else
10286 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010287}
10288
10289/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010290 * "feedkeys()" function
10291 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010292 static void
10293f_feedkeys(argvars, rettv)
10294 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010295 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010296{
10297 int remap = TRUE;
10298 char_u *keys, *flags;
10299 char_u nbuf[NUMBUFLEN];
10300 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010301 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010302
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010303 /* This is not allowed in the sandbox. If the commands would still be
10304 * executed in the sandbox it would be OK, but it probably happens later,
10305 * when "sandbox" is no longer set. */
10306 if (check_secure())
10307 return;
10308
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010309 keys = get_tv_string(&argvars[0]);
10310 if (*keys != NUL)
10311 {
10312 if (argvars[1].v_type != VAR_UNKNOWN)
10313 {
10314 flags = get_tv_string_buf(&argvars[1], nbuf);
10315 for ( ; *flags != NUL; ++flags)
10316 {
10317 switch (*flags)
10318 {
10319 case 'n': remap = FALSE; break;
10320 case 'm': remap = TRUE; break;
10321 case 't': typed = TRUE; break;
10322 }
10323 }
10324 }
10325
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010326 /* Need to escape K_SPECIAL and CSI before putting the string in the
10327 * typeahead buffer. */
10328 keys_esc = vim_strsave_escape_csi(keys);
10329 if (keys_esc != NULL)
10330 {
10331 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010332 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010333 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010334 if (vgetc_busy)
10335 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010336 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010337 }
10338}
10339
10340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 * "filereadable()" function
10342 */
10343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010345 typval_T *argvars;
10346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010348 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 char_u *p;
10350 int n;
10351
Bram Moolenaarc236c162008-07-13 17:41:49 +000010352#ifndef O_NONBLOCK
10353# define O_NONBLOCK 0
10354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010356 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10357 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 {
10359 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010360 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 }
10362 else
10363 n = FALSE;
10364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366}
10367
10368/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010369 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 * rights to write into.
10371 */
10372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010374 typval_T *argvars;
10375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010377 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378}
10379
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010380static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010381
10382 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010383findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010384 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010386 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010387{
10388#ifdef FEAT_SEARCHPATH
10389 char_u *fname;
10390 char_u *fresult = NULL;
10391 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10392 char_u *p;
10393 char_u pathbuf[NUMBUFLEN];
10394 int count = 1;
10395 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010396 int error = FALSE;
10397#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010398
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010399 rettv->vval.v_string = NULL;
10400 rettv->v_type = VAR_STRING;
10401
10402#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010404
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010405 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010407 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10408 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010409 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 else
10411 {
10412 if (*p != NUL)
10413 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010416 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010418 }
10419
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010420 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10421 error = TRUE;
10422
10423 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 do
10426 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010427 if (rettv->v_type == VAR_STRING)
10428 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 fresult = find_file_in_path_option(first ? fname : NULL,
10430 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010431 0, first, path,
10432 find_what,
10433 curbuf->b_ffname,
10434 find_what == FINDFILE_DIR
10435 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010437
10438 if (fresult != NULL && rettv->v_type == VAR_LIST)
10439 list_append_string(rettv->vval.v_list, fresult, -1);
10440
10441 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010443
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010444 if (rettv->v_type == VAR_STRING)
10445 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010446#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010447}
10448
Bram Moolenaar33570922005-01-25 22:26:29 +000010449static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10450static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451
10452/*
10453 * Implementation of map() and filter().
10454 */
10455 static void
10456filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010457 typval_T *argvars;
10458 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010459 int map;
10460{
10461 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010462 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 listitem_T *li, *nli;
10464 list_T *l = NULL;
10465 dictitem_T *di;
10466 hashtab_T *ht;
10467 hashitem_T *hi;
10468 dict_T *d = NULL;
10469 typval_T save_val;
10470 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010472 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010473 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10474 char *arg_errmsg = (map ? N_("map() argument")
10475 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010476 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010477 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010478
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010480 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010481 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010482 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010483 return;
10484 }
10485 else if (argvars[0].v_type == VAR_DICT)
10486 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010487 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010488 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489 return;
10490 }
10491 else
10492 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010493 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 return;
10495 }
10496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 expr = get_tv_string_buf_chk(&argvars[1], buf);
10498 /* On type errors, the preceding call has already displayed an error
10499 * message. Avoid a misleading error message for an empty string that
10500 * was not passed as argument. */
10501 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 prepare_vimvar(VV_VAL, &save_val);
10504 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010505
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010506 /* We reset "did_emsg" to be able to detect whether an error
10507 * occurred during evaluation of the expression. */
10508 save_did_emsg = did_emsg;
10509 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010510
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010511 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 vimvars[VV_KEY].vv_type = VAR_STRING;
10515
10516 ht = &d->dv_hashtab;
10517 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010518 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 if (!HASHITEM_EMPTY(hi))
10522 {
10523 --todo;
10524 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010525 if (tv_check_lock(di->di_tv.v_lock,
10526 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 break;
10528 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010529 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010530 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 break;
10532 if (!map && rem)
10533 dictitem_remove(d, di);
10534 clear_tv(&vimvars[VV_KEY].vv_tv);
10535 }
10536 }
10537 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 }
10539 else
10540 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010541 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 for (li = l->lv_first; li != NULL; li = nli)
10544 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010545 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010546 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010548 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010549 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010550 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010551 break;
10552 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010554 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010555 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010557
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010558 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010560
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010561 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563
10564 copy_tv(&argvars[0], rettv);
10565}
10566
10567 static int
10568filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010569 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 char_u *expr;
10571 int map;
10572 int *remp;
10573{
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010576 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 s = expr;
10580 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010581 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 if (*s != NUL) /* check for trailing chars after expr */
10583 {
10584 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010585 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 }
10587 if (map)
10588 {
10589 /* map(): replace the list item value */
10590 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010591 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 *tv = rettv;
10593 }
10594 else
10595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010596 int error = FALSE;
10597
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 /* On type error, nothing has been removed; return FAIL to stop the
10602 * loop. The error message was given by get_tv_number_chk(). */
10603 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010604 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010606 retval = OK;
10607theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010610}
10611
10612/*
10613 * "filter()" function
10614 */
10615 static void
10616f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010617 typval_T *argvars;
10618 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619{
10620 filter_map(argvars, rettv, FALSE);
10621}
10622
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010623/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010624 * "finddir({fname}[, {path}[, {count}]])" function
10625 */
10626 static void
10627f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 typval_T *argvars;
10629 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010630{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010631 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632}
10633
10634/*
10635 * "findfile({fname}[, {path}[, {count}]])" function
10636 */
10637 static void
10638f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010639 typval_T *argvars;
10640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010642 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010643}
10644
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010645#ifdef FEAT_FLOAT
10646/*
10647 * "float2nr({float})" function
10648 */
10649 static void
10650f_float2nr(argvars, rettv)
10651 typval_T *argvars;
10652 typval_T *rettv;
10653{
10654 float_T f;
10655
10656 if (get_float_arg(argvars, &f) == OK)
10657 {
10658 if (f < -0x7fffffff)
10659 rettv->vval.v_number = -0x7fffffff;
10660 else if (f > 0x7fffffff)
10661 rettv->vval.v_number = 0x7fffffff;
10662 else
10663 rettv->vval.v_number = (varnumber_T)f;
10664 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010665}
10666
10667/*
10668 * "floor({float})" function
10669 */
10670 static void
10671f_floor(argvars, rettv)
10672 typval_T *argvars;
10673 typval_T *rettv;
10674{
10675 float_T f;
10676
10677 rettv->v_type = VAR_FLOAT;
10678 if (get_float_arg(argvars, &f) == OK)
10679 rettv->vval.v_float = floor(f);
10680 else
10681 rettv->vval.v_float = 0.0;
10682}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010683
10684/*
10685 * "fmod()" function
10686 */
10687 static void
10688f_fmod(argvars, rettv)
10689 typval_T *argvars;
10690 typval_T *rettv;
10691{
10692 float_T fx, fy;
10693
10694 rettv->v_type = VAR_FLOAT;
10695 if (get_float_arg(argvars, &fx) == OK
10696 && get_float_arg(&argvars[1], &fy) == OK)
10697 rettv->vval.v_float = fmod(fx, fy);
10698 else
10699 rettv->vval.v_float = 0.0;
10700}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010701#endif
10702
Bram Moolenaar0d660222005-01-07 21:51:51 +000010703/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010704 * "fnameescape({string})" function
10705 */
10706 static void
10707f_fnameescape(argvars, rettv)
10708 typval_T *argvars;
10709 typval_T *rettv;
10710{
10711 rettv->vval.v_string = vim_strsave_fnameescape(
10712 get_tv_string(&argvars[0]), FALSE);
10713 rettv->v_type = VAR_STRING;
10714}
10715
10716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 * "fnamemodify({fname}, {mods})" function
10718 */
10719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010721 typval_T *argvars;
10722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
10724 char_u *fname;
10725 char_u *mods;
10726 int usedlen = 0;
10727 int len;
10728 char_u *fbuf = NULL;
10729 char_u buf[NUMBUFLEN];
10730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010731 fname = get_tv_string_chk(&argvars[0]);
10732 mods = get_tv_string_buf_chk(&argvars[1], buf);
10733 if (fname == NULL || mods == NULL)
10734 fname = NULL;
10735 else
10736 {
10737 len = (int)STRLEN(fname);
10738 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 vim_free(fbuf);
10747}
10748
Bram Moolenaar33570922005-01-25 22:26:29 +000010749static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750
10751/*
10752 * "foldclosed()" function
10753 */
10754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010757 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010758 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759{
10760#ifdef FEAT_FOLDING
10761 linenr_T lnum;
10762 linenr_T first, last;
10763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10766 {
10767 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10768 {
10769 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 return;
10774 }
10775 }
10776#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778}
10779
10780/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781 * "foldclosed()" function
10782 */
10783 static void
10784f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 typval_T *argvars;
10786 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010787{
10788 foldclosed_both(argvars, rettv, FALSE);
10789}
10790
10791/*
10792 * "foldclosedend()" function
10793 */
10794 static void
10795f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 typval_T *argvars;
10797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798{
10799 foldclosed_both(argvars, rettv, TRUE);
10800}
10801
10802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 * "foldlevel()" function
10804 */
10805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010807 typval_T *argvars UNUSED;
10808 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809{
10810#ifdef FEAT_FOLDING
10811 linenr_T lnum;
10812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817}
10818
10819/*
10820 * "foldtext()" function
10821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826{
10827#ifdef FEAT_FOLDING
10828 linenr_T lnum;
10829 char_u *s;
10830 char_u *r;
10831 int len;
10832 char *txt;
10833#endif
10834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 rettv->v_type = VAR_STRING;
10836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10839 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10840 <= curbuf->b_ml.ml_line_count
10841 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 {
10843 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10845 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 {
10847 if (!linewhite(lnum))
10848 break;
10849 ++lnum;
10850 }
10851
10852 /* Find interesting text in this line. */
10853 s = skipwhite(ml_get(lnum));
10854 /* skip C comment-start */
10855 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010858 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010859 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010860 {
10861 s = skipwhite(ml_get(lnum + 1));
10862 if (*s == '*')
10863 s = skipwhite(s + 1);
10864 }
10865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 txt = _("+-%s%3ld lines: ");
10867 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010868 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 + 20 /* for %3ld */
10870 + STRLEN(s))); /* concatenated */
10871 if (r != NULL)
10872 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10874 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10875 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 len = (int)STRLEN(r);
10877 STRCAT(r, s);
10878 /* remove 'foldmarker' and 'commentstring' */
10879 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 }
10882 }
10883#endif
10884}
10885
10886/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010887 * "foldtextresult(lnum)" function
10888 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010891 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010893{
10894#ifdef FEAT_FOLDING
10895 linenr_T lnum;
10896 char_u *text;
10897 char_u buf[51];
10898 foldinfo_T foldinfo;
10899 int fold_count;
10900#endif
10901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->v_type = VAR_STRING;
10903 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010904#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 /* treat illegal types and illegal string values for {lnum} the same */
10907 if (lnum < 0)
10908 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010909 fold_count = foldedCount(curwin, lnum, &foldinfo);
10910 if (fold_count > 0)
10911 {
10912 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10913 &foldinfo, buf);
10914 if (text == buf)
10915 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010917 }
10918#endif
10919}
10920
10921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 * "foreground()" function
10923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010926 typval_T *argvars UNUSED;
10927 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929#ifdef FEAT_GUI
10930 if (gui.in_use)
10931 gui_mch_set_foreground();
10932#else
10933# ifdef WIN32
10934 win32_set_foreground();
10935# endif
10936#endif
10937}
10938
10939/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940 * "function()" function
10941 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *argvars;
10945 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010946{
10947 char_u *s;
10948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010950 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010952 /* Don't check an autoload name for existence here. */
10953 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010954 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 else
10956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 rettv->vval.v_string = vim_strsave(s);
10958 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959 }
10960}
10961
10962/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010963 * "garbagecollect()" function
10964 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010965 static void
10966f_garbagecollect(argvars, rettv)
10967 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010968 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010969{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010970 /* This is postponed until we are back at the toplevel, because we may be
10971 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10972 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010973
10974 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10975 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010976}
10977
10978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010979 * "get()" function
10980 */
10981 static void
10982f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 typval_T *argvars;
10984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010985{
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 listitem_T *li;
10987 list_T *l;
10988 dictitem_T *di;
10989 dict_T *d;
10990 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010993 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 int error = FALSE;
10997
10998 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10999 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 else if (argvars[0].v_type == VAR_DICT)
11004 {
11005 if ((d = argvars[0].vval.v_dict) != NULL)
11006 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011007 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011008 if (di != NULL)
11009 tv = &di->di_tv;
11010 }
11011 }
11012 else
11013 EMSG2(_(e_listdictarg), "get()");
11014
11015 if (tv == NULL)
11016 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011018 copy_tv(&argvars[2], rettv);
11019 }
11020 else
11021 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011022}
11023
Bram Moolenaar342337a2005-07-21 21:11:17 +000011024static 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 +000011025
11026/*
11027 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011028 * Return a range (from start to end) of lines in rettv from the specified
11029 * buffer.
11030 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011031 */
11032 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011033get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011034 buf_T *buf;
11035 linenr_T start;
11036 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011037 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011038 typval_T *rettv;
11039{
11040 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011041
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011042 if (retlist && rettv_list_alloc(rettv) == FAIL)
11043 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011044
11045 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11046 return;
11047
11048 if (!retlist)
11049 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011050 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11051 p = ml_get_buf(buf, start, FALSE);
11052 else
11053 p = (char_u *)"";
11054
11055 rettv->v_type = VAR_STRING;
11056 rettv->vval.v_string = vim_strsave(p);
11057 }
11058 else
11059 {
11060 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011061 return;
11062
11063 if (start < 1)
11064 start = 1;
11065 if (end > buf->b_ml.ml_line_count)
11066 end = buf->b_ml.ml_line_count;
11067 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011068 if (list_append_string(rettv->vval.v_list,
11069 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011070 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011071 }
11072}
11073
11074/*
11075 * "getbufline()" function
11076 */
11077 static void
11078f_getbufline(argvars, rettv)
11079 typval_T *argvars;
11080 typval_T *rettv;
11081{
11082 linenr_T lnum;
11083 linenr_T end;
11084 buf_T *buf;
11085
11086 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11087 ++emsg_off;
11088 buf = get_buf_tv(&argvars[0]);
11089 --emsg_off;
11090
Bram Moolenaar661b1822005-07-28 22:36:45 +000011091 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011092 if (argvars[2].v_type == VAR_UNKNOWN)
11093 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011094 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011095 end = get_tv_lnum_buf(&argvars[2], buf);
11096
Bram Moolenaar342337a2005-07-21 21:11:17 +000011097 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011098}
11099
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100/*
11101 * "getbufvar()" function
11102 */
11103 static void
11104f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011105 typval_T *argvars;
11106 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011107{
11108 buf_T *buf;
11109 buf_T *save_curbuf;
11110 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11114 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115 ++emsg_off;
11116 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011118 if (argvars[2].v_type != VAR_UNKNOWN)
11119 /* set the default value */
11120 copy_tv(&argvars[2], rettv);
11121 else
11122 {
11123 rettv->v_type = VAR_STRING;
11124 rettv->vval.v_string = NULL;
11125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126
11127 if (buf != NULL && varname != NULL)
11128 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011129 /* set curbuf to be our buf, temporarily */
11130 save_curbuf = curbuf;
11131 curbuf = buf;
11132
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011134 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011135 else if (STRCMP(varname, "changedtick") == 0)
11136 {
11137 rettv->v_type = VAR_NUMBER;
11138 rettv->vval.v_number = curbuf->b_changedtick;
11139 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140 else
11141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 if (*varname == NUL)
11143 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11144 * scope prefix before the NUL byte is required by
11145 * find_var_in_ht(). */
11146 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011148 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011150 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011152
11153 /* restore previous notion of curbuf */
11154 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155 }
11156
11157 --emsg_off;
11158}
11159
11160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 * "getchar()" function
11162 */
11163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *argvars;
11166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011171 /* Position the cursor. Needed after a message that ends in a space. */
11172 windgoto(msg_row, msg_col);
11173
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 ++no_mapping;
11175 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011176 for (;;)
11177 {
11178 if (argvars[0].v_type == VAR_UNKNOWN)
11179 /* getchar(): blocking wait. */
11180 n = safe_vgetc();
11181 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11182 /* getchar(1): only check if char avail */
11183 n = vpeekc();
11184 else if (error || vpeekc() == NUL)
11185 /* illegal argument or getchar(0) and no char avail: return zero */
11186 n = 0;
11187 else
11188 /* getchar(0) and char avail: return char */
11189 n = safe_vgetc();
11190 if (n == K_IGNORE)
11191 continue;
11192 break;
11193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 --no_mapping;
11195 --allow_keys;
11196
Bram Moolenaar219b8702006-11-01 14:32:36 +000011197 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11198 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11199 vimvars[VV_MOUSE_COL].vv_nr = 0;
11200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 if (IS_SPECIAL(n) || mod_mask != 0)
11203 {
11204 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11205 int i = 0;
11206
11207 /* Turn a special key into three bytes, plus modifier. */
11208 if (mod_mask != 0)
11209 {
11210 temp[i++] = K_SPECIAL;
11211 temp[i++] = KS_MODIFIER;
11212 temp[i++] = mod_mask;
11213 }
11214 if (IS_SPECIAL(n))
11215 {
11216 temp[i++] = K_SPECIAL;
11217 temp[i++] = K_SECOND(n);
11218 temp[i++] = K_THIRD(n);
11219 }
11220#ifdef FEAT_MBYTE
11221 else if (has_mbyte)
11222 i += (*mb_char2bytes)(n, temp + i);
11223#endif
11224 else
11225 temp[i++] = n;
11226 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227 rettv->v_type = VAR_STRING;
11228 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011229
11230#ifdef FEAT_MOUSE
11231 if (n == K_LEFTMOUSE
11232 || n == K_LEFTMOUSE_NM
11233 || n == K_LEFTDRAG
11234 || n == K_LEFTRELEASE
11235 || n == K_LEFTRELEASE_NM
11236 || n == K_MIDDLEMOUSE
11237 || n == K_MIDDLEDRAG
11238 || n == K_MIDDLERELEASE
11239 || n == K_RIGHTMOUSE
11240 || n == K_RIGHTDRAG
11241 || n == K_RIGHTRELEASE
11242 || n == K_X1MOUSE
11243 || n == K_X1DRAG
11244 || n == K_X1RELEASE
11245 || n == K_X2MOUSE
11246 || n == K_X2DRAG
11247 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011248 || n == K_MOUSELEFT
11249 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011250 || n == K_MOUSEDOWN
11251 || n == K_MOUSEUP)
11252 {
11253 int row = mouse_row;
11254 int col = mouse_col;
11255 win_T *win;
11256 linenr_T lnum;
11257# ifdef FEAT_WINDOWS
11258 win_T *wp;
11259# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011260 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011261
11262 if (row >= 0 && col >= 0)
11263 {
11264 /* Find the window at the mouse coordinates and compute the
11265 * text position. */
11266 win = mouse_find_win(&row, &col);
11267 (void)mouse_comp_pos(win, &row, &col, &lnum);
11268# ifdef FEAT_WINDOWS
11269 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011270 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011271# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011272 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011273 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11274 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11275 }
11276 }
11277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 }
11279}
11280
11281/*
11282 * "getcharmod()" function
11283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011286 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290}
11291
11292/*
11293 * "getcmdline()" function
11294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->v_type = VAR_STRING;
11301 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302}
11303
11304/*
11305 * "getcmdpos()" function
11306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
11315/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011316 * "getcmdtype()" function
11317 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011318 static void
11319f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011320 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011321 typval_T *rettv;
11322{
11323 rettv->v_type = VAR_STRING;
11324 rettv->vval.v_string = alloc(2);
11325 if (rettv->vval.v_string != NULL)
11326 {
11327 rettv->vval.v_string[0] = get_cmdline_type();
11328 rettv->vval.v_string[1] = NUL;
11329 }
11330}
11331
11332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 * "getcwd()" function
11334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011337 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011340 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011343 rettv->vval.v_string = NULL;
11344 cwd = alloc(MAXPATHL);
11345 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011347 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11348 {
11349 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011351 if (rettv->vval.v_string != NULL)
11352 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011354 }
11355 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 }
11357}
11358
11359/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011360 * "getfontname()" function
11361 */
11362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011366{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->v_type = VAR_STRING;
11368 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011369#ifdef FEAT_GUI
11370 if (gui.in_use)
11371 {
11372 GuiFont font;
11373 char_u *name = NULL;
11374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011375 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011376 {
11377 /* Get the "Normal" font. Either the name saved by
11378 * hl_set_font_name() or from the font ID. */
11379 font = gui.norm_font;
11380 name = hl_get_font_name();
11381 }
11382 else
11383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011385 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11386 return;
11387 font = gui_mch_get_font(name, FALSE);
11388 if (font == NOFONT)
11389 return; /* Invalid font name, return empty string. */
11390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011392 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011393 gui_mch_free_font(font);
11394 }
11395#endif
11396}
11397
11398/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399 * "getfperm({fname})" function
11400 */
11401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011403 typval_T *argvars;
11404 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011405{
11406 char_u *fname;
11407 struct stat st;
11408 char_u *perm = NULL;
11409 char_u flags[] = "rwx";
11410 int i;
11411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011415 if (mch_stat((char *)fname, &st) >= 0)
11416 {
11417 perm = vim_strsave((char_u *)"---------");
11418 if (perm != NULL)
11419 {
11420 for (i = 0; i < 9; i++)
11421 {
11422 if (st.st_mode & (1 << (8 - i)))
11423 perm[i] = flags[i % 3];
11424 }
11425 }
11426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011428}
11429
11430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 * "getfsize({fname})" function
11432 */
11433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011435 typval_T *argvars;
11436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437{
11438 char_u *fname;
11439 struct stat st;
11440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444
11445 if (mch_stat((char *)fname, &st) >= 0)
11446 {
11447 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011452
11453 /* non-perfect check for overflow */
11454 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11455 rettv->vval.v_number = -2;
11456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 }
11458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460}
11461
11462/*
11463 * "getftime({fname})" function
11464 */
11465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011467 typval_T *argvars;
11468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
11470 char_u *fname;
11471 struct stat st;
11472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
11475 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479}
11480
11481/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011482 * "getftype({fname})" function
11483 */
11484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011486 typval_T *argvars;
11487 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011488{
11489 char_u *fname;
11490 struct stat st;
11491 char_u *type = NULL;
11492 char *t;
11493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011497 if (mch_lstat((char *)fname, &st) >= 0)
11498 {
11499#ifdef S_ISREG
11500 if (S_ISREG(st.st_mode))
11501 t = "file";
11502 else if (S_ISDIR(st.st_mode))
11503 t = "dir";
11504# ifdef S_ISLNK
11505 else if (S_ISLNK(st.st_mode))
11506 t = "link";
11507# endif
11508# ifdef S_ISBLK
11509 else if (S_ISBLK(st.st_mode))
11510 t = "bdev";
11511# endif
11512# ifdef S_ISCHR
11513 else if (S_ISCHR(st.st_mode))
11514 t = "cdev";
11515# endif
11516# ifdef S_ISFIFO
11517 else if (S_ISFIFO(st.st_mode))
11518 t = "fifo";
11519# endif
11520# ifdef S_ISSOCK
11521 else if (S_ISSOCK(st.st_mode))
11522 t = "fifo";
11523# endif
11524 else
11525 t = "other";
11526#else
11527# ifdef S_IFMT
11528 switch (st.st_mode & S_IFMT)
11529 {
11530 case S_IFREG: t = "file"; break;
11531 case S_IFDIR: t = "dir"; break;
11532# ifdef S_IFLNK
11533 case S_IFLNK: t = "link"; break;
11534# endif
11535# ifdef S_IFBLK
11536 case S_IFBLK: t = "bdev"; break;
11537# endif
11538# ifdef S_IFCHR
11539 case S_IFCHR: t = "cdev"; break;
11540# endif
11541# ifdef S_IFIFO
11542 case S_IFIFO: t = "fifo"; break;
11543# endif
11544# ifdef S_IFSOCK
11545 case S_IFSOCK: t = "socket"; break;
11546# endif
11547 default: t = "other";
11548 }
11549# else
11550 if (mch_isdir(fname))
11551 t = "dir";
11552 else
11553 t = "file";
11554# endif
11555#endif
11556 type = vim_strsave((char_u *)t);
11557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011559}
11560
11561/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011563 */
11564 static void
11565f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 typval_T *argvars;
11567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011568{
11569 linenr_T lnum;
11570 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572
11573 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011574 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011575 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011576 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011577 retlist = FALSE;
11578 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011579 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582 retlist = TRUE;
11583 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011584
Bram Moolenaar342337a2005-07-21 21:11:17 +000011585 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586}
11587
11588/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011589 * "getmatches()" function
11590 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011591 static void
11592f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011593 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011594 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011595{
11596#ifdef FEAT_SEARCH_EXTRA
11597 dict_T *dict;
11598 matchitem_T *cur = curwin->w_match_head;
11599
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011600 if (rettv_list_alloc(rettv) == OK)
11601 {
11602 while (cur != NULL)
11603 {
11604 dict = dict_alloc();
11605 if (dict == NULL)
11606 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011607 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11608 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11609 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11610 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11611 list_append_dict(rettv->vval.v_list, dict);
11612 cur = cur->next;
11613 }
11614 }
11615#endif
11616}
11617
11618/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011619 * "getpid()" function
11620 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011621 static void
11622f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011623 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011624 typval_T *rettv;
11625{
11626 rettv->vval.v_number = mch_get_pid();
11627}
11628
11629/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011630 * "getpos(string)" function
11631 */
11632 static void
11633f_getpos(argvars, rettv)
11634 typval_T *argvars;
11635 typval_T *rettv;
11636{
11637 pos_T *fp;
11638 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011639 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011640
11641 if (rettv_list_alloc(rettv) == OK)
11642 {
11643 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011644 fp = var2fpos(&argvars[0], TRUE, &fnum);
11645 if (fnum != -1)
11646 list_append_number(l, (varnumber_T)fnum);
11647 else
11648 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011649 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11650 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011651 list_append_number(l, (fp != NULL)
11652 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011653 : (varnumber_T)0);
11654 list_append_number(l,
11655#ifdef FEAT_VIRTUALEDIT
11656 (fp != NULL) ? (varnumber_T)fp->coladd :
11657#endif
11658 (varnumber_T)0);
11659 }
11660 else
11661 rettv->vval.v_number = FALSE;
11662}
11663
11664/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011665 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011666 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011667 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011668f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011669 typval_T *argvars UNUSED;
11670 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011671{
11672#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011673 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011674#endif
11675
Bram Moolenaar2641f772005-03-25 21:58:17 +000011676#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011677 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011678 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011679 wp = NULL;
11680 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11681 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011682 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011683 if (wp == NULL)
11684 return;
11685 }
11686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011687 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011688 }
11689#endif
11690}
11691
11692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 * "getreg()" function
11694 */
11695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *argvars;
11698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
11700 char_u *strregname;
11701 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011702 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011705 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 strregname = get_tv_string_chk(&argvars[0]);
11708 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 regname = (strregname == NULL ? '"' : *strregname);
11715 if (regname == 0)
11716 regname = '"';
11717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011718 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 rettv->vval.v_string = error ? NULL :
11720 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721}
11722
11723/*
11724 * "getregtype()" function
11725 */
11726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727f_getregtype(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 char_u *strregname;
11732 int regname;
11733 char_u buf[NUMBUFLEN + 2];
11734 long reglen = 0;
11735
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011736 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 {
11738 strregname = get_tv_string_chk(&argvars[0]);
11739 if (strregname == NULL) /* type error; errmsg already given */
11740 {
11741 rettv->v_type = VAR_STRING;
11742 rettv->vval.v_string = NULL;
11743 return;
11744 }
11745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 else
11747 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749
11750 regname = (strregname == NULL ? '"' : *strregname);
11751 if (regname == 0)
11752 regname = '"';
11753
11754 buf[0] = NUL;
11755 buf[1] = NUL;
11756 switch (get_reg_type(regname, &reglen))
11757 {
11758 case MLINE: buf[0] = 'V'; break;
11759 case MCHAR: buf[0] = 'v'; break;
11760#ifdef FEAT_VISUAL
11761 case MBLOCK:
11762 buf[0] = Ctrl_V;
11763 sprintf((char *)buf + 1, "%ld", reglen + 1);
11764 break;
11765#endif
11766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->v_type = VAR_STRING;
11768 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769}
11770
11771/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011772 * "gettabvar()" function
11773 */
11774 static void
11775f_gettabvar(argvars, rettv)
11776 typval_T *argvars;
11777 typval_T *rettv;
11778{
11779 tabpage_T *tp;
11780 dictitem_T *v;
11781 char_u *varname;
11782
11783 rettv->v_type = VAR_STRING;
11784 rettv->vval.v_string = NULL;
11785
11786 varname = get_tv_string_chk(&argvars[1]);
11787 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11788 if (tp != NULL && varname != NULL)
11789 {
11790 /* look up the variable */
11791 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11792 if (v != NULL)
11793 copy_tv(&v->di_tv, rettv);
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011794 else if (argvars[2].v_type != VAR_UNKNOWN)
11795 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011796 }
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011797 else if (argvars[2].v_type != VAR_UNKNOWN)
11798 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011799}
11800
11801/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011802 * "gettabwinvar()" function
11803 */
11804 static void
11805f_gettabwinvar(argvars, rettv)
11806 typval_T *argvars;
11807 typval_T *rettv;
11808{
11809 getwinvar(argvars, rettv, 1);
11810}
11811
11812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 * "getwinposx()" function
11814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011817 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821#ifdef FEAT_GUI
11822 if (gui.in_use)
11823 {
11824 int x, y;
11825
11826 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 }
11829#endif
11830}
11831
11832/*
11833 * "getwinposy()" function
11834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841#ifdef FEAT_GUI
11842 if (gui.in_use)
11843 {
11844 int x, y;
11845
11846 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 }
11849#endif
11850}
11851
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011852/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011853 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011854 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011855 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011856find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011857 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011858 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011859{
11860#ifdef FEAT_WINDOWS
11861 win_T *wp;
11862#endif
11863 int nr;
11864
11865 nr = get_tv_number_chk(vp, NULL);
11866
11867#ifdef FEAT_WINDOWS
11868 if (nr < 0)
11869 return NULL;
11870 if (nr == 0)
11871 return curwin;
11872
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011873 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11874 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011875 if (--nr <= 0)
11876 break;
11877 return wp;
11878#else
11879 if (nr == 0 || nr == 1)
11880 return curwin;
11881 return NULL;
11882#endif
11883}
11884
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885/*
11886 * "getwinvar()" function
11887 */
11888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011890 typval_T *argvars;
11891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011893 getwinvar(argvars, rettv, 0);
11894}
11895
11896/*
11897 * getwinvar() and gettabwinvar()
11898 */
11899 static void
11900getwinvar(argvars, rettv, off)
11901 typval_T *argvars;
11902 typval_T *rettv;
11903 int off; /* 1 for gettabwinvar() */
11904{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 win_T *win, *oldcurwin;
11906 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011907 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011908 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011910#ifdef FEAT_WINDOWS
11911 if (off == 1)
11912 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11913 else
11914 tp = curtab;
11915#endif
11916 win = find_win_by_nr(&argvars[off], tp);
11917 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011920 if (argvars[off + 2].v_type != VAR_UNKNOWN)
11921 /* set the default return value */
11922 copy_tv(&argvars[off + 2], rettv);
11923 else
11924 {
11925 rettv->v_type = VAR_STRING;
11926 rettv->vval.v_string = NULL;
11927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928
11929 if (win != NULL && varname != NULL)
11930 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011931 /* Set curwin to be our win, temporarily. Also set curbuf, so
11932 * that we can get buffer-local options. */
11933 oldcurwin = curwin;
11934 curwin = win;
11935 curbuf = win->w_buffer;
11936
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011938 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 else
11940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941 if (*varname == NUL)
11942 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11943 * scope prefix before the NUL byte is required by
11944 * find_var_in_ht(). */
11945 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011947 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011949 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011951
11952 /* restore previous notion of curwin */
11953 curwin = oldcurwin;
11954 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 }
11956
11957 --emsg_off;
11958}
11959
11960/*
11961 * "glob()" function
11962 */
11963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011965 typval_T *argvars;
11966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011968 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011970 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011972 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011973 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011975 if (argvars[1].v_type != VAR_UNKNOWN)
11976 {
11977 if (get_tv_number_chk(&argvars[1], &error))
11978 options |= WILD_KEEP_ALL;
11979 if (argvars[2].v_type != VAR_UNKNOWN
11980 && get_tv_number_chk(&argvars[2], &error))
11981 {
11982 rettv->v_type = VAR_LIST;
11983 rettv->vval.v_list = NULL;
11984 }
11985 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011986 if (!error)
11987 {
11988 ExpandInit(&xpc);
11989 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011990 if (p_wic)
11991 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011992 if (rettv->v_type == VAR_STRING)
11993 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011994 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011995 else if (rettv_list_alloc(rettv) != FAIL)
11996 {
11997 int i;
11998
11999 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12000 NULL, options, WILD_ALL_KEEP);
12001 for (i = 0; i < xpc.xp_numfiles; i++)
12002 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12003
12004 ExpandCleanup(&xpc);
12005 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 }
12007 else
12008 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009}
12010
12011/*
12012 * "globpath()" function
12013 */
12014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012016 typval_T *argvars;
12017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012019 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012022 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012024 /* When the optional second argument is non-zero, don't remove matches
12025 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12026 if (argvars[2].v_type != VAR_UNKNOWN
12027 && get_tv_number_chk(&argvars[2], &error))
12028 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012030 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 rettv->vval.v_string = NULL;
12032 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012033 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12034 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035}
12036
12037/*
12038 * "has()" function
12039 */
12040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044{
12045 int i;
12046 char_u *name;
12047 int n = FALSE;
12048 static char *(has_list[]) =
12049 {
12050#ifdef AMIGA
12051 "amiga",
12052# ifdef FEAT_ARP
12053 "arp",
12054# endif
12055#endif
12056#ifdef __BEOS__
12057 "beos",
12058#endif
12059#ifdef MSDOS
12060# ifdef DJGPP
12061 "dos32",
12062# else
12063 "dos16",
12064# endif
12065#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012066#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 "mac",
12068#endif
12069#if defined(MACOS_X_UNIX)
12070 "macunix",
12071#endif
12072#ifdef OS2
12073 "os2",
12074#endif
12075#ifdef __QNX__
12076 "qnx",
12077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078#ifdef UNIX
12079 "unix",
12080#endif
12081#ifdef VMS
12082 "vms",
12083#endif
12084#ifdef WIN16
12085 "win16",
12086#endif
12087#ifdef WIN32
12088 "win32",
12089#endif
12090#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12091 "win32unix",
12092#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012093#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 "win64",
12095#endif
12096#ifdef EBCDIC
12097 "ebcdic",
12098#endif
12099#ifndef CASE_INSENSITIVE_FILENAME
12100 "fname_case",
12101#endif
12102#ifdef FEAT_ARABIC
12103 "arabic",
12104#endif
12105#ifdef FEAT_AUTOCMD
12106 "autocmd",
12107#endif
12108#ifdef FEAT_BEVAL
12109 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012110# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12111 "balloon_multiline",
12112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113#endif
12114#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12115 "builtin_terms",
12116# ifdef ALL_BUILTIN_TCAPS
12117 "all_builtin_terms",
12118# endif
12119#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012120#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12121 || defined(FEAT_GUI_W32) \
12122 || defined(FEAT_GUI_MOTIF))
12123 "browsefilter",
12124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125#ifdef FEAT_BYTEOFF
12126 "byte_offset",
12127#endif
12128#ifdef FEAT_CINDENT
12129 "cindent",
12130#endif
12131#ifdef FEAT_CLIENTSERVER
12132 "clientserver",
12133#endif
12134#ifdef FEAT_CLIPBOARD
12135 "clipboard",
12136#endif
12137#ifdef FEAT_CMDL_COMPL
12138 "cmdline_compl",
12139#endif
12140#ifdef FEAT_CMDHIST
12141 "cmdline_hist",
12142#endif
12143#ifdef FEAT_COMMENTS
12144 "comments",
12145#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012146#ifdef FEAT_CONCEAL
12147 "conceal",
12148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149#ifdef FEAT_CRYPT
12150 "cryptv",
12151#endif
12152#ifdef FEAT_CSCOPE
12153 "cscope",
12154#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012155#ifdef FEAT_CURSORBIND
12156 "cursorbind",
12157#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012158#ifdef CURSOR_SHAPE
12159 "cursorshape",
12160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161#ifdef DEBUG
12162 "debug",
12163#endif
12164#ifdef FEAT_CON_DIALOG
12165 "dialog_con",
12166#endif
12167#ifdef FEAT_GUI_DIALOG
12168 "dialog_gui",
12169#endif
12170#ifdef FEAT_DIFF
12171 "diff",
12172#endif
12173#ifdef FEAT_DIGRAPHS
12174 "digraphs",
12175#endif
12176#ifdef FEAT_DND
12177 "dnd",
12178#endif
12179#ifdef FEAT_EMACS_TAGS
12180 "emacs_tags",
12181#endif
12182 "eval", /* always present, of course! */
12183#ifdef FEAT_EX_EXTRA
12184 "ex_extra",
12185#endif
12186#ifdef FEAT_SEARCH_EXTRA
12187 "extra_search",
12188#endif
12189#ifdef FEAT_FKMAP
12190 "farsi",
12191#endif
12192#ifdef FEAT_SEARCHPATH
12193 "file_in_path",
12194#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012195#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012196 "filterpipe",
12197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198#ifdef FEAT_FIND_ID
12199 "find_in_path",
12200#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012201#ifdef FEAT_FLOAT
12202 "float",
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef FEAT_FOLDING
12205 "folding",
12206#endif
12207#ifdef FEAT_FOOTER
12208 "footer",
12209#endif
12210#if !defined(USE_SYSTEM) && defined(UNIX)
12211 "fork",
12212#endif
12213#ifdef FEAT_GETTEXT
12214 "gettext",
12215#endif
12216#ifdef FEAT_GUI
12217 "gui",
12218#endif
12219#ifdef FEAT_GUI_ATHENA
12220# ifdef FEAT_GUI_NEXTAW
12221 "gui_neXtaw",
12222# else
12223 "gui_athena",
12224# endif
12225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226#ifdef FEAT_GUI_GTK
12227 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012230#ifdef FEAT_GUI_GNOME
12231 "gui_gnome",
12232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233#ifdef FEAT_GUI_MAC
12234 "gui_mac",
12235#endif
12236#ifdef FEAT_GUI_MOTIF
12237 "gui_motif",
12238#endif
12239#ifdef FEAT_GUI_PHOTON
12240 "gui_photon",
12241#endif
12242#ifdef FEAT_GUI_W16
12243 "gui_win16",
12244#endif
12245#ifdef FEAT_GUI_W32
12246 "gui_win32",
12247#endif
12248#ifdef FEAT_HANGULIN
12249 "hangul_input",
12250#endif
12251#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12252 "iconv",
12253#endif
12254#ifdef FEAT_INS_EXPAND
12255 "insert_expand",
12256#endif
12257#ifdef FEAT_JUMPLIST
12258 "jumplist",
12259#endif
12260#ifdef FEAT_KEYMAP
12261 "keymap",
12262#endif
12263#ifdef FEAT_LANGMAP
12264 "langmap",
12265#endif
12266#ifdef FEAT_LIBCALL
12267 "libcall",
12268#endif
12269#ifdef FEAT_LINEBREAK
12270 "linebreak",
12271#endif
12272#ifdef FEAT_LISP
12273 "lispindent",
12274#endif
12275#ifdef FEAT_LISTCMDS
12276 "listcmds",
12277#endif
12278#ifdef FEAT_LOCALMAP
12279 "localmap",
12280#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012281#ifdef FEAT_LUA
12282# ifndef DYNAMIC_LUA
12283 "lua",
12284# endif
12285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286#ifdef FEAT_MENU
12287 "menu",
12288#endif
12289#ifdef FEAT_SESSION
12290 "mksession",
12291#endif
12292#ifdef FEAT_MODIFY_FNAME
12293 "modify_fname",
12294#endif
12295#ifdef FEAT_MOUSE
12296 "mouse",
12297#endif
12298#ifdef FEAT_MOUSESHAPE
12299 "mouseshape",
12300#endif
12301#if defined(UNIX) || defined(VMS)
12302# ifdef FEAT_MOUSE_DEC
12303 "mouse_dec",
12304# endif
12305# ifdef FEAT_MOUSE_GPM
12306 "mouse_gpm",
12307# endif
12308# ifdef FEAT_MOUSE_JSB
12309 "mouse_jsbterm",
12310# endif
12311# ifdef FEAT_MOUSE_NET
12312 "mouse_netterm",
12313# endif
12314# ifdef FEAT_MOUSE_PTERM
12315 "mouse_pterm",
12316# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012317# ifdef FEAT_MOUSE_SGR
12318 "mouse_sgr",
12319# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012320# ifdef FEAT_SYSMOUSE
12321 "mouse_sysmouse",
12322# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012323# ifdef FEAT_MOUSE_URXVT
12324 "mouse_urxvt",
12325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326# ifdef FEAT_MOUSE_XTERM
12327 "mouse_xterm",
12328# endif
12329#endif
12330#ifdef FEAT_MBYTE
12331 "multi_byte",
12332#endif
12333#ifdef FEAT_MBYTE_IME
12334 "multi_byte_ime",
12335#endif
12336#ifdef FEAT_MULTI_LANG
12337 "multi_lang",
12338#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012339#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012340#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012341 "mzscheme",
12342#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344#ifdef FEAT_OLE
12345 "ole",
12346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347#ifdef FEAT_PATH_EXTRA
12348 "path_extra",
12349#endif
12350#ifdef FEAT_PERL
12351#ifndef DYNAMIC_PERL
12352 "perl",
12353#endif
12354#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012355#ifdef FEAT_PERSISTENT_UNDO
12356 "persistent_undo",
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_PYTHON
12359#ifndef DYNAMIC_PYTHON
12360 "python",
12361#endif
12362#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012363#ifdef FEAT_PYTHON3
12364#ifndef DYNAMIC_PYTHON3
12365 "python3",
12366#endif
12367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368#ifdef FEAT_POSTSCRIPT
12369 "postscript",
12370#endif
12371#ifdef FEAT_PRINTER
12372 "printer",
12373#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012374#ifdef FEAT_PROFILE
12375 "profile",
12376#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012377#ifdef FEAT_RELTIME
12378 "reltime",
12379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380#ifdef FEAT_QUICKFIX
12381 "quickfix",
12382#endif
12383#ifdef FEAT_RIGHTLEFT
12384 "rightleft",
12385#endif
12386#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12387 "ruby",
12388#endif
12389#ifdef FEAT_SCROLLBIND
12390 "scrollbind",
12391#endif
12392#ifdef FEAT_CMDL_INFO
12393 "showcmd",
12394 "cmdline_info",
12395#endif
12396#ifdef FEAT_SIGNS
12397 "signs",
12398#endif
12399#ifdef FEAT_SMARTINDENT
12400 "smartindent",
12401#endif
12402#ifdef FEAT_SNIFF
12403 "sniff",
12404#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012405#ifdef STARTUPTIME
12406 "startuptime",
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_STL_OPT
12409 "statusline",
12410#endif
12411#ifdef FEAT_SUN_WORKSHOP
12412 "sun_workshop",
12413#endif
12414#ifdef FEAT_NETBEANS_INTG
12415 "netbeans_intg",
12416#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012417#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012418 "spell",
12419#endif
12420#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 "syntax",
12422#endif
12423#if defined(USE_SYSTEM) || !defined(UNIX)
12424 "system",
12425#endif
12426#ifdef FEAT_TAG_BINS
12427 "tag_binary",
12428#endif
12429#ifdef FEAT_TAG_OLDSTATIC
12430 "tag_old_static",
12431#endif
12432#ifdef FEAT_TAG_ANYWHITE
12433 "tag_any_white",
12434#endif
12435#ifdef FEAT_TCL
12436# ifndef DYNAMIC_TCL
12437 "tcl",
12438# endif
12439#endif
12440#ifdef TERMINFO
12441 "terminfo",
12442#endif
12443#ifdef FEAT_TERMRESPONSE
12444 "termresponse",
12445#endif
12446#ifdef FEAT_TEXTOBJ
12447 "textobjects",
12448#endif
12449#ifdef HAVE_TGETENT
12450 "tgetent",
12451#endif
12452#ifdef FEAT_TITLE
12453 "title",
12454#endif
12455#ifdef FEAT_TOOLBAR
12456 "toolbar",
12457#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012458#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12459 "unnamedplus",
12460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461#ifdef FEAT_USR_CMDS
12462 "user-commands", /* was accidentally included in 5.4 */
12463 "user_commands",
12464#endif
12465#ifdef FEAT_VIMINFO
12466 "viminfo",
12467#endif
12468#ifdef FEAT_VERTSPLIT
12469 "vertsplit",
12470#endif
12471#ifdef FEAT_VIRTUALEDIT
12472 "virtualedit",
12473#endif
12474#ifdef FEAT_VISUAL
12475 "visual",
12476#endif
12477#ifdef FEAT_VISUALEXTRA
12478 "visualextra",
12479#endif
12480#ifdef FEAT_VREPLACE
12481 "vreplace",
12482#endif
12483#ifdef FEAT_WILDIGN
12484 "wildignore",
12485#endif
12486#ifdef FEAT_WILDMENU
12487 "wildmenu",
12488#endif
12489#ifdef FEAT_WINDOWS
12490 "windows",
12491#endif
12492#ifdef FEAT_WAK
12493 "winaltkeys",
12494#endif
12495#ifdef FEAT_WRITEBACKUP
12496 "writebackup",
12497#endif
12498#ifdef FEAT_XIM
12499 "xim",
12500#endif
12501#ifdef FEAT_XFONTSET
12502 "xfontset",
12503#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012504#ifdef FEAT_XPM_W32
12505 "xpm_w32",
12506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507#ifdef USE_XSMP
12508 "xsmp",
12509#endif
12510#ifdef USE_XSMP_INTERACT
12511 "xsmp_interact",
12512#endif
12513#ifdef FEAT_XCLIPBOARD
12514 "xterm_clipboard",
12515#endif
12516#ifdef FEAT_XTERM_SAVE
12517 "xterm_save",
12518#endif
12519#if defined(UNIX) && defined(FEAT_X11)
12520 "X11",
12521#endif
12522 NULL
12523 };
12524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 for (i = 0; has_list[i] != NULL; ++i)
12527 if (STRICMP(name, has_list[i]) == 0)
12528 {
12529 n = TRUE;
12530 break;
12531 }
12532
12533 if (n == FALSE)
12534 {
12535 if (STRNICMP(name, "patch", 5) == 0)
12536 n = has_patch(atoi((char *)name + 5));
12537 else if (STRICMP(name, "vim_starting") == 0)
12538 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012539#ifdef FEAT_MBYTE
12540 else if (STRICMP(name, "multi_byte_encoding") == 0)
12541 n = has_mbyte;
12542#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012543#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12544 else if (STRICMP(name, "balloon_multiline") == 0)
12545 n = multiline_balloon_available();
12546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547#ifdef DYNAMIC_TCL
12548 else if (STRICMP(name, "tcl") == 0)
12549 n = tcl_enabled(FALSE);
12550#endif
12551#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12552 else if (STRICMP(name, "iconv") == 0)
12553 n = iconv_enabled(FALSE);
12554#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012555#ifdef DYNAMIC_LUA
12556 else if (STRICMP(name, "lua") == 0)
12557 n = lua_enabled(FALSE);
12558#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012559#ifdef DYNAMIC_MZSCHEME
12560 else if (STRICMP(name, "mzscheme") == 0)
12561 n = mzscheme_enabled(FALSE);
12562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563#ifdef DYNAMIC_RUBY
12564 else if (STRICMP(name, "ruby") == 0)
12565 n = ruby_enabled(FALSE);
12566#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012567#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568#ifdef DYNAMIC_PYTHON
12569 else if (STRICMP(name, "python") == 0)
12570 n = python_enabled(FALSE);
12571#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012572#endif
12573#ifdef FEAT_PYTHON3
12574#ifdef DYNAMIC_PYTHON3
12575 else if (STRICMP(name, "python3") == 0)
12576 n = python3_enabled(FALSE);
12577#endif
12578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579#ifdef DYNAMIC_PERL
12580 else if (STRICMP(name, "perl") == 0)
12581 n = perl_enabled(FALSE);
12582#endif
12583#ifdef FEAT_GUI
12584 else if (STRICMP(name, "gui_running") == 0)
12585 n = (gui.in_use || gui.starting);
12586# ifdef FEAT_GUI_W32
12587 else if (STRICMP(name, "gui_win32s") == 0)
12588 n = gui_is_win32s();
12589# endif
12590# ifdef FEAT_BROWSE
12591 else if (STRICMP(name, "browse") == 0)
12592 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12593# endif
12594#endif
12595#ifdef FEAT_SYN_HL
12596 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012597 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598#endif
12599#if defined(WIN3264)
12600 else if (STRICMP(name, "win95") == 0)
12601 n = mch_windows95();
12602#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012603#ifdef FEAT_NETBEANS_INTG
12604 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012605 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 }
12608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610}
12611
12612/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012613 * "has_key()" function
12614 */
12615 static void
12616f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012617 typval_T *argvars;
12618 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012619{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012620 if (argvars[0].v_type != VAR_DICT)
12621 {
12622 EMSG(_(e_dictreq));
12623 return;
12624 }
12625 if (argvars[0].vval.v_dict == NULL)
12626 return;
12627
12628 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012629 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012630}
12631
12632/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012633 * "haslocaldir()" function
12634 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012635 static void
12636f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012637 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012638 typval_T *rettv;
12639{
12640 rettv->vval.v_number = (curwin->w_localdir != NULL);
12641}
12642
12643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 * "hasmapto()" function
12645 */
12646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 typval_T *argvars;
12649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650{
12651 char_u *name;
12652 char_u *mode;
12653 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012654 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012657 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 mode = (char_u *)"nvo";
12659 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012662 if (argvars[2].v_type != VAR_UNKNOWN)
12663 abbr = get_tv_number(&argvars[2]);
12664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
Bram Moolenaar2c932302006-03-18 21:42:09 +000012666 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012669 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670}
12671
12672/*
12673 * "histadd()" function
12674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679{
12680#ifdef FEAT_CMDHIST
12681 int histype;
12682 char_u *str;
12683 char_u buf[NUMBUFLEN];
12684#endif
12685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 if (check_restricted() || check_secure())
12688 return;
12689#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012690 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12691 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 if (histype >= 0)
12693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 if (*str != NUL)
12696 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012697 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 return;
12701 }
12702 }
12703#endif
12704}
12705
12706/*
12707 * "histdel()" function
12708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012711 typval_T *argvars UNUSED;
12712 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
12714#ifdef FEAT_CMDHIST
12715 int n;
12716 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12720 if (str == NULL)
12721 n = 0;
12722 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012725 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 else
12730 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 get_tv_string_buf(&argvars[1], buf));
12733 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734#endif
12735}
12736
12737/*
12738 * "histget()" function
12739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012742 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744{
12745#ifdef FEAT_CMDHIST
12746 int type;
12747 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12751 if (str == NULL)
12752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 {
12755 type = get_histtype(str);
12756 if (argvars[1].v_type == VAR_UNKNOWN)
12757 idx = get_history_idx(type);
12758 else
12759 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12760 /* -1 on type error */
12761 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767}
12768
12769/*
12770 * "histnr()" function
12771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012774 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776{
12777 int i;
12778
12779#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012780 char_u *history = get_tv_string_chk(&argvars[0]);
12781
12782 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 if (i >= HIST_CMD && i < HIST_COUNT)
12784 i = get_history_idx(i);
12785 else
12786#endif
12787 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789}
12790
12791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 * "highlightID(name)" function
12793 */
12794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012796 typval_T *argvars;
12797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800}
12801
12802/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012803 * "highlight_exists()" function
12804 */
12805 static void
12806f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012807 typval_T *argvars;
12808 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012809{
12810 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12811}
12812
12813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 * "hostname()" function
12815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820{
12821 char_u hostname[256];
12822
12823 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 rettv->v_type = VAR_STRING;
12825 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826}
12827
12828/*
12829 * iconv() function
12830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012833 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835{
12836#ifdef FEAT_MBYTE
12837 char_u buf1[NUMBUFLEN];
12838 char_u buf2[NUMBUFLEN];
12839 char_u *from, *to, *str;
12840 vimconv_T vimconv;
12841#endif
12842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->v_type = VAR_STRING;
12844 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845
12846#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 str = get_tv_string(&argvars[0]);
12848 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12849 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 vimconv.vc_type = CONV_NONE;
12851 convert_setup(&vimconv, from, to);
12852
12853 /* If the encodings are equal, no conversion needed. */
12854 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858
12859 convert_setup(&vimconv, NULL, NULL);
12860 vim_free(from);
12861 vim_free(to);
12862#endif
12863}
12864
12865/*
12866 * "indent()" function
12867 */
12868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012870 typval_T *argvars;
12871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872{
12873 linenr_T lnum;
12874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012882/*
12883 * "index()" function
12884 */
12885 static void
12886f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012889{
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 list_T *l;
12891 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012892 long idx = 0;
12893 int ic = FALSE;
12894
12895 rettv->vval.v_number = -1;
12896 if (argvars[0].v_type != VAR_LIST)
12897 {
12898 EMSG(_(e_listreq));
12899 return;
12900 }
12901 l = argvars[0].vval.v_list;
12902 if (l != NULL)
12903 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012904 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012905 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 int error = FALSE;
12908
Bram Moolenaar758711c2005-02-02 23:11:38 +000012909 /* Start at specified item. Use the cached index that list_find()
12910 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012911 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012912 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012913 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012914 ic = get_tv_number_chk(&argvars[3], &error);
12915 if (error)
12916 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012917 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012918
Bram Moolenaar758711c2005-02-02 23:11:38 +000012919 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012920 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012921 {
12922 rettv->vval.v_number = idx;
12923 break;
12924 }
12925 }
12926}
12927
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928static int inputsecret_flag = 0;
12929
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012930static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12931
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012933 * This function is used by f_input() and f_inputdialog() functions. The third
12934 * argument to f_input() specifies the type of completion to use at the
12935 * prompt. The third argument to f_inputdialog() specifies the value to return
12936 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 */
12938 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012939get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012940 typval_T *argvars;
12941 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012942 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012944 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 char_u *p = NULL;
12946 int c;
12947 char_u buf[NUMBUFLEN];
12948 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012949 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012950 int xp_type = EXPAND_NOTHING;
12951 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012954 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955
12956#ifdef NO_CONSOLE_INPUT
12957 /* While starting up, there is no place to enter text. */
12958 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960#endif
12961
12962 cmd_silent = FALSE; /* Want to see the prompt. */
12963 if (prompt != NULL)
12964 {
12965 /* Only the part of the message after the last NL is considered as
12966 * prompt for the command line */
12967 p = vim_strrchr(prompt, '\n');
12968 if (p == NULL)
12969 p = prompt;
12970 else
12971 {
12972 ++p;
12973 c = *p;
12974 *p = NUL;
12975 msg_start();
12976 msg_clr_eos();
12977 msg_puts_attr(prompt, echo_attr);
12978 msg_didout = FALSE;
12979 msg_starthere();
12980 *p = c;
12981 }
12982 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012984 if (argvars[1].v_type != VAR_UNKNOWN)
12985 {
12986 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12987 if (defstr != NULL)
12988 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012990 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012991 {
12992 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012993 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012994 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012995
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012996 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012997 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012998
Bram Moolenaar4463f292005-09-25 22:20:24 +000012999 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13000 if (xp_name == NULL)
13001 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013002
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013003 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013004
Bram Moolenaar4463f292005-09-25 22:20:24 +000013005 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13006 &xp_arg) == FAIL)
13007 return;
13008 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013009 }
13010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013011 if (defstr != NULL)
13012 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013013 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13014 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013015 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013016 && argvars[1].v_type != VAR_UNKNOWN
13017 && argvars[2].v_type != VAR_UNKNOWN)
13018 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13019 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013020
13021 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013023 /* since the user typed this, no need to wait for return */
13024 need_wait_return = FALSE;
13025 msg_didout = FALSE;
13026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 cmd_silent = cmd_silent_save;
13028}
13029
13030/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013031 * "input()" function
13032 * Also handles inputsecret() when inputsecret is set.
13033 */
13034 static void
13035f_input(argvars, rettv)
13036 typval_T *argvars;
13037 typval_T *rettv;
13038{
13039 get_user_input(argvars, rettv, FALSE);
13040}
13041
13042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 * "inputdialog()" function
13044 */
13045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013047 typval_T *argvars;
13048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049{
13050#if defined(FEAT_GUI_TEXTDIALOG)
13051 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13052 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13053 {
13054 char_u *message;
13055 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 message = get_tv_string_chk(&argvars[0]);
13059 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013060 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013061 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 else
13063 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 if (message != NULL && defstr != NULL
13065 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013066 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013067 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 else
13069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 if (message != NULL && defstr != NULL
13071 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013072 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->vval.v_string = vim_strsave(
13074 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 }
13080 else
13081#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013082 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083}
13084
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013085/*
13086 * "inputlist()" function
13087 */
13088 static void
13089f_inputlist(argvars, rettv)
13090 typval_T *argvars;
13091 typval_T *rettv;
13092{
13093 listitem_T *li;
13094 int selected;
13095 int mouse_used;
13096
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013097#ifdef NO_CONSOLE_INPUT
13098 /* While starting up, there is no place to enter text. */
13099 if (no_console_input())
13100 return;
13101#endif
13102 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13103 {
13104 EMSG2(_(e_listarg), "inputlist()");
13105 return;
13106 }
13107
13108 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013109 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013110 lines_left = Rows; /* avoid more prompt */
13111 msg_scroll = TRUE;
13112 msg_clr_eos();
13113
13114 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13115 {
13116 msg_puts(get_tv_string(&li->li_tv));
13117 msg_putchar('\n');
13118 }
13119
13120 /* Ask for choice. */
13121 selected = prompt_for_number(&mouse_used);
13122 if (mouse_used)
13123 selected -= lines_left;
13124
13125 rettv->vval.v_number = selected;
13126}
13127
13128
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13130
13131/*
13132 * "inputrestore()" function
13133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138{
13139 if (ga_userinput.ga_len > 0)
13140 {
13141 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13143 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013144 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145 }
13146 else if (p_verbose > 1)
13147 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013148 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 }
13151}
13152
13153/*
13154 * "inputsave()" function
13155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013157f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013158 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013161 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 if (ga_grow(&ga_userinput, 1) == OK)
13163 {
13164 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13165 + ga_userinput.ga_len);
13166 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013167 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 }
13169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171}
13172
13173/*
13174 * "inputsecret()" function
13175 */
13176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 typval_T *argvars;
13179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180{
13181 ++cmdline_star;
13182 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013183 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 --cmdline_star;
13185 --inputsecret_flag;
13186}
13187
13188/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189 * "insert()" function
13190 */
13191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013193 typval_T *argvars;
13194 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195{
13196 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 listitem_T *item;
13198 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200
13201 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013202 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013203 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013204 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 {
13206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 before = get_tv_number_chk(&argvars[2], &error);
13208 if (error)
13209 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210
Bram Moolenaar758711c2005-02-02 23:11:38 +000013211 if (before == l->lv_len)
13212 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 else
13214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013215 item = list_find(l, before);
13216 if (item == NULL)
13217 {
13218 EMSGN(_(e_listidx), before);
13219 l = NULL;
13220 }
13221 }
13222 if (l != NULL)
13223 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013224 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013225 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013226 }
13227 }
13228}
13229
13230/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013231 * "invert(expr)" function
13232 */
13233 static void
13234f_invert(argvars, rettv)
13235 typval_T *argvars;
13236 typval_T *rettv;
13237{
13238 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13239}
13240
13241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 * "isdirectory()" function
13243 */
13244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013246 typval_T *argvars;
13247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250}
13251
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013252/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013253 * "islocked()" function
13254 */
13255 static void
13256f_islocked(argvars, rettv)
13257 typval_T *argvars;
13258 typval_T *rettv;
13259{
13260 lval_T lv;
13261 char_u *end;
13262 dictitem_T *di;
13263
13264 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013265 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13266 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013267 if (end != NULL && lv.ll_name != NULL)
13268 {
13269 if (*end != NUL)
13270 EMSG(_(e_trailing));
13271 else
13272 {
13273 if (lv.ll_tv == NULL)
13274 {
13275 if (check_changedtick(lv.ll_name))
13276 rettv->vval.v_number = 1; /* always locked */
13277 else
13278 {
13279 di = find_var(lv.ll_name, NULL);
13280 if (di != NULL)
13281 {
13282 /* Consider a variable locked when:
13283 * 1. the variable itself is locked
13284 * 2. the value of the variable is locked.
13285 * 3. the List or Dict value is locked.
13286 */
13287 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13288 || tv_islocked(&di->di_tv));
13289 }
13290 }
13291 }
13292 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013293 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013294 else if (lv.ll_newkey != NULL)
13295 EMSG2(_(e_dictkey), lv.ll_newkey);
13296 else if (lv.ll_list != NULL)
13297 /* List item. */
13298 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13299 else
13300 /* Dictionary item. */
13301 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13302 }
13303 }
13304
13305 clear_lval(&lv);
13306}
13307
Bram Moolenaar33570922005-01-25 22:26:29 +000013308static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013309
13310/*
13311 * Turn a dict into a list:
13312 * "what" == 0: list of keys
13313 * "what" == 1: list of values
13314 * "what" == 2: list of items
13315 */
13316 static void
13317dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013318 typval_T *argvars;
13319 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013320 int what;
13321{
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 list_T *l2;
13323 dictitem_T *di;
13324 hashitem_T *hi;
13325 listitem_T *li;
13326 listitem_T *li2;
13327 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013328 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013329
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330 if (argvars[0].v_type != VAR_DICT)
13331 {
13332 EMSG(_(e_dictreq));
13333 return;
13334 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013335 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013336 return;
13337
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013338 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013339 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013340
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013341 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013342 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013344 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013346 --todo;
13347 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013348
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013349 li = listitem_alloc();
13350 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013351 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013352 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013354 if (what == 0)
13355 {
13356 /* keys() */
13357 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013358 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013359 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13360 }
13361 else if (what == 1)
13362 {
13363 /* values() */
13364 copy_tv(&di->di_tv, &li->li_tv);
13365 }
13366 else
13367 {
13368 /* items() */
13369 l2 = list_alloc();
13370 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013371 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013372 li->li_tv.vval.v_list = l2;
13373 if (l2 == NULL)
13374 break;
13375 ++l2->lv_refcount;
13376
13377 li2 = listitem_alloc();
13378 if (li2 == NULL)
13379 break;
13380 list_append(l2, li2);
13381 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013382 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013383 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13384
13385 li2 = listitem_alloc();
13386 if (li2 == NULL)
13387 break;
13388 list_append(l2, li2);
13389 copy_tv(&di->di_tv, &li2->li_tv);
13390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013391 }
13392 }
13393}
13394
13395/*
13396 * "items(dict)" function
13397 */
13398 static void
13399f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 typval_T *argvars;
13401 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013402{
13403 dict_list(argvars, rettv, 2);
13404}
13405
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013407 * "join()" function
13408 */
13409 static void
13410f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013411 typval_T *argvars;
13412 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013413{
13414 garray_T ga;
13415 char_u *sep;
13416
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013417 if (argvars[0].v_type != VAR_LIST)
13418 {
13419 EMSG(_(e_listreq));
13420 return;
13421 }
13422 if (argvars[0].vval.v_list == NULL)
13423 return;
13424 if (argvars[1].v_type == VAR_UNKNOWN)
13425 sep = (char_u *)" ";
13426 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013428
13429 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430
13431 if (sep != NULL)
13432 {
13433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013434 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 ga_append(&ga, NUL);
13436 rettv->vval.v_string = (char_u *)ga.ga_data;
13437 }
13438 else
13439 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013440}
13441
13442/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013443 * "keys()" function
13444 */
13445 static void
13446f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 typval_T *argvars;
13448 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013449{
13450 dict_list(argvars, rettv, 0);
13451}
13452
13453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 * "last_buffer_nr()" function.
13455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460{
13461 int n = 0;
13462 buf_T *buf;
13463
13464 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13465 if (n < buf->b_fnum)
13466 n = buf->b_fnum;
13467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013468 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013469}
13470
13471/*
13472 * "len()" function
13473 */
13474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013476 typval_T *argvars;
13477 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013478{
13479 switch (argvars[0].v_type)
13480 {
13481 case VAR_STRING:
13482 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->vval.v_number = (varnumber_T)STRLEN(
13484 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485 break;
13486 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013488 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013489 case VAR_DICT:
13490 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13491 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013493 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 break;
13495 }
13496}
13497
Bram Moolenaar33570922005-01-25 22:26:29 +000013498static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013499
13500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013502 typval_T *argvars;
13503 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013504 int type;
13505{
13506#ifdef FEAT_LIBCALL
13507 char_u *string_in;
13508 char_u **string_result;
13509 int nr_result;
13510#endif
13511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013513 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515
13516 if (check_restricted() || check_secure())
13517 return;
13518
13519#ifdef FEAT_LIBCALL
13520 /* The first two args must be strings, otherwise its meaningless */
13521 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13522 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013523 string_in = NULL;
13524 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013525 string_in = argvars[2].vval.v_string;
13526 if (type == VAR_NUMBER)
13527 string_result = NULL;
13528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013530 if (mch_libcall(argvars[0].vval.v_string,
13531 argvars[1].vval.v_string,
13532 string_in,
13533 argvars[2].vval.v_number,
13534 string_result,
13535 &nr_result) == OK
13536 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013538 }
13539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540}
13541
13542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013543 * "libcall()" function
13544 */
13545 static void
13546f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013547 typval_T *argvars;
13548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549{
13550 libcall_common(argvars, rettv, VAR_STRING);
13551}
13552
13553/*
13554 * "libcallnr()" function
13555 */
13556 static void
13557f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 typval_T *argvars;
13559 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013560{
13561 libcall_common(argvars, rettv, VAR_NUMBER);
13562}
13563
13564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 * "line(string)" function
13566 */
13567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571{
13572 linenr_T lnum = 0;
13573 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013574 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013576 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 if (fp != NULL)
13578 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580}
13581
13582/*
13583 * "line2byte(lnum)" function
13584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013587 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589{
13590#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592#else
13593 linenr_T lnum;
13594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13600 if (rettv->vval.v_number >= 0)
13601 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602#endif
13603}
13604
13605/*
13606 * "lispindent(lnum)" function
13607 */
13608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612{
13613#ifdef FEAT_LISP
13614 pos_T pos;
13615 linenr_T lnum;
13616
13617 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13620 {
13621 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 curwin->w_cursor = pos;
13624 }
13625 else
13626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628}
13629
13630/*
13631 * "localtime()" function
13632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013638 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639}
13640
Bram Moolenaar33570922005-01-25 22:26:29 +000013641static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642
13643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013645 typval_T *argvars;
13646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 int exact;
13648{
13649 char_u *keys;
13650 char_u *which;
13651 char_u buf[NUMBUFLEN];
13652 char_u *keys_buf = NULL;
13653 char_u *rhs;
13654 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013655 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013656 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013657 mapblock_T *mp;
13658 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659
13660 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 rettv->v_type = VAR_STRING;
13662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 if (*keys == NUL)
13666 return;
13667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013668 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013672 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013673 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013674 if (argvars[3].v_type != VAR_UNKNOWN)
13675 get_dict = get_tv_number(&argvars[3]);
13676 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 else
13679 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 if (which == NULL)
13681 return;
13682
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 mode = get_map_mode(&which, 0);
13684
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013685 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013686 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013688
13689 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013691 /* Return a string. */
13692 if (rhs != NULL)
13693 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694
Bram Moolenaarbd743252010-10-20 21:23:33 +020013695 }
13696 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13697 {
13698 /* Return a dictionary. */
13699 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13700 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13701 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaarbd743252010-10-20 21:23:33 +020013703 dict_add_nr_str(dict, "lhs", 0L, lhs);
13704 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13705 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13706 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13707 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13708 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13709 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13710 dict_add_nr_str(dict, "mode", 0L, mapmode);
13711
13712 vim_free(lhs);
13713 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 }
13715}
13716
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013717#ifdef FEAT_FLOAT
13718/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013719 * "log()" function
13720 */
13721 static void
13722f_log(argvars, rettv)
13723 typval_T *argvars;
13724 typval_T *rettv;
13725{
13726 float_T f;
13727
13728 rettv->v_type = VAR_FLOAT;
13729 if (get_float_arg(argvars, &f) == OK)
13730 rettv->vval.v_float = log(f);
13731 else
13732 rettv->vval.v_float = 0.0;
13733}
13734
13735/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013736 * "log10()" function
13737 */
13738 static void
13739f_log10(argvars, rettv)
13740 typval_T *argvars;
13741 typval_T *rettv;
13742{
13743 float_T f;
13744
13745 rettv->v_type = VAR_FLOAT;
13746 if (get_float_arg(argvars, &f) == OK)
13747 rettv->vval.v_float = log10(f);
13748 else
13749 rettv->vval.v_float = 0.0;
13750}
13751#endif
13752
Bram Moolenaar1dced572012-04-05 16:54:08 +020013753#ifdef FEAT_LUA
13754/*
13755 * "luaeval()" function
13756 */
13757 static void
13758f_luaeval(argvars, rettv)
13759 typval_T *argvars;
13760 typval_T *rettv;
13761{
13762 char_u *str;
13763 char_u buf[NUMBUFLEN];
13764
13765 str = get_tv_string_buf(&argvars[0], buf);
13766 do_luaeval(str, argvars + 1, rettv);
13767}
13768#endif
13769
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013771 * "map()" function
13772 */
13773 static void
13774f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013775 typval_T *argvars;
13776 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013777{
13778 filter_map(argvars, rettv, TRUE);
13779}
13780
13781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013782 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 */
13784 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013786 typval_T *argvars;
13787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790}
13791
13792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 */
13795 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013796f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801}
13802
Bram Moolenaar33570922005-01-25 22:26:29 +000013803static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804
13805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 int type;
13810{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013811 char_u *str = NULL;
13812 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 char_u *pat;
13814 regmatch_T regmatch;
13815 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013816 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 char_u *save_cpo;
13818 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013819 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013820 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013821 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013822 list_T *l = NULL;
13823 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013824 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013825 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826
13827 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13828 save_cpo = p_cpo;
13829 p_cpo = (char_u *)"";
13830
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013831 rettv->vval.v_number = -1;
13832 if (type == 3)
13833 {
13834 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013835 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013836 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013837 }
13838 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013840 rettv->v_type = VAR_STRING;
13841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013844 if (argvars[0].v_type == VAR_LIST)
13845 {
13846 if ((l = argvars[0].vval.v_list) == NULL)
13847 goto theend;
13848 li = l->lv_first;
13849 }
13850 else
13851 expr = str = get_tv_string(&argvars[0]);
13852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013853 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13854 if (pat == NULL)
13855 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013856
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013857 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 int error = FALSE;
13860
13861 start = get_tv_number_chk(&argvars[2], &error);
13862 if (error)
13863 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013864 if (l != NULL)
13865 {
13866 li = list_find(l, start);
13867 if (li == NULL)
13868 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013869 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013870 }
13871 else
13872 {
13873 if (start < 0)
13874 start = 0;
13875 if (start > (long)STRLEN(str))
13876 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013877 /* When "count" argument is there ignore matches before "start",
13878 * otherwise skip part of the string. Differs when pattern is "^"
13879 * or "\<". */
13880 if (argvars[3].v_type != VAR_UNKNOWN)
13881 startcol = start;
13882 else
13883 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013884 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013886 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 nth = get_tv_number_chk(&argvars[3], &error);
13888 if (error)
13889 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 }
13891
13892 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13893 if (regmatch.regprog != NULL)
13894 {
13895 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013896
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013897 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013898 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013899 if (l != NULL)
13900 {
13901 if (li == NULL)
13902 {
13903 match = FALSE;
13904 break;
13905 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013906 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013907 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013908 if (str == NULL)
13909 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013910 }
13911
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013912 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013913
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013914 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013915 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013916 if (l == NULL && !match)
13917 break;
13918
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013919 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 if (l != NULL)
13921 {
13922 li = li->li_next;
13923 ++idx;
13924 }
13925 else
13926 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013927#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013928 startcol = (colnr_T)(regmatch.startp[0]
13929 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013930#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013931 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013932#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013933 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013934 }
13935
13936 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013938 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013940 int i;
13941
13942 /* return list with matched string and submatches */
13943 for (i = 0; i < NSUBEXP; ++i)
13944 {
13945 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013946 {
13947 if (list_append_string(rettv->vval.v_list,
13948 (char_u *)"", 0) == FAIL)
13949 break;
13950 }
13951 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013952 regmatch.startp[i],
13953 (int)(regmatch.endp[i] - regmatch.startp[i]))
13954 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013955 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013956 }
13957 }
13958 else if (type == 2)
13959 {
13960 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013961 if (l != NULL)
13962 copy_tv(&li->li_tv, rettv);
13963 else
13964 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966 }
13967 else if (l != NULL)
13968 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 else
13970 {
13971 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 (varnumber_T)(regmatch.startp[0] - str);
13974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013977 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 }
13979 }
13980 vim_free(regmatch.regprog);
13981 }
13982
13983theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013984 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 p_cpo = save_cpo;
13986}
13987
13988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013989 * "match()" function
13990 */
13991 static void
13992f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 typval_T *argvars;
13994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995{
13996 find_some_match(argvars, rettv, 1);
13997}
13998
13999/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014000 * "matchadd()" function
14001 */
14002 static void
14003f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014004 typval_T *argvars UNUSED;
14005 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014006{
14007#ifdef FEAT_SEARCH_EXTRA
14008 char_u buf[NUMBUFLEN];
14009 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14010 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14011 int prio = 10; /* default priority */
14012 int id = -1;
14013 int error = FALSE;
14014
14015 rettv->vval.v_number = -1;
14016
14017 if (grp == NULL || pat == NULL)
14018 return;
14019 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014020 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014021 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014022 if (argvars[3].v_type != VAR_UNKNOWN)
14023 id = get_tv_number_chk(&argvars[3], &error);
14024 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014025 if (error == TRUE)
14026 return;
14027 if (id >= 1 && id <= 3)
14028 {
14029 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14030 return;
14031 }
14032
14033 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14034#endif
14035}
14036
14037/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014038 * "matcharg()" function
14039 */
14040 static void
14041f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014042 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014043 typval_T *rettv;
14044{
14045 if (rettv_list_alloc(rettv) == OK)
14046 {
14047#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014048 int id = get_tv_number(&argvars[0]);
14049 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014050
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014051 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014052 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014053 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14054 {
14055 list_append_string(rettv->vval.v_list,
14056 syn_id2name(m->hlg_id), -1);
14057 list_append_string(rettv->vval.v_list, m->pattern, -1);
14058 }
14059 else
14060 {
14061 list_append_string(rettv->vval.v_list, NUL, -1);
14062 list_append_string(rettv->vval.v_list, NUL, -1);
14063 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014064 }
14065#endif
14066 }
14067}
14068
14069/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014070 * "matchdelete()" function
14071 */
14072 static void
14073f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014074 typval_T *argvars UNUSED;
14075 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014076{
14077#ifdef FEAT_SEARCH_EXTRA
14078 rettv->vval.v_number = match_delete(curwin,
14079 (int)get_tv_number(&argvars[0]), TRUE);
14080#endif
14081}
14082
14083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084 * "matchend()" function
14085 */
14086 static void
14087f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014088 typval_T *argvars;
14089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090{
14091 find_some_match(argvars, rettv, 0);
14092}
14093
14094/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014095 * "matchlist()" function
14096 */
14097 static void
14098f_matchlist(argvars, rettv)
14099 typval_T *argvars;
14100 typval_T *rettv;
14101{
14102 find_some_match(argvars, rettv, 3);
14103}
14104
14105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106 * "matchstr()" function
14107 */
14108 static void
14109f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *argvars;
14111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112{
14113 find_some_match(argvars, rettv, 2);
14114}
14115
Bram Moolenaar33570922005-01-25 22:26:29 +000014116static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014117
14118 static void
14119max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 typval_T *argvars;
14121 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014122 int domax;
14123{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014124 long n = 0;
14125 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014127
14128 if (argvars[0].v_type == VAR_LIST)
14129 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 list_T *l;
14131 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014132
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014133 l = argvars[0].vval.v_list;
14134 if (l != NULL)
14135 {
14136 li = l->lv_first;
14137 if (li != NULL)
14138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014140 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014141 {
14142 li = li->li_next;
14143 if (li == NULL)
14144 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014146 if (domax ? i > n : i < n)
14147 n = i;
14148 }
14149 }
14150 }
14151 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014152 else if (argvars[0].v_type == VAR_DICT)
14153 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014154 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014155 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014158
14159 d = argvars[0].vval.v_dict;
14160 if (d != NULL)
14161 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014162 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014164 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014165 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014167 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014169 if (first)
14170 {
14171 n = i;
14172 first = FALSE;
14173 }
14174 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014175 n = i;
14176 }
14177 }
14178 }
14179 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014180 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014181 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014183}
14184
14185/*
14186 * "max()" function
14187 */
14188 static void
14189f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014190 typval_T *argvars;
14191 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014192{
14193 max_min(argvars, rettv, TRUE);
14194}
14195
14196/*
14197 * "min()" function
14198 */
14199 static void
14200f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 typval_T *argvars;
14202 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014203{
14204 max_min(argvars, rettv, FALSE);
14205}
14206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014207static int mkdir_recurse __ARGS((char_u *dir, int prot));
14208
14209/*
14210 * Create the directory in which "dir" is located, and higher levels when
14211 * needed.
14212 */
14213 static int
14214mkdir_recurse(dir, prot)
14215 char_u *dir;
14216 int prot;
14217{
14218 char_u *p;
14219 char_u *updir;
14220 int r = FAIL;
14221
14222 /* Get end of directory name in "dir".
14223 * We're done when it's "/" or "c:/". */
14224 p = gettail_sep(dir);
14225 if (p <= get_past_head(dir))
14226 return OK;
14227
14228 /* If the directory exists we're done. Otherwise: create it.*/
14229 updir = vim_strnsave(dir, (int)(p - dir));
14230 if (updir == NULL)
14231 return FAIL;
14232 if (mch_isdir(updir))
14233 r = OK;
14234 else if (mkdir_recurse(updir, prot) == OK)
14235 r = vim_mkdir_emsg(updir, prot);
14236 vim_free(updir);
14237 return r;
14238}
14239
14240#ifdef vim_mkdir
14241/*
14242 * "mkdir()" function
14243 */
14244 static void
14245f_mkdir(argvars, rettv)
14246 typval_T *argvars;
14247 typval_T *rettv;
14248{
14249 char_u *dir;
14250 char_u buf[NUMBUFLEN];
14251 int prot = 0755;
14252
14253 rettv->vval.v_number = FAIL;
14254 if (check_restricted() || check_secure())
14255 return;
14256
14257 dir = get_tv_string_buf(&argvars[0], buf);
14258 if (argvars[1].v_type != VAR_UNKNOWN)
14259 {
14260 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 prot = get_tv_number_chk(&argvars[2], NULL);
14262 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014263 mkdir_recurse(dir, prot);
14264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014266}
14267#endif
14268
Bram Moolenaar0d660222005-01-07 21:51:51 +000014269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270 * "mode()" function
14271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014274 typval_T *argvars;
14275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014277 char_u buf[3];
14278
14279 buf[1] = NUL;
14280 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281
14282#ifdef FEAT_VISUAL
14283 if (VIsual_active)
14284 {
14285 if (VIsual_select)
14286 buf[0] = VIsual_mode + 's' - 'v';
14287 else
14288 buf[0] = VIsual_mode;
14289 }
14290 else
14291#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014292 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14293 || State == CONFIRM)
14294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014296 if (State == ASKMORE)
14297 buf[1] = 'm';
14298 else if (State == CONFIRM)
14299 buf[1] = '?';
14300 }
14301 else if (State == EXTERNCMD)
14302 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 else if (State & INSERT)
14304 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014305#ifdef FEAT_VREPLACE
14306 if (State & VREPLACE_FLAG)
14307 {
14308 buf[0] = 'R';
14309 buf[1] = 'v';
14310 }
14311 else
14312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 if (State & REPLACE_FLAG)
14314 buf[0] = 'R';
14315 else
14316 buf[0] = 'i';
14317 }
14318 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014321 if (exmode_active)
14322 buf[1] = 'v';
14323 }
14324 else if (exmode_active)
14325 {
14326 buf[0] = 'c';
14327 buf[1] = 'e';
14328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014332 if (finish_op)
14333 buf[1] = 'o';
14334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014336 /* Clear out the minor mode when the argument is not a non-zero number or
14337 * non-empty string. */
14338 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014339 buf[1] = NUL;
14340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 rettv->vval.v_string = vim_strsave(buf);
14342 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343}
14344
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014345#ifdef FEAT_MZSCHEME
14346/*
14347 * "mzeval()" function
14348 */
14349 static void
14350f_mzeval(argvars, rettv)
14351 typval_T *argvars;
14352 typval_T *rettv;
14353{
14354 char_u *str;
14355 char_u buf[NUMBUFLEN];
14356
14357 str = get_tv_string_buf(&argvars[0], buf);
14358 do_mzeval(str, rettv);
14359}
Bram Moolenaar75676462013-01-30 14:55:42 +010014360
14361 void
14362mzscheme_call_vim(name, args, rettv)
14363 char_u *name;
14364 typval_T *args;
14365 typval_T *rettv;
14366{
14367 typval_T argvars[3];
14368
14369 argvars[0].v_type = VAR_STRING;
14370 argvars[0].vval.v_string = name;
14371 copy_tv(args, &argvars[1]);
14372 argvars[2].v_type = VAR_UNKNOWN;
14373 f_call(argvars, rettv);
14374 clear_tv(&argvars[1]);
14375}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014376#endif
14377
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379 * "nextnonblank()" function
14380 */
14381 static void
14382f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T *argvars;
14384 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385{
14386 linenr_T lnum;
14387
14388 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014391 {
14392 lnum = 0;
14393 break;
14394 }
14395 if (*skipwhite(ml_get(lnum)) != NUL)
14396 break;
14397 }
14398 rettv->vval.v_number = lnum;
14399}
14400
14401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 * "nr2char()" function
14403 */
14404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014406 typval_T *argvars;
14407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408{
14409 char_u buf[NUMBUFLEN];
14410
14411#ifdef FEAT_MBYTE
14412 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014413 {
14414 int utf8 = 0;
14415
14416 if (argvars[1].v_type != VAR_UNKNOWN)
14417 utf8 = get_tv_number_chk(&argvars[1], NULL);
14418 if (utf8)
14419 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14420 else
14421 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 else
14424#endif
14425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 buf[1] = NUL;
14428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->v_type = VAR_STRING;
14430 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014431}
14432
14433/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014434 * "or(expr, expr)" function
14435 */
14436 static void
14437f_or(argvars, rettv)
14438 typval_T *argvars;
14439 typval_T *rettv;
14440{
14441 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14442 | get_tv_number_chk(&argvars[1], NULL);
14443}
14444
14445/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014446 * "pathshorten()" function
14447 */
14448 static void
14449f_pathshorten(argvars, rettv)
14450 typval_T *argvars;
14451 typval_T *rettv;
14452{
14453 char_u *p;
14454
14455 rettv->v_type = VAR_STRING;
14456 p = get_tv_string_chk(&argvars[0]);
14457 if (p == NULL)
14458 rettv->vval.v_string = NULL;
14459 else
14460 {
14461 p = vim_strsave(p);
14462 rettv->vval.v_string = p;
14463 if (p != NULL)
14464 shorten_dir(p);
14465 }
14466}
14467
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014468#ifdef FEAT_FLOAT
14469/*
14470 * "pow()" function
14471 */
14472 static void
14473f_pow(argvars, rettv)
14474 typval_T *argvars;
14475 typval_T *rettv;
14476{
14477 float_T fx, fy;
14478
14479 rettv->v_type = VAR_FLOAT;
14480 if (get_float_arg(argvars, &fx) == OK
14481 && get_float_arg(&argvars[1], &fy) == OK)
14482 rettv->vval.v_float = pow(fx, fy);
14483 else
14484 rettv->vval.v_float = 0.0;
14485}
14486#endif
14487
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489 * "prevnonblank()" function
14490 */
14491 static void
14492f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T *argvars;
14494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495{
14496 linenr_T lnum;
14497
14498 lnum = get_tv_lnum(argvars);
14499 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14500 lnum = 0;
14501 else
14502 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14503 --lnum;
14504 rettv->vval.v_number = lnum;
14505}
14506
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014507#ifdef HAVE_STDARG_H
14508/* This dummy va_list is here because:
14509 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14510 * - locally in the function results in a "used before set" warning
14511 * - using va_start() to initialize it gives "function with fixed args" error */
14512static va_list ap;
14513#endif
14514
Bram Moolenaar8c711452005-01-14 21:53:12 +000014515/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014516 * "printf()" function
14517 */
14518 static void
14519f_printf(argvars, rettv)
14520 typval_T *argvars;
14521 typval_T *rettv;
14522{
14523 rettv->v_type = VAR_STRING;
14524 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014525#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014526 {
14527 char_u buf[NUMBUFLEN];
14528 int len;
14529 char_u *s;
14530 int saved_did_emsg = did_emsg;
14531 char *fmt;
14532
14533 /* Get the required length, allocate the buffer and do it for real. */
14534 did_emsg = FALSE;
14535 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014536 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014537 if (!did_emsg)
14538 {
14539 s = alloc(len + 1);
14540 if (s != NULL)
14541 {
14542 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014543 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014544 }
14545 }
14546 did_emsg |= saved_did_emsg;
14547 }
14548#endif
14549}
14550
14551/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014552 * "pumvisible()" function
14553 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014554 static void
14555f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014556 typval_T *argvars UNUSED;
14557 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014558{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014559#ifdef FEAT_INS_EXPAND
14560 if (pum_visible())
14561 rettv->vval.v_number = 1;
14562#endif
14563}
14564
Bram Moolenaardb913952012-06-29 12:54:53 +020014565#ifdef FEAT_PYTHON3
14566/*
14567 * "py3eval()" function
14568 */
14569 static void
14570f_py3eval(argvars, rettv)
14571 typval_T *argvars;
14572 typval_T *rettv;
14573{
14574 char_u *str;
14575 char_u buf[NUMBUFLEN];
14576
14577 str = get_tv_string_buf(&argvars[0], buf);
14578 do_py3eval(str, rettv);
14579}
14580#endif
14581
14582#ifdef FEAT_PYTHON
14583/*
14584 * "pyeval()" function
14585 */
14586 static void
14587f_pyeval(argvars, rettv)
14588 typval_T *argvars;
14589 typval_T *rettv;
14590{
14591 char_u *str;
14592 char_u buf[NUMBUFLEN];
14593
14594 str = get_tv_string_buf(&argvars[0], buf);
14595 do_pyeval(str, rettv);
14596}
14597#endif
14598
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014599/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014600 * "range()" function
14601 */
14602 static void
14603f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 typval_T *argvars;
14605 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606{
14607 long start;
14608 long end;
14609 long stride = 1;
14610 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 if (argvars[1].v_type == VAR_UNKNOWN)
14615 {
14616 end = start - 1;
14617 start = 0;
14618 }
14619 else
14620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 }
14625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 if (error)
14627 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014629 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014630 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014631 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632 else
14633 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014634 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014636 if (list_append_number(rettv->vval.v_list,
14637 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014639 }
14640}
14641
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014642/*
14643 * "readfile()" function
14644 */
14645 static void
14646f_readfile(argvars, rettv)
14647 typval_T *argvars;
14648 typval_T *rettv;
14649{
14650 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014651 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014652 char_u *fname;
14653 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014654 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14655 int io_size = sizeof(buf);
14656 int readlen; /* size of last fread() */
14657 char_u *prev = NULL; /* previously read bytes, if any */
14658 long prevlen = 0; /* length of data in prev */
14659 long prevsize = 0; /* size of prev buffer */
14660 long maxline = MAXLNUM;
14661 long cnt = 0;
14662 char_u *p; /* position in buf */
14663 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014665 if (argvars[1].v_type != VAR_UNKNOWN)
14666 {
14667 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14668 binary = TRUE;
14669 if (argvars[2].v_type != VAR_UNKNOWN)
14670 maxline = get_tv_number(&argvars[2]);
14671 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014673 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014674 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014675
14676 /* Always open the file in binary mode, library functions have a mind of
14677 * their own about CR-LF conversion. */
14678 fname = get_tv_string(&argvars[0]);
14679 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14680 {
14681 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14682 return;
14683 }
14684
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014685 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014686 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014687 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014688
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014689 /* This for loop processes what was read, but is also entered at end
14690 * of file so that either:
14691 * - an incomplete line gets written
14692 * - a "binary" file gets an empty line at the end if it ends in a
14693 * newline. */
14694 for (p = buf, start = buf;
14695 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14696 ++p)
14697 {
14698 if (*p == '\n' || readlen <= 0)
14699 {
14700 listitem_T *li;
14701 char_u *s = NULL;
14702 long_u len = p - start;
14703
14704 /* Finished a line. Remove CRs before NL. */
14705 if (readlen > 0 && !binary)
14706 {
14707 while (len > 0 && start[len - 1] == '\r')
14708 --len;
14709 /* removal may cross back to the "prev" string */
14710 if (len == 0)
14711 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14712 --prevlen;
14713 }
14714 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014715 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716 else
14717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014718 /* Change "prev" buffer to be the right size. This way
14719 * the bytes are only copied once, and very long lines are
14720 * allocated only once. */
14721 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014723 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014725 prev = NULL; /* the list will own the string */
14726 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 }
14728 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 if (s == NULL)
14730 {
14731 do_outofmem_msg((long_u) prevlen + len + 1);
14732 failed = TRUE;
14733 break;
14734 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014735
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014736 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014737 {
14738 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014739 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014740 break;
14741 }
14742 li->li_tv.v_type = VAR_STRING;
14743 li->li_tv.v_lock = 0;
14744 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014745 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014746
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014747 start = p + 1; /* step over newline */
14748 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014749 break;
14750 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014751 else if (*p == NUL)
14752 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014753#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014754 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14755 * when finding the BF and check the previous two bytes. */
14756 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014757 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14759 * + 1, these may be in the "prev" string. */
14760 char_u back1 = p >= buf + 1 ? p[-1]
14761 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14762 char_u back2 = p >= buf + 2 ? p[-2]
14763 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14764 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014765
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014766 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014768 char_u *dest = p - 2;
14769
14770 /* Usually a BOM is at the beginning of a file, and so at
14771 * the beginning of a line; then we can just step over it.
14772 */
14773 if (start == dest)
14774 start = p + 1;
14775 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014776 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014777 /* have to shuffle buf to close gap */
14778 int adjust_prevlen = 0;
14779
14780 if (dest < buf)
14781 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014782 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783 dest = buf;
14784 }
14785 if (readlen > p - buf + 1)
14786 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14787 readlen -= 3 - adjust_prevlen;
14788 prevlen -= adjust_prevlen;
14789 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014790 }
14791 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014793#endif
14794 } /* for */
14795
14796 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14797 break;
14798 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014799 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014800 /* There's part of a line in buf, store it in "prev". */
14801 if (p - start + prevlen >= prevsize)
14802 {
14803 /* need bigger "prev" buffer */
14804 char_u *newprev;
14805
14806 /* A common use case is ordinary text files and "prev" gets a
14807 * fragment of a line, so the first allocation is made
14808 * small, to avoid repeatedly 'allocing' large and
14809 * 'reallocing' small. */
14810 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014811 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014812 else
14813 {
14814 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014815 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014816 prevsize = grow50pc > growmin ? grow50pc : growmin;
14817 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014818 newprev = prev == NULL ? alloc(prevsize)
14819 : vim_realloc(prev, prevsize);
14820 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014821 {
14822 do_outofmem_msg((long_u)prevsize);
14823 failed = TRUE;
14824 break;
14825 }
14826 prev = newprev;
14827 }
14828 /* Add the line part to end of "prev". */
14829 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014830 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014831 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014832 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014833
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014834 /*
14835 * For a negative line count use only the lines at the end of the file,
14836 * free the rest.
14837 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014838 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014839 while (cnt > -maxline)
14840 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014841 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014842 --cnt;
14843 }
14844
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014845 if (failed)
14846 {
14847 list_free(rettv->vval.v_list, TRUE);
14848 /* readfile doc says an empty list is returned on error */
14849 rettv->vval.v_list = list_alloc();
14850 }
14851
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014852 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014853 fclose(fd);
14854}
14855
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014856#if defined(FEAT_RELTIME)
14857static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14858
14859/*
14860 * Convert a List to proftime_T.
14861 * Return FAIL when there is something wrong.
14862 */
14863 static int
14864list2proftime(arg, tm)
14865 typval_T *arg;
14866 proftime_T *tm;
14867{
14868 long n1, n2;
14869 int error = FALSE;
14870
14871 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14872 || arg->vval.v_list->lv_len != 2)
14873 return FAIL;
14874 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14875 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14876# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014877 tm->HighPart = n1;
14878 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014879# else
14880 tm->tv_sec = n1;
14881 tm->tv_usec = n2;
14882# endif
14883 return error ? FAIL : OK;
14884}
14885#endif /* FEAT_RELTIME */
14886
14887/*
14888 * "reltime()" function
14889 */
14890 static void
14891f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014892 typval_T *argvars UNUSED;
14893 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014894{
14895#ifdef FEAT_RELTIME
14896 proftime_T res;
14897 proftime_T start;
14898
14899 if (argvars[0].v_type == VAR_UNKNOWN)
14900 {
14901 /* No arguments: get current time. */
14902 profile_start(&res);
14903 }
14904 else if (argvars[1].v_type == VAR_UNKNOWN)
14905 {
14906 if (list2proftime(&argvars[0], &res) == FAIL)
14907 return;
14908 profile_end(&res);
14909 }
14910 else
14911 {
14912 /* Two arguments: compute the difference. */
14913 if (list2proftime(&argvars[0], &start) == FAIL
14914 || list2proftime(&argvars[1], &res) == FAIL)
14915 return;
14916 profile_sub(&res, &start);
14917 }
14918
14919 if (rettv_list_alloc(rettv) == OK)
14920 {
14921 long n1, n2;
14922
14923# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014924 n1 = res.HighPart;
14925 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014926# else
14927 n1 = res.tv_sec;
14928 n2 = res.tv_usec;
14929# endif
14930 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14931 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14932 }
14933#endif
14934}
14935
14936/*
14937 * "reltimestr()" function
14938 */
14939 static void
14940f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014941 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014942 typval_T *rettv;
14943{
14944#ifdef FEAT_RELTIME
14945 proftime_T tm;
14946#endif
14947
14948 rettv->v_type = VAR_STRING;
14949 rettv->vval.v_string = NULL;
14950#ifdef FEAT_RELTIME
14951 if (list2proftime(&argvars[0], &tm) == OK)
14952 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14953#endif
14954}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014955
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14957static void make_connection __ARGS((void));
14958static int check_connection __ARGS((void));
14959
14960 static void
14961make_connection()
14962{
14963 if (X_DISPLAY == NULL
14964# ifdef FEAT_GUI
14965 && !gui.in_use
14966# endif
14967 )
14968 {
14969 x_force_connect = TRUE;
14970 setup_term_clip();
14971 x_force_connect = FALSE;
14972 }
14973}
14974
14975 static int
14976check_connection()
14977{
14978 make_connection();
14979 if (X_DISPLAY == NULL)
14980 {
14981 EMSG(_("E240: No connection to Vim server"));
14982 return FAIL;
14983 }
14984 return OK;
14985}
14986#endif
14987
14988#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014989static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014990
14991 static void
14992remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014993 typval_T *argvars;
14994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995 int expr;
14996{
14997 char_u *server_name;
14998 char_u *keys;
14999 char_u *r = NULL;
15000 char_u buf[NUMBUFLEN];
15001# ifdef WIN32
15002 HWND w;
15003# else
15004 Window w;
15005# endif
15006
15007 if (check_restricted() || check_secure())
15008 return;
15009
15010# ifdef FEAT_X11
15011 if (check_connection() == FAIL)
15012 return;
15013# endif
15014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 server_name = get_tv_string_chk(&argvars[0]);
15016 if (server_name == NULL)
15017 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 keys = get_tv_string_buf(&argvars[1], buf);
15019# ifdef WIN32
15020 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15021# else
15022 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15023 < 0)
15024# endif
15025 {
15026 if (r != NULL)
15027 EMSG(r); /* sending worked but evaluation failed */
15028 else
15029 EMSG2(_("E241: Unable to send to %s"), server_name);
15030 return;
15031 }
15032
15033 rettv->vval.v_string = r;
15034
15035 if (argvars[2].v_type != VAR_UNKNOWN)
15036 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015038 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015041 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015042 v.di_tv.v_type = VAR_STRING;
15043 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015044 idvar = get_tv_string_chk(&argvars[2]);
15045 if (idvar != NULL)
15046 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015047 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048 }
15049}
15050#endif
15051
15052/*
15053 * "remote_expr()" function
15054 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 static void
15056f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059{
15060 rettv->v_type = VAR_STRING;
15061 rettv->vval.v_string = NULL;
15062#ifdef FEAT_CLIENTSERVER
15063 remote_common(argvars, rettv, TRUE);
15064#endif
15065}
15066
15067/*
15068 * "remote_foreground()" function
15069 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015070 static void
15071f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015072 typval_T *argvars UNUSED;
15073 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075#ifdef FEAT_CLIENTSERVER
15076# ifdef WIN32
15077 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015078 {
15079 char_u *server_name = get_tv_string_chk(&argvars[0]);
15080
15081 if (server_name != NULL)
15082 serverForeground(server_name);
15083 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084# else
15085 /* Send a foreground() expression to the server. */
15086 argvars[1].v_type = VAR_STRING;
15087 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15088 argvars[2].v_type = VAR_UNKNOWN;
15089 remote_common(argvars, rettv, TRUE);
15090 vim_free(argvars[1].vval.v_string);
15091# endif
15092#endif
15093}
15094
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095 static void
15096f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015097 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099{
15100#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 char_u *s = NULL;
15103# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015104 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015106 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107
15108 if (check_restricted() || check_secure())
15109 {
15110 rettv->vval.v_number = -1;
15111 return;
15112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015113 serverid = get_tv_string_chk(&argvars[0]);
15114 if (serverid == NULL)
15115 {
15116 rettv->vval.v_number = -1;
15117 return; /* type error; errmsg already given */
15118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015120 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 if (n == 0)
15122 rettv->vval.v_number = -1;
15123 else
15124 {
15125 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15126 rettv->vval.v_number = (s != NULL);
15127 }
15128# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 if (check_connection() == FAIL)
15130 return;
15131
15132 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015133 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134# endif
15135
15136 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015138 char_u *retvar;
15139
Bram Moolenaar33570922005-01-25 22:26:29 +000015140 v.di_tv.v_type = VAR_STRING;
15141 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 retvar = get_tv_string_chk(&argvars[1]);
15143 if (retvar != NULL)
15144 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146 }
15147#else
15148 rettv->vval.v_number = -1;
15149#endif
15150}
15151
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 static void
15153f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156{
15157 char_u *r = NULL;
15158
15159#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015160 char_u *serverid = get_tv_string_chk(&argvars[0]);
15161
15162 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163 {
15164# ifdef WIN32
15165 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015166 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015168 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 if (n != 0)
15170 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15171 if (r == NULL)
15172# else
15173 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015174 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175# endif
15176 EMSG(_("E277: Unable to read a server reply"));
15177 }
15178#endif
15179 rettv->v_type = VAR_STRING;
15180 rettv->vval.v_string = r;
15181}
15182
15183/*
15184 * "remote_send()" function
15185 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186 static void
15187f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015188 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015190{
15191 rettv->v_type = VAR_STRING;
15192 rettv->vval.v_string = NULL;
15193#ifdef FEAT_CLIENTSERVER
15194 remote_common(argvars, rettv, FALSE);
15195#endif
15196}
15197
15198/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015199 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015200 */
15201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015202f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015203 typval_T *argvars;
15204 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015205{
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 list_T *l;
15207 listitem_T *item, *item2;
15208 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015209 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015210 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015211 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015212 dict_T *d;
15213 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015214 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015215
Bram Moolenaar8c711452005-01-14 21:53:12 +000015216 if (argvars[0].v_type == VAR_DICT)
15217 {
15218 if (argvars[2].v_type != VAR_UNKNOWN)
15219 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015220 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015221 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015223 key = get_tv_string_chk(&argvars[1]);
15224 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 di = dict_find(d, key, -1);
15227 if (di == NULL)
15228 EMSG2(_(e_dictkey), key);
15229 else
15230 {
15231 *rettv = di->di_tv;
15232 init_tv(&di->di_tv);
15233 dictitem_remove(d, di);
15234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236 }
15237 }
15238 else if (argvars[0].v_type != VAR_LIST)
15239 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015240 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015241 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 int error = FALSE;
15244
15245 idx = get_tv_number_chk(&argvars[1], &error);
15246 if (error)
15247 ; /* type error: do nothing, errmsg already given */
15248 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015249 EMSGN(_(e_listidx), idx);
15250 else
15251 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015252 if (argvars[2].v_type == VAR_UNKNOWN)
15253 {
15254 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015255 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015256 *rettv = item->li_tv;
15257 vim_free(item);
15258 }
15259 else
15260 {
15261 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015262 end = get_tv_number_chk(&argvars[2], &error);
15263 if (error)
15264 ; /* type error: do nothing */
15265 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015266 EMSGN(_(e_listidx), end);
15267 else
15268 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015269 int cnt = 0;
15270
15271 for (li = item; li != NULL; li = li->li_next)
15272 {
15273 ++cnt;
15274 if (li == item2)
15275 break;
15276 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015277 if (li == NULL) /* didn't find "item2" after "item" */
15278 EMSG(_(e_invrange));
15279 else
15280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015281 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015283 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015284 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015285 l->lv_first = item;
15286 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015287 item->li_prev = NULL;
15288 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015289 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015290 }
15291 }
15292 }
15293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015294 }
15295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296}
15297
15298/*
15299 * "rename({from}, {to})" function
15300 */
15301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015302f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015303 typval_T *argvars;
15304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305{
15306 char_u buf[NUMBUFLEN];
15307
15308 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15312 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313}
15314
15315/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015316 * "repeat()" function
15317 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015318 static void
15319f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015320 typval_T *argvars;
15321 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015322{
15323 char_u *p;
15324 int n;
15325 int slen;
15326 int len;
15327 char_u *r;
15328 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015329
15330 n = get_tv_number(&argvars[1]);
15331 if (argvars[0].v_type == VAR_LIST)
15332 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015333 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015334 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015335 if (list_extend(rettv->vval.v_list,
15336 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015337 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015338 }
15339 else
15340 {
15341 p = get_tv_string(&argvars[0]);
15342 rettv->v_type = VAR_STRING;
15343 rettv->vval.v_string = NULL;
15344
15345 slen = (int)STRLEN(p);
15346 len = slen * n;
15347 if (len <= 0)
15348 return;
15349
15350 r = alloc(len + 1);
15351 if (r != NULL)
15352 {
15353 for (i = 0; i < n; i++)
15354 mch_memmove(r + i * slen, p, (size_t)slen);
15355 r[len] = NUL;
15356 }
15357
15358 rettv->vval.v_string = r;
15359 }
15360}
15361
15362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 * "resolve()" function
15364 */
15365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015367 typval_T *argvars;
15368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369{
15370 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015371#ifdef HAVE_READLINK
15372 char_u *buf = NULL;
15373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376#ifdef FEAT_SHORTCUT
15377 {
15378 char_u *v = NULL;
15379
15380 v = mch_resolve_shortcut(p);
15381 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 }
15386#else
15387# ifdef HAVE_READLINK
15388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 char_u *cpy;
15390 int len;
15391 char_u *remain = NULL;
15392 char_u *q;
15393 int is_relative_to_current = FALSE;
15394 int has_trailing_pathsep = FALSE;
15395 int limit = 100;
15396
15397 p = vim_strsave(p);
15398
15399 if (p[0] == '.' && (vim_ispathsep(p[1])
15400 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15401 is_relative_to_current = TRUE;
15402
15403 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015404 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015407 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409
15410 q = getnextcomp(p);
15411 if (*q != NUL)
15412 {
15413 /* Separate the first path component in "p", and keep the
15414 * remainder (beginning with the path separator). */
15415 remain = vim_strsave(q - 1);
15416 q[-1] = NUL;
15417 }
15418
Bram Moolenaard9462e32011-04-11 21:35:11 +020015419 buf = alloc(MAXPATHL + 1);
15420 if (buf == NULL)
15421 goto fail;
15422
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 for (;;)
15424 {
15425 for (;;)
15426 {
15427 len = readlink((char *)p, (char *)buf, MAXPATHL);
15428 if (len <= 0)
15429 break;
15430 buf[len] = NUL;
15431
15432 if (limit-- == 0)
15433 {
15434 vim_free(p);
15435 vim_free(remain);
15436 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 goto fail;
15439 }
15440
15441 /* Ensure that the result will have a trailing path separator
15442 * if the argument has one. */
15443 if (remain == NULL && has_trailing_pathsep)
15444 add_pathsep(buf);
15445
15446 /* Separate the first path component in the link value and
15447 * concatenate the remainders. */
15448 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15449 if (*q != NUL)
15450 {
15451 if (remain == NULL)
15452 remain = vim_strsave(q - 1);
15453 else
15454 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015455 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 if (cpy != NULL)
15457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 vim_free(remain);
15459 remain = cpy;
15460 }
15461 }
15462 q[-1] = NUL;
15463 }
15464
15465 q = gettail(p);
15466 if (q > p && *q == NUL)
15467 {
15468 /* Ignore trailing path separator. */
15469 q[-1] = NUL;
15470 q = gettail(p);
15471 }
15472 if (q > p && !mch_isFullName(buf))
15473 {
15474 /* symlink is relative to directory of argument */
15475 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15476 if (cpy != NULL)
15477 {
15478 STRCPY(cpy, p);
15479 STRCPY(gettail(cpy), buf);
15480 vim_free(p);
15481 p = cpy;
15482 }
15483 }
15484 else
15485 {
15486 vim_free(p);
15487 p = vim_strsave(buf);
15488 }
15489 }
15490
15491 if (remain == NULL)
15492 break;
15493
15494 /* Append the first path component of "remain" to "p". */
15495 q = getnextcomp(remain + 1);
15496 len = q - remain - (*q != NUL);
15497 cpy = vim_strnsave(p, STRLEN(p) + len);
15498 if (cpy != NULL)
15499 {
15500 STRNCAT(cpy, remain, len);
15501 vim_free(p);
15502 p = cpy;
15503 }
15504 /* Shorten "remain". */
15505 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015506 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 else
15508 {
15509 vim_free(remain);
15510 remain = NULL;
15511 }
15512 }
15513
15514 /* If the result is a relative path name, make it explicitly relative to
15515 * the current directory if and only if the argument had this form. */
15516 if (!vim_ispathsep(*p))
15517 {
15518 if (is_relative_to_current
15519 && *p != NUL
15520 && !(p[0] == '.'
15521 && (p[1] == NUL
15522 || vim_ispathsep(p[1])
15523 || (p[1] == '.'
15524 && (p[2] == NUL
15525 || vim_ispathsep(p[2]))))))
15526 {
15527 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015528 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 if (cpy != NULL)
15530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 vim_free(p);
15532 p = cpy;
15533 }
15534 }
15535 else if (!is_relative_to_current)
15536 {
15537 /* Strip leading "./". */
15538 q = p;
15539 while (q[0] == '.' && vim_ispathsep(q[1]))
15540 q += 2;
15541 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015542 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 }
15544 }
15545
15546 /* Ensure that the result will have no trailing path separator
15547 * if the argument had none. But keep "/" or "//". */
15548 if (!has_trailing_pathsep)
15549 {
15550 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015551 if (after_pathsep(p, q))
15552 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 }
15554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 }
15557# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559# endif
15560#endif
15561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563
15564#ifdef HAVE_READLINK
15565fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015566 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015568 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569}
15570
15571/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 */
15574 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015575f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015576 typval_T *argvars;
15577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578{
Bram Moolenaar33570922005-01-25 22:26:29 +000015579 list_T *l;
15580 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 if (argvars[0].v_type != VAR_LIST)
15583 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015584 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015585 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586 {
15587 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015588 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015589 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590 while (li != NULL)
15591 {
15592 ni = li->li_prev;
15593 list_append(l, li);
15594 li = ni;
15595 }
15596 rettv->vval.v_list = l;
15597 rettv->v_type = VAR_LIST;
15598 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015599 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601}
15602
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015603#define SP_NOMOVE 0x01 /* don't move cursor */
15604#define SP_REPEAT 0x02 /* repeat to find outer pair */
15605#define SP_RETCOUNT 0x04 /* return matchcount */
15606#define SP_SETPCMARK 0x08 /* set previous context mark */
15607#define SP_START 0x10 /* accept match at start position */
15608#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15609#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015610
Bram Moolenaar33570922005-01-25 22:26:29 +000015611static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015612
15613/*
15614 * Get flags for a search function.
15615 * Possibly sets "p_ws".
15616 * Returns BACKWARD, FORWARD or zero (for an error).
15617 */
15618 static int
15619get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015620 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015621 int *flagsp;
15622{
15623 int dir = FORWARD;
15624 char_u *flags;
15625 char_u nbuf[NUMBUFLEN];
15626 int mask;
15627
15628 if (varp->v_type != VAR_UNKNOWN)
15629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015630 flags = get_tv_string_buf_chk(varp, nbuf);
15631 if (flags == NULL)
15632 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633 while (*flags != NUL)
15634 {
15635 switch (*flags)
15636 {
15637 case 'b': dir = BACKWARD; break;
15638 case 'w': p_ws = TRUE; break;
15639 case 'W': p_ws = FALSE; break;
15640 default: mask = 0;
15641 if (flagsp != NULL)
15642 switch (*flags)
15643 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015644 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015645 case 'e': mask = SP_END; break;
15646 case 'm': mask = SP_RETCOUNT; break;
15647 case 'n': mask = SP_NOMOVE; break;
15648 case 'p': mask = SP_SUBPAT; break;
15649 case 'r': mask = SP_REPEAT; break;
15650 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651 }
15652 if (mask == 0)
15653 {
15654 EMSG2(_(e_invarg2), flags);
15655 dir = 0;
15656 }
15657 else
15658 *flagsp |= mask;
15659 }
15660 if (dir == 0)
15661 break;
15662 ++flags;
15663 }
15664 }
15665 return dir;
15666}
15667
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015669 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015671 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015672search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015673 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015674 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015675 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015677 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 char_u *pat;
15679 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015680 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 int save_p_ws = p_ws;
15682 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015683 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015684 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015685 proftime_T tm;
15686#ifdef FEAT_RELTIME
15687 long time_limit = 0;
15688#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015689 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015690 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015693 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015694 if (dir == 0)
15695 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015696 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015697 if (flags & SP_START)
15698 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015699 if (flags & SP_END)
15700 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701
Bram Moolenaar76929292008-01-06 19:07:36 +000015702 /* Optional arguments: line number to stop searching and timeout. */
15703 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 {
15705 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15706 if (lnum_stop < 0)
15707 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015708#ifdef FEAT_RELTIME
15709 if (argvars[3].v_type != VAR_UNKNOWN)
15710 {
15711 time_limit = get_tv_number_chk(&argvars[3], NULL);
15712 if (time_limit < 0)
15713 goto theend;
15714 }
15715#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015716 }
15717
Bram Moolenaar76929292008-01-06 19:07:36 +000015718#ifdef FEAT_RELTIME
15719 /* Set the time limit, if there is one. */
15720 profile_setlimit(time_limit, &tm);
15721#endif
15722
Bram Moolenaar231334e2005-07-25 20:46:57 +000015723 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015724 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015725 * Check to make sure only those flags are set.
15726 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15727 * flags cannot be set. Check for that condition also.
15728 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015729 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015730 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015732 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015733 goto theend;
15734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015736 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015737 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015738 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015739 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015741 if (flags & SP_SUBPAT)
15742 retval = subpatnum;
15743 else
15744 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015745 if (flags & SP_SETPCMARK)
15746 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015748 if (match_pos != NULL)
15749 {
15750 /* Store the match cursor position */
15751 match_pos->lnum = pos.lnum;
15752 match_pos->col = pos.col + 1;
15753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 /* "/$" will put the cursor after the end of the line, may need to
15755 * correct that here */
15756 check_cursor();
15757 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015758
15759 /* If 'n' flag is used: restore cursor position. */
15760 if (flags & SP_NOMOVE)
15761 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015762 else
15763 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015764theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015766
15767 return retval;
15768}
15769
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015770#ifdef FEAT_FLOAT
15771/*
15772 * "round({float})" function
15773 */
15774 static void
15775f_round(argvars, rettv)
15776 typval_T *argvars;
15777 typval_T *rettv;
15778{
15779 float_T f;
15780
15781 rettv->v_type = VAR_FLOAT;
15782 if (get_float_arg(argvars, &f) == OK)
15783 /* round() is not in C90, use ceil() or floor() instead. */
15784 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15785 else
15786 rettv->vval.v_float = 0.0;
15787}
15788#endif
15789
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015790/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015791 * "screencol()" function
15792 *
15793 * First column is 1 to be consistent with virtcol().
15794 */
15795 static void
15796f_screencol(argvars, rettv)
15797 typval_T *argvars UNUSED;
15798 typval_T *rettv;
15799{
15800 rettv->vval.v_number = screen_screencol() + 1;
15801}
15802
15803/*
15804 * "screenrow()" function
15805 */
15806 static void
15807f_screenrow(argvars, rettv)
15808 typval_T *argvars UNUSED;
15809 typval_T *rettv;
15810{
15811 rettv->vval.v_number = screen_screenrow() + 1;
15812}
15813
15814/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015815 * "search()" function
15816 */
15817 static void
15818f_search(argvars, rettv)
15819 typval_T *argvars;
15820 typval_T *rettv;
15821{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015822 int flags = 0;
15823
15824 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825}
15826
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015828 * "searchdecl()" function
15829 */
15830 static void
15831f_searchdecl(argvars, rettv)
15832 typval_T *argvars;
15833 typval_T *rettv;
15834{
15835 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015836 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015837 int error = FALSE;
15838 char_u *name;
15839
15840 rettv->vval.v_number = 1; /* default: FAIL */
15841
15842 name = get_tv_string_chk(&argvars[0]);
15843 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015844 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015845 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015846 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15847 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15848 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015849 if (!error && name != NULL)
15850 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015851 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015852}
15853
15854/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015855 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015857 static int
15858searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015859 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015860 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861{
15862 char_u *spat, *mpat, *epat;
15863 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 int dir;
15866 int flags = 0;
15867 char_u nbuf1[NUMBUFLEN];
15868 char_u nbuf2[NUMBUFLEN];
15869 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015870 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015871 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015872 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015875 spat = get_tv_string_chk(&argvars[0]);
15876 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15877 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15878 if (spat == NULL || mpat == NULL || epat == NULL)
15879 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 /* Handle the optional fourth argument: flags */
15882 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015883 if (dir == 0)
15884 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015885
15886 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015887 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15888 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015889 if ((flags & (SP_END | SP_SUBPAT)) != 0
15890 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015891 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015892 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015893 goto theend;
15894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015896 /* Using 'r' implies 'W', otherwise it doesn't work. */
15897 if (flags & SP_REPEAT)
15898 p_ws = FALSE;
15899
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015900 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015901 if (argvars[3].v_type == VAR_UNKNOWN
15902 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 skip = (char_u *)"";
15904 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 if (argvars[5].v_type != VAR_UNKNOWN)
15908 {
15909 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15910 if (lnum_stop < 0)
15911 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015912#ifdef FEAT_RELTIME
15913 if (argvars[6].v_type != VAR_UNKNOWN)
15914 {
15915 time_limit = get_tv_number_chk(&argvars[6], NULL);
15916 if (time_limit < 0)
15917 goto theend;
15918 }
15919#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015920 }
15921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 if (skip == NULL)
15923 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015925 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015926 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015927
15928theend:
15929 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015930
15931 return retval;
15932}
15933
15934/*
15935 * "searchpair()" function
15936 */
15937 static void
15938f_searchpair(argvars, rettv)
15939 typval_T *argvars;
15940 typval_T *rettv;
15941{
15942 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15943}
15944
15945/*
15946 * "searchpairpos()" function
15947 */
15948 static void
15949f_searchpairpos(argvars, rettv)
15950 typval_T *argvars;
15951 typval_T *rettv;
15952{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015953 pos_T match_pos;
15954 int lnum = 0;
15955 int col = 0;
15956
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015957 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015958 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015959
15960 if (searchpair_cmn(argvars, &match_pos) > 0)
15961 {
15962 lnum = match_pos.lnum;
15963 col = match_pos.col;
15964 }
15965
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015966 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15967 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015968}
15969
15970/*
15971 * Search for a start/middle/end thing.
15972 * Used by searchpair(), see its documentation for the details.
15973 * Returns 0 or -1 for no match,
15974 */
15975 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015976do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15977 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015978 char_u *spat; /* start pattern */
15979 char_u *mpat; /* middle pattern */
15980 char_u *epat; /* end pattern */
15981 int dir; /* BACKWARD or FORWARD */
15982 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015983 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015984 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015985 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015986 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015987{
15988 char_u *save_cpo;
15989 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15990 long retval = 0;
15991 pos_T pos;
15992 pos_T firstpos;
15993 pos_T foundpos;
15994 pos_T save_cursor;
15995 pos_T save_pos;
15996 int n;
15997 int r;
15998 int nest = 1;
15999 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016000 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016001 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016002
16003 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16004 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016005 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016006
Bram Moolenaar76929292008-01-06 19:07:36 +000016007#ifdef FEAT_RELTIME
16008 /* Set the time limit, if there is one. */
16009 profile_setlimit(time_limit, &tm);
16010#endif
16011
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016012 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16013 * start/middle/end (pat3, for the top pair). */
16014 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16015 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16016 if (pat2 == NULL || pat3 == NULL)
16017 goto theend;
16018 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16019 if (*mpat == NUL)
16020 STRCPY(pat3, pat2);
16021 else
16022 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16023 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016024 if (flags & SP_START)
16025 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016026
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 save_cursor = curwin->w_cursor;
16028 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016029 clearpos(&firstpos);
16030 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 pat = pat3;
16032 for (;;)
16033 {
16034 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016035 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16037 /* didn't find it or found the first match again: FAIL */
16038 break;
16039
16040 if (firstpos.lnum == 0)
16041 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016042 if (equalpos(pos, foundpos))
16043 {
16044 /* Found the same position again. Can happen with a pattern that
16045 * has "\zs" at the end and searching backwards. Advance one
16046 * character and try again. */
16047 if (dir == BACKWARD)
16048 decl(&pos);
16049 else
16050 incl(&pos);
16051 }
16052 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016054 /* clear the start flag to avoid getting stuck here */
16055 options &= ~SEARCH_START;
16056
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 /* If the skip pattern matches, ignore this match. */
16058 if (*skip != NUL)
16059 {
16060 save_pos = curwin->w_cursor;
16061 curwin->w_cursor = pos;
16062 r = eval_to_bool(skip, &err, NULL, FALSE);
16063 curwin->w_cursor = save_pos;
16064 if (err)
16065 {
16066 /* Evaluating {skip} caused an error, break here. */
16067 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016068 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 break;
16070 }
16071 if (r)
16072 continue;
16073 }
16074
16075 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16076 {
16077 /* Found end when searching backwards or start when searching
16078 * forward: nested pair. */
16079 ++nest;
16080 pat = pat2; /* nested, don't search for middle */
16081 }
16082 else
16083 {
16084 /* Found end when searching forward or start when searching
16085 * backward: end of (nested) pair; or found middle in outer pair. */
16086 if (--nest == 1)
16087 pat = pat3; /* outer level, search for middle */
16088 }
16089
16090 if (nest == 0)
16091 {
16092 /* Found the match: return matchcount or line number. */
16093 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016094 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016096 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016097 if (flags & SP_SETPCMARK)
16098 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 curwin->w_cursor = pos;
16100 if (!(flags & SP_REPEAT))
16101 break;
16102 nest = 1; /* search for next unmatched */
16103 }
16104 }
16105
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016106 if (match_pos != NULL)
16107 {
16108 /* Store the match cursor position */
16109 match_pos->lnum = curwin->w_cursor.lnum;
16110 match_pos->col = curwin->w_cursor.col + 1;
16111 }
16112
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016114 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 curwin->w_cursor = save_cursor;
16116
16117theend:
16118 vim_free(pat2);
16119 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016120 if (p_cpo == empty_option)
16121 p_cpo = save_cpo;
16122 else
16123 /* Darn, evaluating the {skip} expression changed the value. */
16124 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016125
16126 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127}
16128
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016129/*
16130 * "searchpos()" function
16131 */
16132 static void
16133f_searchpos(argvars, rettv)
16134 typval_T *argvars;
16135 typval_T *rettv;
16136{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016137 pos_T match_pos;
16138 int lnum = 0;
16139 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016140 int n;
16141 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016144 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016145
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016146 n = search_cmn(argvars, &match_pos, &flags);
16147 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016148 {
16149 lnum = match_pos.lnum;
16150 col = match_pos.col;
16151 }
16152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016153 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16154 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016155 if (flags & SP_SUBPAT)
16156 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016157}
16158
16159
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 static void
16161f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165#ifdef FEAT_CLIENTSERVER
16166 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016167 char_u *server = get_tv_string_chk(&argvars[0]);
16168 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169
Bram Moolenaar0d660222005-01-07 21:51:51 +000016170 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016171 if (server == NULL || reply == NULL)
16172 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173 if (check_restricted() || check_secure())
16174 return;
16175# ifdef FEAT_X11
16176 if (check_connection() == FAIL)
16177 return;
16178# endif
16179
16180 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182 EMSG(_("E258: Unable to send to client"));
16183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185 rettv->vval.v_number = 0;
16186#else
16187 rettv->vval.v_number = -1;
16188#endif
16189}
16190
Bram Moolenaar0d660222005-01-07 21:51:51 +000016191 static void
16192f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195{
16196 char_u *r = NULL;
16197
16198#ifdef FEAT_CLIENTSERVER
16199# ifdef WIN32
16200 r = serverGetVimNames();
16201# else
16202 make_connection();
16203 if (X_DISPLAY != NULL)
16204 r = serverGetVimNames(X_DISPLAY);
16205# endif
16206#endif
16207 rettv->v_type = VAR_STRING;
16208 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209}
16210
16211/*
16212 * "setbufvar()" function
16213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016215f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016216 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016217 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218{
16219 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 char_u nbuf[NUMBUFLEN];
16224
16225 if (check_restricted() || check_secure())
16226 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16228 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 varp = &argvars[2];
16231
16232 if (buf != NULL && varname != NULL && varp != NULL)
16233 {
16234 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236
16237 if (*varname == '&')
16238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016239 long numval;
16240 char_u *strval;
16241 int error = FALSE;
16242
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016244 numval = get_tv_number_chk(varp, &error);
16245 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 if (!error && strval != NULL)
16247 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 }
16249 else
16250 {
16251 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16252 if (bufvarname != NULL)
16253 {
16254 STRCPY(bufvarname, "b:");
16255 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016256 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 vim_free(bufvarname);
16258 }
16259 }
16260
16261 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264}
16265
16266/*
16267 * "setcmdpos()" function
16268 */
16269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016271 typval_T *argvars;
16272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 int pos = (int)get_tv_number(&argvars[0]) - 1;
16275
16276 if (pos >= 0)
16277 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278}
16279
16280/*
16281 * "setline()" function
16282 */
16283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016284f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 typval_T *argvars;
16286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287{
16288 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016289 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016290 list_T *l = NULL;
16291 listitem_T *li = NULL;
16292 long added = 0;
16293 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016295 lnum = get_tv_lnum(&argvars[0]);
16296 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016298 l = argvars[1].vval.v_list;
16299 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016301 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016302 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016304 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016305 for (;;)
16306 {
16307 if (l != NULL)
16308 {
16309 /* list argument, get next string */
16310 if (li == NULL)
16311 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016312 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016313 li = li->li_next;
16314 }
16315
16316 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016317 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016318 break;
16319 if (lnum <= curbuf->b_ml.ml_line_count)
16320 {
16321 /* existing line, replace it */
16322 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16323 {
16324 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016325 if (lnum == curwin->w_cursor.lnum)
16326 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016327 rettv->vval.v_number = 0; /* OK */
16328 }
16329 }
16330 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16331 {
16332 /* lnum is one past the last line, append the line */
16333 ++added;
16334 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16335 rettv->vval.v_number = 0; /* OK */
16336 }
16337
16338 if (l == NULL) /* only one string argument */
16339 break;
16340 ++lnum;
16341 }
16342
16343 if (added > 0)
16344 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345}
16346
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016347static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16348
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016350 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016351 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016352 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016353set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016354 win_T *wp UNUSED;
16355 typval_T *list_arg UNUSED;
16356 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016357 typval_T *rettv;
16358{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016359#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016360 char_u *act;
16361 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016362#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016363
Bram Moolenaar2641f772005-03-25 21:58:17 +000016364 rettv->vval.v_number = -1;
16365
16366#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016367 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016368 EMSG(_(e_listreq));
16369 else
16370 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016371 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016372
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016373 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016374 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016375 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016376 if (act == NULL)
16377 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016378 if (*act == 'a' || *act == 'r')
16379 action = *act;
16380 }
16381
Bram Moolenaar81484f42012-12-05 15:16:47 +010016382 if (l != NULL && set_errorlist(wp, l, action,
16383 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016384 rettv->vval.v_number = 0;
16385 }
16386#endif
16387}
16388
16389/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016390 * "setloclist()" function
16391 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016392 static void
16393f_setloclist(argvars, rettv)
16394 typval_T *argvars;
16395 typval_T *rettv;
16396{
16397 win_T *win;
16398
16399 rettv->vval.v_number = -1;
16400
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016401 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016402 if (win != NULL)
16403 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16404}
16405
16406/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016407 * "setmatches()" function
16408 */
16409 static void
16410f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016411 typval_T *argvars UNUSED;
16412 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016413{
16414#ifdef FEAT_SEARCH_EXTRA
16415 list_T *l;
16416 listitem_T *li;
16417 dict_T *d;
16418
16419 rettv->vval.v_number = -1;
16420 if (argvars[0].v_type != VAR_LIST)
16421 {
16422 EMSG(_(e_listreq));
16423 return;
16424 }
16425 if ((l = argvars[0].vval.v_list) != NULL)
16426 {
16427
16428 /* To some extent make sure that we are dealing with a list from
16429 * "getmatches()". */
16430 li = l->lv_first;
16431 while (li != NULL)
16432 {
16433 if (li->li_tv.v_type != VAR_DICT
16434 || (d = li->li_tv.vval.v_dict) == NULL)
16435 {
16436 EMSG(_(e_invarg));
16437 return;
16438 }
16439 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16440 && dict_find(d, (char_u *)"pattern", -1) != NULL
16441 && dict_find(d, (char_u *)"priority", -1) != NULL
16442 && dict_find(d, (char_u *)"id", -1) != NULL))
16443 {
16444 EMSG(_(e_invarg));
16445 return;
16446 }
16447 li = li->li_next;
16448 }
16449
16450 clear_matches(curwin);
16451 li = l->lv_first;
16452 while (li != NULL)
16453 {
16454 d = li->li_tv.vval.v_dict;
16455 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16456 get_dict_string(d, (char_u *)"pattern", FALSE),
16457 (int)get_dict_number(d, (char_u *)"priority"),
16458 (int)get_dict_number(d, (char_u *)"id"));
16459 li = li->li_next;
16460 }
16461 rettv->vval.v_number = 0;
16462 }
16463#endif
16464}
16465
16466/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016467 * "setpos()" function
16468 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016469 static void
16470f_setpos(argvars, rettv)
16471 typval_T *argvars;
16472 typval_T *rettv;
16473{
16474 pos_T pos;
16475 int fnum;
16476 char_u *name;
16477
Bram Moolenaar08250432008-02-13 11:42:46 +000016478 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016479 name = get_tv_string_chk(argvars);
16480 if (name != NULL)
16481 {
16482 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16483 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016484 if (--pos.col < 0)
16485 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016486 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016487 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016488 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016489 if (fnum == curbuf->b_fnum)
16490 {
16491 curwin->w_cursor = pos;
16492 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016493 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016494 }
16495 else
16496 EMSG(_(e_invarg));
16497 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016498 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16499 {
16500 /* set mark */
16501 if (setmark_pos(name[1], &pos, fnum) == OK)
16502 rettv->vval.v_number = 0;
16503 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016504 else
16505 EMSG(_(e_invarg));
16506 }
16507 }
16508}
16509
16510/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016511 * "setqflist()" function
16512 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016513 static void
16514f_setqflist(argvars, rettv)
16515 typval_T *argvars;
16516 typval_T *rettv;
16517{
16518 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16519}
16520
16521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 * "setreg()" function
16523 */
16524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016525f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016526 typval_T *argvars;
16527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528{
16529 int regname;
16530 char_u *strregname;
16531 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016532 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 int append;
16534 char_u yank_type;
16535 long block_len;
16536
16537 block_len = -1;
16538 yank_type = MAUTO;
16539 append = FALSE;
16540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016544 if (strregname == NULL)
16545 return; /* type error; errmsg already given */
16546 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 if (regname == 0 || regname == '@')
16548 regname = '"';
16549 else if (regname == '=')
16550 return;
16551
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016552 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 stropt = get_tv_string_chk(&argvars[2]);
16555 if (stropt == NULL)
16556 return; /* type error */
16557 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 switch (*stropt)
16559 {
16560 case 'a': case 'A': /* append */
16561 append = TRUE;
16562 break;
16563 case 'v': case 'c': /* character-wise selection */
16564 yank_type = MCHAR;
16565 break;
16566 case 'V': case 'l': /* line-wise selection */
16567 yank_type = MLINE;
16568 break;
16569#ifdef FEAT_VISUAL
16570 case 'b': case Ctrl_V: /* block-wise selection */
16571 yank_type = MBLOCK;
16572 if (VIM_ISDIGIT(stropt[1]))
16573 {
16574 ++stropt;
16575 block_len = getdigits(&stropt) - 1;
16576 --stropt;
16577 }
16578 break;
16579#endif
16580 }
16581 }
16582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 strval = get_tv_string_chk(&argvars[1]);
16584 if (strval != NULL)
16585 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016587 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588}
16589
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016590/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016591 * "settabvar()" function
16592 */
16593 static void
16594f_settabvar(argvars, rettv)
16595 typval_T *argvars;
16596 typval_T *rettv;
16597{
16598 tabpage_T *save_curtab;
16599 char_u *varname, *tabvarname;
16600 typval_T *varp;
16601 tabpage_T *tp;
16602
16603 rettv->vval.v_number = 0;
16604
16605 if (check_restricted() || check_secure())
16606 return;
16607
16608 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16609 varname = get_tv_string_chk(&argvars[1]);
16610 varp = &argvars[2];
16611
16612 if (tp != NULL && varname != NULL && varp != NULL)
16613 {
16614 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016615 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016616
16617 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16618 if (tabvarname != NULL)
16619 {
16620 STRCPY(tabvarname, "t:");
16621 STRCPY(tabvarname + 2, varname);
16622 set_var(tabvarname, varp, TRUE);
16623 vim_free(tabvarname);
16624 }
16625
16626 /* Restore current tabpage */
16627 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016628 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016629 }
16630}
16631
16632/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016633 * "settabwinvar()" function
16634 */
16635 static void
16636f_settabwinvar(argvars, rettv)
16637 typval_T *argvars;
16638 typval_T *rettv;
16639{
16640 setwinvar(argvars, rettv, 1);
16641}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642
16643/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016644 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016648 typval_T *argvars;
16649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016651 setwinvar(argvars, rettv, 0);
16652}
16653
16654/*
16655 * "setwinvar()" and "settabwinvar()" functions
16656 */
16657 static void
16658setwinvar(argvars, rettv, off)
16659 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016660 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016661 int off;
16662{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 win_T *win;
16664#ifdef FEAT_WINDOWS
16665 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016666 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667#endif
16668 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016669 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016671 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672
16673 if (check_restricted() || check_secure())
16674 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016675
16676#ifdef FEAT_WINDOWS
16677 if (off == 1)
16678 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16679 else
16680 tp = curtab;
16681#endif
16682 win = find_win_by_nr(&argvars[off], tp);
16683 varname = get_tv_string_chk(&argvars[off + 1]);
16684 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685
16686 if (win != NULL && varname != NULL && varp != NULL)
16687 {
16688#ifdef FEAT_WINDOWS
16689 /* set curwin to be our win, temporarily */
16690 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016691 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016692 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016693 if (!win_valid(win))
16694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 curwin = win;
16696 curbuf = curwin->w_buffer;
16697#endif
16698
16699 if (*varname == '&')
16700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016701 long numval;
16702 char_u *strval;
16703 int error = FALSE;
16704
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 numval = get_tv_number_chk(varp, &error);
16707 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 if (!error && strval != NULL)
16709 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 }
16711 else
16712 {
16713 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16714 if (winvarname != NULL)
16715 {
16716 STRCPY(winvarname, "w:");
16717 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016718 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 vim_free(winvarname);
16720 }
16721 }
16722
16723#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016724 /* Restore current tabpage and window, if still valid (autocomands can
16725 * make them invalid). */
16726 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016727 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 if (win_valid(save_curwin))
16729 {
16730 curwin = save_curwin;
16731 curbuf = curwin->w_buffer;
16732 }
16733#endif
16734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735}
16736
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016737#ifdef FEAT_CRYPT
16738/*
16739 * "sha256({string})" function
16740 */
16741 static void
16742f_sha256(argvars, rettv)
16743 typval_T *argvars;
16744 typval_T *rettv;
16745{
16746 char_u *p;
16747
16748 p = get_tv_string(&argvars[0]);
16749 rettv->vval.v_string = vim_strsave(
16750 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16751 rettv->v_type = VAR_STRING;
16752}
16753#endif /* FEAT_CRYPT */
16754
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016756 * "shellescape({string})" function
16757 */
16758 static void
16759f_shellescape(argvars, rettv)
16760 typval_T *argvars;
16761 typval_T *rettv;
16762{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016763 rettv->vval.v_string = vim_strsave_shellescape(
16764 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016765 rettv->v_type = VAR_STRING;
16766}
16767
16768/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016769 * shiftwidth() function
16770 */
16771 static void
16772f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016773 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016774 typval_T *rettv;
16775{
16776 rettv->vval.v_number = get_sw_value();
16777}
16778
16779/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 */
16782 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016784 typval_T *argvars;
16785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016787 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788
Bram Moolenaar0d660222005-01-07 21:51:51 +000016789 p = get_tv_string(&argvars[0]);
16790 rettv->vval.v_string = vim_strsave(p);
16791 simplify_filename(rettv->vval.v_string); /* simplify in place */
16792 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793}
16794
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016795#ifdef FEAT_FLOAT
16796/*
16797 * "sin()" function
16798 */
16799 static void
16800f_sin(argvars, rettv)
16801 typval_T *argvars;
16802 typval_T *rettv;
16803{
16804 float_T f;
16805
16806 rettv->v_type = VAR_FLOAT;
16807 if (get_float_arg(argvars, &f) == OK)
16808 rettv->vval.v_float = sin(f);
16809 else
16810 rettv->vval.v_float = 0.0;
16811}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016812
16813/*
16814 * "sinh()" function
16815 */
16816 static void
16817f_sinh(argvars, rettv)
16818 typval_T *argvars;
16819 typval_T *rettv;
16820{
16821 float_T f;
16822
16823 rettv->v_type = VAR_FLOAT;
16824 if (get_float_arg(argvars, &f) == OK)
16825 rettv->vval.v_float = sinh(f);
16826 else
16827 rettv->vval.v_float = 0.0;
16828}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016829#endif
16830
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831static int
16832#ifdef __BORLANDC__
16833 _RTLENTRYF
16834#endif
16835 item_compare __ARGS((const void *s1, const void *s2));
16836static int
16837#ifdef __BORLANDC__
16838 _RTLENTRYF
16839#endif
16840 item_compare2 __ARGS((const void *s1, const void *s2));
16841
16842static int item_compare_ic;
16843static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016844static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016845static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846#define ITEM_COMPARE_FAIL 999
16847
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851 static int
16852#ifdef __BORLANDC__
16853_RTLENTRYF
16854#endif
16855item_compare(s1, s2)
16856 const void *s1;
16857 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 char_u *p1, *p2;
16860 char_u *tofree1, *tofree2;
16861 int res;
16862 char_u numbuf1[NUMBUFLEN];
16863 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016865 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16866 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016867 if (p1 == NULL)
16868 p1 = (char_u *)"";
16869 if (p2 == NULL)
16870 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871 if (item_compare_ic)
16872 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 res = STRCMP(p1, p2);
16875 vim_free(tofree1);
16876 vim_free(tofree2);
16877 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878}
16879
16880 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881#ifdef __BORLANDC__
16882_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016884item_compare2(s1, s2)
16885 const void *s1;
16886 const void *s2;
16887{
16888 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016889 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016890 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016893 /* shortcut after failure in previous call; compare all items equal */
16894 if (item_compare_func_err)
16895 return 0;
16896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016897 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16898 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016899 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16900 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016901
16902 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016903 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016904 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16905 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 clear_tv(&argv[0]);
16907 clear_tv(&argv[1]);
16908
16909 if (res == FAIL)
16910 res = ITEM_COMPARE_FAIL;
16911 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16913 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016914 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915 clear_tv(&rettv);
16916 return res;
16917}
16918
16919/*
16920 * "sort({list})" function
16921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 typval_T *argvars;
16925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926{
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 list_T *l;
16928 listitem_T *li;
16929 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 long len;
16931 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 if (argvars[0].v_type != VAR_LIST)
16934 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 else
16936 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016938 if (l == NULL || tv_check_lock(l->lv_lock,
16939 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 return;
16941 rettv->vval.v_list = l;
16942 rettv->v_type = VAR_LIST;
16943 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 len = list_len(l);
16946 if (len <= 1)
16947 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 item_compare_ic = FALSE;
16950 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016951 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 if (argvars[1].v_type != VAR_UNKNOWN)
16953 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016954 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 else
16958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 int error = FALSE;
16960
16961 i = get_tv_number_chk(&argvars[1], &error);
16962 if (error)
16963 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 if (i == 1)
16965 item_compare_ic = TRUE;
16966 else
16967 item_compare_func = get_tv_string(&argvars[1]);
16968 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016969
16970 if (argvars[2].v_type != VAR_UNKNOWN)
16971 {
16972 /* optional third argument: {dict} */
16973 if (argvars[2].v_type != VAR_DICT)
16974 {
16975 EMSG(_(e_dictreq));
16976 return;
16977 }
16978 item_compare_selfdict = argvars[2].vval.v_dict;
16979 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 if (ptrs == NULL)
16985 return;
16986 i = 0;
16987 for (li = l->lv_first; li != NULL; li = li->li_next)
16988 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 /* test the compare function */
16992 if (item_compare_func != NULL
16993 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16994 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016995 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 {
16998 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016999 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 item_compare_func == NULL ? item_compare : item_compare2);
17001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 if (!item_compare_func_err)
17003 {
17004 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017005 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 l->lv_len = 0;
17007 for (i = 0; i < len; ++i)
17008 list_append(l, ptrs[i]);
17009 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010 }
17011
17012 vim_free(ptrs);
17013 }
17014}
17015
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017016/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017017 * "soundfold({word})" function
17018 */
17019 static void
17020f_soundfold(argvars, rettv)
17021 typval_T *argvars;
17022 typval_T *rettv;
17023{
17024 char_u *s;
17025
17026 rettv->v_type = VAR_STRING;
17027 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017028#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017029 rettv->vval.v_string = eval_soundfold(s);
17030#else
17031 rettv->vval.v_string = vim_strsave(s);
17032#endif
17033}
17034
17035/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017036 * "spellbadword()" function
17037 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017038 static void
17039f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017040 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017041 typval_T *rettv;
17042{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017043 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017044 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017045 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017046
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017047 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017048 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017049
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017050#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017051 if (argvars[0].v_type == VAR_UNKNOWN)
17052 {
17053 /* Find the start and length of the badly spelled word. */
17054 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17055 if (len != 0)
17056 word = ml_get_cursor();
17057 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017058 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017059 {
17060 char_u *str = get_tv_string_chk(&argvars[0]);
17061 int capcol = -1;
17062
17063 if (str != NULL)
17064 {
17065 /* Check the argument for spelling. */
17066 while (*str != NUL)
17067 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017068 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017069 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017070 {
17071 word = str;
17072 break;
17073 }
17074 str += len;
17075 }
17076 }
17077 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017078#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017079
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017080 list_append_string(rettv->vval.v_list, word, len);
17081 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017082 attr == HLF_SPB ? "bad" :
17083 attr == HLF_SPR ? "rare" :
17084 attr == HLF_SPL ? "local" :
17085 attr == HLF_SPC ? "caps" :
17086 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017087}
17088
17089/*
17090 * "spellsuggest()" function
17091 */
17092 static void
17093f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017094 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017095 typval_T *rettv;
17096{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017097#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017098 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017099 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017100 int maxcount;
17101 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017103 listitem_T *li;
17104 int need_capital = FALSE;
17105#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017106
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017107 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017108 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017109
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017110#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017111 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017112 {
17113 str = get_tv_string(&argvars[0]);
17114 if (argvars[1].v_type != VAR_UNKNOWN)
17115 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017116 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017117 if (maxcount <= 0)
17118 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017119 if (argvars[2].v_type != VAR_UNKNOWN)
17120 {
17121 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17122 if (typeerr)
17123 return;
17124 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017125 }
17126 else
17127 maxcount = 25;
17128
Bram Moolenaar4770d092006-01-12 23:22:24 +000017129 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017130
17131 for (i = 0; i < ga.ga_len; ++i)
17132 {
17133 str = ((char_u **)ga.ga_data)[i];
17134
17135 li = listitem_alloc();
17136 if (li == NULL)
17137 vim_free(str);
17138 else
17139 {
17140 li->li_tv.v_type = VAR_STRING;
17141 li->li_tv.v_lock = 0;
17142 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017143 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017144 }
17145 }
17146 ga_clear(&ga);
17147 }
17148#endif
17149}
17150
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017152f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017153 typval_T *argvars;
17154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017155{
17156 char_u *str;
17157 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017158 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159 regmatch_T regmatch;
17160 char_u patbuf[NUMBUFLEN];
17161 char_u *save_cpo;
17162 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017164 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166
17167 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17168 save_cpo = p_cpo;
17169 p_cpo = (char_u *)"";
17170
17171 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017172 if (argvars[1].v_type != VAR_UNKNOWN)
17173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017174 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17175 if (pat == NULL)
17176 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017177 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017178 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017179 }
17180 if (pat == NULL || *pat == NUL)
17181 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017183 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017185 if (typeerr)
17186 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17189 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017192 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017194 if (*str == NUL)
17195 match = FALSE; /* empty item at the end */
17196 else
17197 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 if (match)
17199 end = regmatch.startp[0];
17200 else
17201 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017202 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17203 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017204 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017205 if (list_append_string(rettv->vval.v_list, str,
17206 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017207 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208 }
17209 if (!match)
17210 break;
17211 /* Advance to just after the match. */
17212 if (regmatch.endp[0] > str)
17213 col = 0;
17214 else
17215 {
17216 /* Don't get stuck at the same match. */
17217#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017218 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219#else
17220 col = 1;
17221#endif
17222 }
17223 str = regmatch.endp[0];
17224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230}
17231
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017232#ifdef FEAT_FLOAT
17233/*
17234 * "sqrt()" function
17235 */
17236 static void
17237f_sqrt(argvars, rettv)
17238 typval_T *argvars;
17239 typval_T *rettv;
17240{
17241 float_T f;
17242
17243 rettv->v_type = VAR_FLOAT;
17244 if (get_float_arg(argvars, &f) == OK)
17245 rettv->vval.v_float = sqrt(f);
17246 else
17247 rettv->vval.v_float = 0.0;
17248}
17249
17250/*
17251 * "str2float()" function
17252 */
17253 static void
17254f_str2float(argvars, rettv)
17255 typval_T *argvars;
17256 typval_T *rettv;
17257{
17258 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17259
17260 if (*p == '+')
17261 p = skipwhite(p + 1);
17262 (void)string2float(p, &rettv->vval.v_float);
17263 rettv->v_type = VAR_FLOAT;
17264}
17265#endif
17266
Bram Moolenaar2c932302006-03-18 21:42:09 +000017267/*
17268 * "str2nr()" function
17269 */
17270 static void
17271f_str2nr(argvars, rettv)
17272 typval_T *argvars;
17273 typval_T *rettv;
17274{
17275 int base = 10;
17276 char_u *p;
17277 long n;
17278
17279 if (argvars[1].v_type != VAR_UNKNOWN)
17280 {
17281 base = get_tv_number(&argvars[1]);
17282 if (base != 8 && base != 10 && base != 16)
17283 {
17284 EMSG(_(e_invarg));
17285 return;
17286 }
17287 }
17288
17289 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017290 if (*p == '+')
17291 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017292 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17293 rettv->vval.v_number = n;
17294}
17295
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296#ifdef HAVE_STRFTIME
17297/*
17298 * "strftime({format}[, {time}])" function
17299 */
17300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 typval_T *argvars;
17303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304{
17305 char_u result_buf[256];
17306 struct tm *curtime;
17307 time_t seconds;
17308 char_u *p;
17309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017313 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 seconds = time(NULL);
17315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017316 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 curtime = localtime(&seconds);
17318 /* MSVC returns NULL for an invalid value of seconds. */
17319 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017320 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 else
17322 {
17323# ifdef FEAT_MBYTE
17324 vimconv_T conv;
17325 char_u *enc;
17326
17327 conv.vc_type = CONV_NONE;
17328 enc = enc_locale();
17329 convert_setup(&conv, p_enc, enc);
17330 if (conv.vc_type != CONV_NONE)
17331 p = string_convert(&conv, p, NULL);
17332# endif
17333 if (p != NULL)
17334 (void)strftime((char *)result_buf, sizeof(result_buf),
17335 (char *)p, curtime);
17336 else
17337 result_buf[0] = NUL;
17338
17339# ifdef FEAT_MBYTE
17340 if (conv.vc_type != CONV_NONE)
17341 vim_free(p);
17342 convert_setup(&conv, enc, p_enc);
17343 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017344 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 else
17346# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017347 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348
17349# ifdef FEAT_MBYTE
17350 /* Release conversion descriptors */
17351 convert_setup(&conv, NULL, NULL);
17352 vim_free(enc);
17353# endif
17354 }
17355}
17356#endif
17357
17358/*
17359 * "stridx()" function
17360 */
17361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 typval_T *argvars;
17364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365{
17366 char_u buf[NUMBUFLEN];
17367 char_u *needle;
17368 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 needle = get_tv_string_chk(&argvars[1]);
17374 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017375 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 if (needle == NULL || haystack == NULL)
17377 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 if (argvars[2].v_type != VAR_UNKNOWN)
17380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017381 int error = FALSE;
17382
17383 start_idx = get_tv_number_chk(&argvars[2], &error);
17384 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017385 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017386 if (start_idx >= 0)
17387 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 }
17389
17390 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17391 if (pos != NULL)
17392 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393}
17394
17395/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017396 * "string()" function
17397 */
17398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017399f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 typval_T *argvars;
17401 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017402{
17403 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017404 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017407 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017408 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017409 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411}
17412
17413/*
17414 * "strlen()" function
17415 */
17416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017417f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017418 typval_T *argvars;
17419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_number = (varnumber_T)(STRLEN(
17422 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423}
17424
17425/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017426 * "strchars()" function
17427 */
17428 static void
17429f_strchars(argvars, rettv)
17430 typval_T *argvars;
17431 typval_T *rettv;
17432{
17433 char_u *s = get_tv_string(&argvars[0]);
17434#ifdef FEAT_MBYTE
17435 varnumber_T len = 0;
17436
17437 while (*s != NUL)
17438 {
17439 mb_cptr2char_adv(&s);
17440 ++len;
17441 }
17442 rettv->vval.v_number = len;
17443#else
17444 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17445#endif
17446}
17447
17448/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017449 * "strdisplaywidth()" function
17450 */
17451 static void
17452f_strdisplaywidth(argvars, rettv)
17453 typval_T *argvars;
17454 typval_T *rettv;
17455{
17456 char_u *s = get_tv_string(&argvars[0]);
17457 int col = 0;
17458
17459 if (argvars[1].v_type != VAR_UNKNOWN)
17460 col = get_tv_number(&argvars[1]);
17461
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017462 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017463}
17464
17465/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017466 * "strwidth()" function
17467 */
17468 static void
17469f_strwidth(argvars, rettv)
17470 typval_T *argvars;
17471 typval_T *rettv;
17472{
17473 char_u *s = get_tv_string(&argvars[0]);
17474
17475 rettv->vval.v_number = (varnumber_T)(
17476#ifdef FEAT_MBYTE
17477 mb_string2cells(s, -1)
17478#else
17479 STRLEN(s)
17480#endif
17481 );
17482}
17483
17484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 * "strpart()" function
17486 */
17487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 typval_T *argvars;
17490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491{
17492 char_u *p;
17493 int n;
17494 int len;
17495 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017496 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017498 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 slen = (int)STRLEN(p);
17500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017501 n = get_tv_number_chk(&argvars[1], &error);
17502 if (error)
17503 len = 0;
17504 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 else
17507 len = slen - n; /* default len: all bytes that are available. */
17508
17509 /*
17510 * Only return the overlap between the specified part and the actual
17511 * string.
17512 */
17513 if (n < 0)
17514 {
17515 len += n;
17516 n = 0;
17517 }
17518 else if (n > slen)
17519 n = slen;
17520 if (len < 0)
17521 len = 0;
17522 else if (n + len > slen)
17523 len = slen - n;
17524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525 rettv->v_type = VAR_STRING;
17526 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527}
17528
17529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530 * "strridx()" function
17531 */
17532 static void
17533f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 typval_T *argvars;
17535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017536{
17537 char_u buf[NUMBUFLEN];
17538 char_u *needle;
17539 char_u *haystack;
17540 char_u *rest;
17541 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017542 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 needle = get_tv_string_chk(&argvars[1]);
17545 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017546
17547 rettv->vval.v_number = -1;
17548 if (needle == NULL || haystack == NULL)
17549 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017550
17551 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017552 if (argvars[2].v_type != VAR_UNKNOWN)
17553 {
17554 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017555 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017556 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017557 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017558 }
17559 else
17560 end_idx = haystack_len;
17561
Bram Moolenaar0d660222005-01-07 21:51:51 +000017562 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017563 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017565 lastmatch = haystack + end_idx;
17566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017568 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 for (rest = haystack; *rest != '\0'; ++rest)
17570 {
17571 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017572 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 break;
17574 lastmatch = rest;
17575 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017576 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017577
17578 if (lastmatch == NULL)
17579 rettv->vval.v_number = -1;
17580 else
17581 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17582}
17583
17584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 * "strtrans()" function
17586 */
17587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017588f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 typval_T *argvars;
17590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017592 rettv->v_type = VAR_STRING;
17593 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594}
17595
17596/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597 * "submatch()" function
17598 */
17599 static void
17600f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017601 typval_T *argvars;
17602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603{
17604 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017605 rettv->vval.v_string =
17606 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017607}
17608
17609/*
17610 * "substitute()" function
17611 */
17612 static void
17613f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 typval_T *argvars;
17615 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616{
17617 char_u patbuf[NUMBUFLEN];
17618 char_u subbuf[NUMBUFLEN];
17619 char_u flagsbuf[NUMBUFLEN];
17620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017621 char_u *str = get_tv_string_chk(&argvars[0]);
17622 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17623 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17624 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17625
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17628 rettv->vval.v_string = NULL;
17629 else
17630 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017631}
17632
17633/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017634 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017637f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640{
17641 int id = 0;
17642#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017643 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 long col;
17645 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017646 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017648 lnum = get_tv_lnum(argvars); /* -1 on type error */
17649 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17650 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017652 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017653 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017654 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655#endif
17656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017657 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658}
17659
17660/*
17661 * "synIDattr(id, what [, mode])" function
17662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667{
17668 char_u *p = NULL;
17669#ifdef FEAT_SYN_HL
17670 int id;
17671 char_u *what;
17672 char_u *mode;
17673 char_u modebuf[NUMBUFLEN];
17674 int modec;
17675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 id = get_tv_number(&argvars[0]);
17677 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017678 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017682 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 modec = 0; /* replace invalid with current */
17684 }
17685 else
17686 {
17687#ifdef FEAT_GUI
17688 if (gui.in_use)
17689 modec = 'g';
17690 else
17691#endif
17692 if (t_colors > 1)
17693 modec = 'c';
17694 else
17695 modec = 't';
17696 }
17697
17698
17699 switch (TOLOWER_ASC(what[0]))
17700 {
17701 case 'b':
17702 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17703 p = highlight_color(id, what, modec);
17704 else /* bold */
17705 p = highlight_has_attr(id, HL_BOLD, modec);
17706 break;
17707
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017708 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 p = highlight_color(id, what, modec);
17710 break;
17711
17712 case 'i':
17713 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17714 p = highlight_has_attr(id, HL_INVERSE, modec);
17715 else /* italic */
17716 p = highlight_has_attr(id, HL_ITALIC, modec);
17717 break;
17718
17719 case 'n': /* name */
17720 p = get_highlight_name(NULL, id - 1);
17721 break;
17722
17723 case 'r': /* reverse */
17724 p = highlight_has_attr(id, HL_INVERSE, modec);
17725 break;
17726
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017727 case 's':
17728 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17729 p = highlight_color(id, what, modec);
17730 else /* standout */
17731 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 break;
17733
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017734 case 'u':
17735 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17736 /* underline */
17737 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17738 else
17739 /* undercurl */
17740 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 break;
17742 }
17743
17744 if (p != NULL)
17745 p = vim_strsave(p);
17746#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017747 rettv->v_type = VAR_STRING;
17748 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749}
17750
17751/*
17752 * "synIDtrans(id)" function
17753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758{
17759 int id;
17760
17761#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763
17764 if (id > 0)
17765 id = syn_get_final_id(id);
17766 else
17767#endif
17768 id = 0;
17769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017770 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771}
17772
17773/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017774 * "synconcealed(lnum, col)" function
17775 */
17776 static void
17777f_synconcealed(argvars, rettv)
17778 typval_T *argvars UNUSED;
17779 typval_T *rettv;
17780{
17781#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17782 long lnum;
17783 long col;
17784 int syntax_flags = 0;
17785 int cchar;
17786 int matchid = 0;
17787 char_u str[NUMBUFLEN];
17788#endif
17789
17790 rettv->v_type = VAR_LIST;
17791 rettv->vval.v_list = NULL;
17792
17793#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17794 lnum = get_tv_lnum(argvars); /* -1 on type error */
17795 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17796
17797 vim_memset(str, NUL, sizeof(str));
17798
17799 if (rettv_list_alloc(rettv) != FAIL)
17800 {
17801 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17802 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17803 && curwin->w_p_cole > 0)
17804 {
17805 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17806 syntax_flags = get_syntax_info(&matchid);
17807
17808 /* get the conceal character */
17809 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17810 {
17811 cchar = syn_get_sub_char();
17812 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17813 cchar = lcs_conceal;
17814 if (cchar != NUL)
17815 {
17816# ifdef FEAT_MBYTE
17817 if (has_mbyte)
17818 (*mb_char2bytes)(cchar, str);
17819 else
17820# endif
17821 str[0] = cchar;
17822 }
17823 }
17824 }
17825
17826 list_append_number(rettv->vval.v_list,
17827 (syntax_flags & HL_CONCEAL) != 0);
17828 /* -1 to auto-determine strlen */
17829 list_append_string(rettv->vval.v_list, str, -1);
17830 list_append_number(rettv->vval.v_list, matchid);
17831 }
17832#endif
17833}
17834
17835/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017836 * "synstack(lnum, col)" function
17837 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017838 static void
17839f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017840 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017841 typval_T *rettv;
17842{
17843#ifdef FEAT_SYN_HL
17844 long lnum;
17845 long col;
17846 int i;
17847 int id;
17848#endif
17849
17850 rettv->v_type = VAR_LIST;
17851 rettv->vval.v_list = NULL;
17852
17853#ifdef FEAT_SYN_HL
17854 lnum = get_tv_lnum(argvars); /* -1 on type error */
17855 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17856
17857 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017858 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017859 && rettv_list_alloc(rettv) != FAIL)
17860 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017861 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017862 for (i = 0; ; ++i)
17863 {
17864 id = syn_get_stack_item(i);
17865 if (id < 0)
17866 break;
17867 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17868 break;
17869 }
17870 }
17871#endif
17872}
17873
17874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 * "system()" function
17876 */
17877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017878f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017879 typval_T *argvars;
17880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017882 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017884 char_u *infile = NULL;
17885 char_u buf[NUMBUFLEN];
17886 int err = FALSE;
17887 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017889 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017890 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017892 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017893 {
17894 /*
17895 * Write the string to a temp file, to be used for input of the shell
17896 * command.
17897 */
17898 if ((infile = vim_tempname('i')) == NULL)
17899 {
17900 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017901 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017902 }
17903
17904 fd = mch_fopen((char *)infile, WRITEBIN);
17905 if (fd == NULL)
17906 {
17907 EMSG2(_(e_notopen), infile);
17908 goto done;
17909 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017910 p = get_tv_string_buf_chk(&argvars[1], buf);
17911 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017912 {
17913 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017914 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017915 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017916 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17917 err = TRUE;
17918 if (fclose(fd) != 0)
17919 err = TRUE;
17920 if (err)
17921 {
17922 EMSG(_("E677: Error writing temp file"));
17923 goto done;
17924 }
17925 }
17926
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017927 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17928 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017929
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930#ifdef USE_CR
17931 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017932 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 {
17934 char_u *s;
17935
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017936 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 {
17938 if (*s == CAR)
17939 *s = NL;
17940 }
17941 }
17942#else
17943# ifdef USE_CRNL
17944 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017945 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 {
17947 char_u *s, *d;
17948
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017949 d = res;
17950 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 {
17952 if (s[0] == CAR && s[1] == NL)
17953 ++s;
17954 *d++ = *s;
17955 }
17956 *d = NUL;
17957 }
17958# endif
17959#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017960
17961done:
17962 if (infile != NULL)
17963 {
17964 mch_remove(infile);
17965 vim_free(infile);
17966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017967 rettv->v_type = VAR_STRING;
17968 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969}
17970
17971/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017972 * "tabpagebuflist()" function
17973 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017974 static void
17975f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017976 typval_T *argvars UNUSED;
17977 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017978{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017979#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017980 tabpage_T *tp;
17981 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017982
17983 if (argvars[0].v_type == VAR_UNKNOWN)
17984 wp = firstwin;
17985 else
17986 {
17987 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17988 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017989 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017990 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017991 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017992 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017993 for (; wp != NULL; wp = wp->w_next)
17994 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017995 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017996 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017997 }
17998#endif
17999}
18000
18001
18002/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018003 * "tabpagenr()" function
18004 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018005 static void
18006f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018007 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018008 typval_T *rettv;
18009{
18010 int nr = 1;
18011#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018012 char_u *arg;
18013
18014 if (argvars[0].v_type != VAR_UNKNOWN)
18015 {
18016 arg = get_tv_string_chk(&argvars[0]);
18017 nr = 0;
18018 if (arg != NULL)
18019 {
18020 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018021 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018022 else
18023 EMSG2(_(e_invexpr2), arg);
18024 }
18025 }
18026 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018027 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018028#endif
18029 rettv->vval.v_number = nr;
18030}
18031
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018032
18033#ifdef FEAT_WINDOWS
18034static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18035
18036/*
18037 * Common code for tabpagewinnr() and winnr().
18038 */
18039 static int
18040get_winnr(tp, argvar)
18041 tabpage_T *tp;
18042 typval_T *argvar;
18043{
18044 win_T *twin;
18045 int nr = 1;
18046 win_T *wp;
18047 char_u *arg;
18048
18049 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18050 if (argvar->v_type != VAR_UNKNOWN)
18051 {
18052 arg = get_tv_string_chk(argvar);
18053 if (arg == NULL)
18054 nr = 0; /* type error; errmsg already given */
18055 else if (STRCMP(arg, "$") == 0)
18056 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18057 else if (STRCMP(arg, "#") == 0)
18058 {
18059 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18060 if (twin == NULL)
18061 nr = 0;
18062 }
18063 else
18064 {
18065 EMSG2(_(e_invexpr2), arg);
18066 nr = 0;
18067 }
18068 }
18069
18070 if (nr > 0)
18071 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18072 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018073 {
18074 if (wp == NULL)
18075 {
18076 /* didn't find it in this tabpage */
18077 nr = 0;
18078 break;
18079 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018080 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018081 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018082 return nr;
18083}
18084#endif
18085
18086/*
18087 * "tabpagewinnr()" function
18088 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018089 static void
18090f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018091 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018092 typval_T *rettv;
18093{
18094 int nr = 1;
18095#ifdef FEAT_WINDOWS
18096 tabpage_T *tp;
18097
18098 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18099 if (tp == NULL)
18100 nr = 0;
18101 else
18102 nr = get_winnr(tp, &argvars[1]);
18103#endif
18104 rettv->vval.v_number = nr;
18105}
18106
18107
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018108/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018109 * "tagfiles()" function
18110 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018111 static void
18112f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018113 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018114 typval_T *rettv;
18115{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018116 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018117 tagname_T tn;
18118 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018120 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018121 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018122 fname = alloc(MAXPATHL);
18123 if (fname == NULL)
18124 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018125
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018126 for (first = TRUE; ; first = FALSE)
18127 if (get_tagfname(&tn, first, fname) == FAIL
18128 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018129 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018130 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018131 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018132}
18133
18134/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018135 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018136 */
18137 static void
18138f_taglist(argvars, rettv)
18139 typval_T *argvars;
18140 typval_T *rettv;
18141{
18142 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018143
18144 tag_pattern = get_tv_string(&argvars[0]);
18145
18146 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018147 if (*tag_pattern == NUL)
18148 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018149
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018150 if (rettv_list_alloc(rettv) == OK)
18151 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018152}
18153
18154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 * "tempname()" function
18156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018158f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018159 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161{
18162 static int x = 'A';
18163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018164 rettv->v_type = VAR_STRING;
18165 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166
18167 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18168 * names. Skip 'I' and 'O', they are used for shell redirection. */
18169 do
18170 {
18171 if (x == 'Z')
18172 x = '0';
18173 else if (x == '9')
18174 x = 'A';
18175 else
18176 {
18177#ifdef EBCDIC
18178 if (x == 'I')
18179 x = 'J';
18180 else if (x == 'R')
18181 x = 'S';
18182 else
18183#endif
18184 ++x;
18185 }
18186 } while (x == 'I' || x == 'O');
18187}
18188
18189/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018190 * "test(list)" function: Just checking the walls...
18191 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018192 static void
18193f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018194 typval_T *argvars UNUSED;
18195 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018196{
18197 /* Used for unit testing. Change the code below to your liking. */
18198#if 0
18199 listitem_T *li;
18200 list_T *l;
18201 char_u *bad, *good;
18202
18203 if (argvars[0].v_type != VAR_LIST)
18204 return;
18205 l = argvars[0].vval.v_list;
18206 if (l == NULL)
18207 return;
18208 li = l->lv_first;
18209 if (li == NULL)
18210 return;
18211 bad = get_tv_string(&li->li_tv);
18212 li = li->li_next;
18213 if (li == NULL)
18214 return;
18215 good = get_tv_string(&li->li_tv);
18216 rettv->vval.v_number = test_edit_score(bad, good);
18217#endif
18218}
18219
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018220#ifdef FEAT_FLOAT
18221/*
18222 * "tan()" function
18223 */
18224 static void
18225f_tan(argvars, rettv)
18226 typval_T *argvars;
18227 typval_T *rettv;
18228{
18229 float_T f;
18230
18231 rettv->v_type = VAR_FLOAT;
18232 if (get_float_arg(argvars, &f) == OK)
18233 rettv->vval.v_float = tan(f);
18234 else
18235 rettv->vval.v_float = 0.0;
18236}
18237
18238/*
18239 * "tanh()" function
18240 */
18241 static void
18242f_tanh(argvars, rettv)
18243 typval_T *argvars;
18244 typval_T *rettv;
18245{
18246 float_T f;
18247
18248 rettv->v_type = VAR_FLOAT;
18249 if (get_float_arg(argvars, &f) == OK)
18250 rettv->vval.v_float = tanh(f);
18251 else
18252 rettv->vval.v_float = 0.0;
18253}
18254#endif
18255
Bram Moolenaard52d9742005-08-21 22:20:28 +000018256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 * "tolower(string)" function
18258 */
18259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018260f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018261 typval_T *argvars;
18262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263{
18264 char_u *p;
18265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018266 p = vim_strsave(get_tv_string(&argvars[0]));
18267 rettv->v_type = VAR_STRING;
18268 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269
18270 if (p != NULL)
18271 while (*p != NUL)
18272 {
18273#ifdef FEAT_MBYTE
18274 int l;
18275
18276 if (enc_utf8)
18277 {
18278 int c, lc;
18279
18280 c = utf_ptr2char(p);
18281 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018282 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 /* TODO: reallocate string when byte count changes. */
18284 if (utf_char2len(lc) == l)
18285 utf_char2bytes(lc, p);
18286 p += l;
18287 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018288 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 p += l; /* skip multi-byte character */
18290 else
18291#endif
18292 {
18293 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18294 ++p;
18295 }
18296 }
18297}
18298
18299/*
18300 * "toupper(string)" function
18301 */
18302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018303f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018304 typval_T *argvars;
18305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018308 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309}
18310
18311/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018312 * "tr(string, fromstr, tostr)" function
18313 */
18314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018316 typval_T *argvars;
18317 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018318{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018319 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018320 char_u *fromstr;
18321 char_u *tostr;
18322 char_u *p;
18323#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018324 int inlen;
18325 int fromlen;
18326 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018327 int idx;
18328 char_u *cpstr;
18329 int cplen;
18330 int first = TRUE;
18331#endif
18332 char_u buf[NUMBUFLEN];
18333 char_u buf2[NUMBUFLEN];
18334 garray_T ga;
18335
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018336 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018337 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18338 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018339
18340 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018341 rettv->v_type = VAR_STRING;
18342 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018343 if (fromstr == NULL || tostr == NULL)
18344 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018345 ga_init2(&ga, (int)sizeof(char), 80);
18346
18347#ifdef FEAT_MBYTE
18348 if (!has_mbyte)
18349#endif
18350 /* not multi-byte: fromstr and tostr must be the same length */
18351 if (STRLEN(fromstr) != STRLEN(tostr))
18352 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018353#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018354error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018355#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018356 EMSG2(_(e_invarg2), fromstr);
18357 ga_clear(&ga);
18358 return;
18359 }
18360
18361 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018362 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018363 {
18364#ifdef FEAT_MBYTE
18365 if (has_mbyte)
18366 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018367 inlen = (*mb_ptr2len)(in_str);
18368 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018369 cplen = inlen;
18370 idx = 0;
18371 for (p = fromstr; *p != NUL; p += fromlen)
18372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018373 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018374 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018375 {
18376 for (p = tostr; *p != NUL; p += tolen)
18377 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018378 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018379 if (idx-- == 0)
18380 {
18381 cplen = tolen;
18382 cpstr = p;
18383 break;
18384 }
18385 }
18386 if (*p == NUL) /* tostr is shorter than fromstr */
18387 goto error;
18388 break;
18389 }
18390 ++idx;
18391 }
18392
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018393 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018394 {
18395 /* Check that fromstr and tostr have the same number of
18396 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018397 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018398 first = FALSE;
18399 for (p = tostr; *p != NUL; p += tolen)
18400 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018401 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018402 --idx;
18403 }
18404 if (idx != 0)
18405 goto error;
18406 }
18407
18408 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018409 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018410 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018411
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018412 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018413 }
18414 else
18415#endif
18416 {
18417 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018418 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018419 if (p != NULL)
18420 ga_append(&ga, tostr[p - fromstr]);
18421 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018422 ga_append(&ga, *in_str);
18423 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018424 }
18425 }
18426
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018427 /* add a terminating NUL */
18428 ga_grow(&ga, 1);
18429 ga_append(&ga, NUL);
18430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018432}
18433
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018434#ifdef FEAT_FLOAT
18435/*
18436 * "trunc({float})" function
18437 */
18438 static void
18439f_trunc(argvars, rettv)
18440 typval_T *argvars;
18441 typval_T *rettv;
18442{
18443 float_T f;
18444
18445 rettv->v_type = VAR_FLOAT;
18446 if (get_float_arg(argvars, &f) == OK)
18447 /* trunc() is not in C90, use floor() or ceil() instead. */
18448 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18449 else
18450 rettv->vval.v_float = 0.0;
18451}
18452#endif
18453
Bram Moolenaar8299df92004-07-10 09:47:34 +000018454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 * "type(expr)" function
18456 */
18457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018458f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018459 typval_T *argvars;
18460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018462 int n;
18463
18464 switch (argvars[0].v_type)
18465 {
18466 case VAR_NUMBER: n = 0; break;
18467 case VAR_STRING: n = 1; break;
18468 case VAR_FUNC: n = 2; break;
18469 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018470 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018471#ifdef FEAT_FLOAT
18472 case VAR_FLOAT: n = 5; break;
18473#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018474 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18475 }
18476 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477}
18478
18479/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018480 * "undofile(name)" function
18481 */
18482 static void
18483f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018484 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018485 typval_T *rettv;
18486{
18487 rettv->v_type = VAR_STRING;
18488#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018489 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018490 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018491
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018492 if (*fname == NUL)
18493 {
18494 /* If there is no file name there will be no undo file. */
18495 rettv->vval.v_string = NULL;
18496 }
18497 else
18498 {
18499 char_u *ffname = FullName_save(fname, FALSE);
18500
18501 if (ffname != NULL)
18502 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18503 vim_free(ffname);
18504 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018505 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018506#else
18507 rettv->vval.v_string = NULL;
18508#endif
18509}
18510
18511/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018512 * "undotree()" function
18513 */
18514 static void
18515f_undotree(argvars, rettv)
18516 typval_T *argvars UNUSED;
18517 typval_T *rettv;
18518{
18519 if (rettv_dict_alloc(rettv) == OK)
18520 {
18521 dict_T *dict = rettv->vval.v_dict;
18522 list_T *list;
18523
Bram Moolenaar730cde92010-06-27 05:18:54 +020018524 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018525 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018526 dict_add_nr_str(dict, "save_last",
18527 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018528 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18529 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018530 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018531
18532 list = list_alloc();
18533 if (list != NULL)
18534 {
18535 u_eval_tree(curbuf->b_u_oldhead, list);
18536 dict_add_list(dict, "entries", list);
18537 }
18538 }
18539}
18540
18541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018542 * "values(dict)" function
18543 */
18544 static void
18545f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018546 typval_T *argvars;
18547 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018548{
18549 dict_list(argvars, rettv, 1);
18550}
18551
18552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 * "virtcol(string)" function
18554 */
18555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018556f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018557 typval_T *argvars;
18558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559{
18560 colnr_T vcol = 0;
18561 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018562 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018564 fp = var2fpos(&argvars[0], FALSE, &fnum);
18565 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18566 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 {
18568 getvvcol(curwin, fp, NULL, NULL, &vcol);
18569 ++vcol;
18570 }
18571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573}
18574
18575/*
18576 * "visualmode()" function
18577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018580 typval_T *argvars UNUSED;
18581 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582{
18583#ifdef FEAT_VISUAL
18584 char_u str[2];
18585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018586 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 str[0] = curbuf->b_visual_mode_eval;
18588 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590
18591 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018592 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594#endif
18595}
18596
18597/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018598 * "wildmenumode()" function
18599 */
18600 static void
18601f_wildmenumode(argvars, rettv)
18602 typval_T *argvars UNUSED;
18603 typval_T *rettv UNUSED;
18604{
18605#ifdef FEAT_WILDMENU
18606 if (wild_menu_showing)
18607 rettv->vval.v_number = 1;
18608#endif
18609}
18610
18611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 * "winbufnr(nr)" function
18613 */
18614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018616 typval_T *argvars;
18617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618{
18619 win_T *wp;
18620
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018621 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626}
18627
18628/*
18629 * "wincol()" function
18630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018632f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018633 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635{
18636 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638}
18639
18640/*
18641 * "winheight(nr)" function
18642 */
18643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018645 typval_T *argvars;
18646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647{
18648 win_T *wp;
18649
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018650 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655}
18656
18657/*
18658 * "winline()" function
18659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664{
18665 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667}
18668
18669/*
18670 * "winnr()" function
18671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018673f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018674 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676{
18677 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018678
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018680 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683}
18684
18685/*
18686 * "winrestcmd()" function
18687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018689f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692{
18693#ifdef FEAT_WINDOWS
18694 win_T *wp;
18695 int winnr = 1;
18696 garray_T ga;
18697 char_u buf[50];
18698
18699 ga_init2(&ga, (int)sizeof(char), 70);
18700 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18701 {
18702 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18703 ga_concat(&ga, buf);
18704# ifdef FEAT_VERTSPLIT
18705 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18706 ga_concat(&ga, buf);
18707# endif
18708 ++winnr;
18709 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018710 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018712 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018716 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717}
18718
18719/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018720 * "winrestview()" function
18721 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018722 static void
18723f_winrestview(argvars, rettv)
18724 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018725 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018726{
18727 dict_T *dict;
18728
18729 if (argvars[0].v_type != VAR_DICT
18730 || (dict = argvars[0].vval.v_dict) == NULL)
18731 EMSG(_(e_invarg));
18732 else
18733 {
18734 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18735 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18736#ifdef FEAT_VIRTUALEDIT
18737 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18738#endif
18739 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018740 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018741
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018742 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018743#ifdef FEAT_DIFF
18744 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18745#endif
18746 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18747 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18748
18749 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018750 win_new_height(curwin, curwin->w_height);
18751# ifdef FEAT_VERTSPLIT
18752 win_new_width(curwin, W_WIDTH(curwin));
18753# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018754 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018755
18756 if (curwin->w_topline == 0)
18757 curwin->w_topline = 1;
18758 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18759 curwin->w_topline = curbuf->b_ml.ml_line_count;
18760#ifdef FEAT_DIFF
18761 check_topfill(curwin, TRUE);
18762#endif
18763 }
18764}
18765
18766/*
18767 * "winsaveview()" function
18768 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018769 static void
18770f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018771 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018772 typval_T *rettv;
18773{
18774 dict_T *dict;
18775
Bram Moolenaara800b422010-06-27 01:15:55 +020018776 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018777 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018778 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018779
18780 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18781 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18782#ifdef FEAT_VIRTUALEDIT
18783 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18784#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018785 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018786 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18787
18788 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18789#ifdef FEAT_DIFF
18790 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18791#endif
18792 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18793 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18794}
18795
18796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 * "winwidth(nr)" function
18798 */
18799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018801 typval_T *argvars;
18802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803{
18804 win_T *wp;
18805
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018806 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 else
18810#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814#endif
18815}
18816
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018818 * "writefile()" function
18819 */
18820 static void
18821f_writefile(argvars, rettv)
18822 typval_T *argvars;
18823 typval_T *rettv;
18824{
18825 int binary = FALSE;
18826 char_u *fname;
18827 FILE *fd;
18828 listitem_T *li;
18829 char_u *s;
18830 int ret = 0;
18831 int c;
18832
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018833 if (check_restricted() || check_secure())
18834 return;
18835
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018836 if (argvars[0].v_type != VAR_LIST)
18837 {
18838 EMSG2(_(e_listarg), "writefile()");
18839 return;
18840 }
18841 if (argvars[0].vval.v_list == NULL)
18842 return;
18843
18844 if (argvars[2].v_type != VAR_UNKNOWN
18845 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18846 binary = TRUE;
18847
18848 /* Always open the file in binary mode, library functions have a mind of
18849 * their own about CR-LF conversion. */
18850 fname = get_tv_string(&argvars[1]);
18851 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18852 {
18853 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18854 ret = -1;
18855 }
18856 else
18857 {
18858 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18859 li = li->li_next)
18860 {
18861 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18862 {
18863 if (*s == '\n')
18864 c = putc(NUL, fd);
18865 else
18866 c = putc(*s, fd);
18867 if (c == EOF)
18868 {
18869 ret = -1;
18870 break;
18871 }
18872 }
18873 if (!binary || li->li_next != NULL)
18874 if (putc('\n', fd) == EOF)
18875 {
18876 ret = -1;
18877 break;
18878 }
18879 if (ret < 0)
18880 {
18881 EMSG(_(e_write));
18882 break;
18883 }
18884 }
18885 fclose(fd);
18886 }
18887
18888 rettv->vval.v_number = ret;
18889}
18890
18891/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018892 * "xor(expr, expr)" function
18893 */
18894 static void
18895f_xor(argvars, rettv)
18896 typval_T *argvars;
18897 typval_T *rettv;
18898{
18899 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18900 ^ get_tv_number_chk(&argvars[1], NULL);
18901}
18902
18903
18904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018906 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 */
18908 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018909var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018911 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018912 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018914 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018916 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917
Bram Moolenaara5525202006-03-02 22:52:09 +000018918 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018919 if (varp->v_type == VAR_LIST)
18920 {
18921 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018922 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018923 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018924 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018925
18926 l = varp->vval.v_list;
18927 if (l == NULL)
18928 return NULL;
18929
18930 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018931 pos.lnum = list_find_nr(l, 0L, &error);
18932 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018933 return NULL; /* invalid line number */
18934
18935 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018936 pos.col = list_find_nr(l, 1L, &error);
18937 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018938 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018939 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018940
18941 /* We accept "$" for the column number: last column. */
18942 li = list_find(l, 1L);
18943 if (li != NULL && li->li_tv.v_type == VAR_STRING
18944 && li->li_tv.vval.v_string != NULL
18945 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18946 pos.col = len + 1;
18947
Bram Moolenaara5525202006-03-02 22:52:09 +000018948 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018949 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018950 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018951 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018952
Bram Moolenaara5525202006-03-02 22:52:09 +000018953#ifdef FEAT_VIRTUALEDIT
18954 /* Get the virtual offset. Defaults to zero. */
18955 pos.coladd = list_find_nr(l, 2L, &error);
18956 if (error)
18957 pos.coladd = 0;
18958#endif
18959
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018960 return &pos;
18961 }
18962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018963 name = get_tv_string_chk(varp);
18964 if (name == NULL)
18965 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018966 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018968#ifdef FEAT_VISUAL
18969 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18970 {
18971 if (VIsual_active)
18972 return &VIsual;
18973 return &curwin->w_cursor;
18974 }
18975#endif
18976 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018978 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18980 return NULL;
18981 return pp;
18982 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018983
18984#ifdef FEAT_VIRTUALEDIT
18985 pos.coladd = 0;
18986#endif
18987
Bram Moolenaar477933c2007-07-17 14:32:23 +000018988 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018989 {
18990 pos.col = 0;
18991 if (name[1] == '0') /* "w0": first visible line */
18992 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018993 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018994 pos.lnum = curwin->w_topline;
18995 return &pos;
18996 }
18997 else if (name[1] == '$') /* "w$": last visible line */
18998 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018999 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019000 pos.lnum = curwin->w_botline - 1;
19001 return &pos;
19002 }
19003 }
19004 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019006 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 {
19008 pos.lnum = curbuf->b_ml.ml_line_count;
19009 pos.col = 0;
19010 }
19011 else
19012 {
19013 pos.lnum = curwin->w_cursor.lnum;
19014 pos.col = (colnr_T)STRLEN(ml_get_curline());
19015 }
19016 return &pos;
19017 }
19018 return NULL;
19019}
19020
19021/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019022 * Convert list in "arg" into a position and optional file number.
19023 * When "fnump" is NULL there is no file number, only 3 items.
19024 * Note that the column is passed on as-is, the caller may want to decrement
19025 * it to use 1 for the first column.
19026 * Return FAIL when conversion is not possible, doesn't check the position for
19027 * validity.
19028 */
19029 static int
19030list2fpos(arg, posp, fnump)
19031 typval_T *arg;
19032 pos_T *posp;
19033 int *fnump;
19034{
19035 list_T *l = arg->vval.v_list;
19036 long i = 0;
19037 long n;
19038
Bram Moolenaarbde35262006-07-23 20:12:24 +000019039 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19040 * when "fnump" isn't NULL and "coladd" is optional. */
19041 if (arg->v_type != VAR_LIST
19042 || l == NULL
19043 || l->lv_len < (fnump == NULL ? 2 : 3)
19044 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019045 return FAIL;
19046
19047 if (fnump != NULL)
19048 {
19049 n = list_find_nr(l, i++, NULL); /* fnum */
19050 if (n < 0)
19051 return FAIL;
19052 if (n == 0)
19053 n = curbuf->b_fnum; /* current buffer */
19054 *fnump = n;
19055 }
19056
19057 n = list_find_nr(l, i++, NULL); /* lnum */
19058 if (n < 0)
19059 return FAIL;
19060 posp->lnum = n;
19061
19062 n = list_find_nr(l, i++, NULL); /* col */
19063 if (n < 0)
19064 return FAIL;
19065 posp->col = n;
19066
19067#ifdef FEAT_VIRTUALEDIT
19068 n = list_find_nr(l, i, NULL);
19069 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019070 posp->coladd = 0;
19071 else
19072 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019073#endif
19074
19075 return OK;
19076}
19077
19078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 * Get the length of an environment variable name.
19080 * Advance "arg" to the first character after the name.
19081 * Return 0 for error.
19082 */
19083 static int
19084get_env_len(arg)
19085 char_u **arg;
19086{
19087 char_u *p;
19088 int len;
19089
19090 for (p = *arg; vim_isIDc(*p); ++p)
19091 ;
19092 if (p == *arg) /* no name found */
19093 return 0;
19094
19095 len = (int)(p - *arg);
19096 *arg = p;
19097 return len;
19098}
19099
19100/*
19101 * Get the length of the name of a function or internal variable.
19102 * "arg" is advanced to the first non-white character after the name.
19103 * Return 0 if something is wrong.
19104 */
19105 static int
19106get_id_len(arg)
19107 char_u **arg;
19108{
19109 char_u *p;
19110 int len;
19111
19112 /* Find the end of the name. */
19113 for (p = *arg; eval_isnamec(*p); ++p)
19114 ;
19115 if (p == *arg) /* no name found */
19116 return 0;
19117
19118 len = (int)(p - *arg);
19119 *arg = skipwhite(p);
19120
19121 return len;
19122}
19123
19124/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019125 * Get the length of the name of a variable or function.
19126 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019128 * Return -1 if curly braces expansion failed.
19129 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 * If the name contains 'magic' {}'s, expand them and return the
19131 * expanded name in an allocated string via 'alias' - caller must free.
19132 */
19133 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019134get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 char_u **arg;
19136 char_u **alias;
19137 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019138 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139{
19140 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 char_u *p;
19142 char_u *expr_start;
19143 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144
19145 *alias = NULL; /* default to no alias */
19146
19147 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19148 && (*arg)[2] == (int)KE_SNR)
19149 {
19150 /* hard coded <SNR>, already translated */
19151 *arg += 3;
19152 return get_id_len(arg) + 3;
19153 }
19154 len = eval_fname_script(*arg);
19155 if (len > 0)
19156 {
19157 /* literal "<SID>", "s:" or "<SNR>" */
19158 *arg += len;
19159 }
19160
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019164 p = find_name_end(*arg, &expr_start, &expr_end,
19165 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 if (expr_start != NULL)
19167 {
19168 char_u *temp_string;
19169
19170 if (!evaluate)
19171 {
19172 len += (int)(p - *arg);
19173 *arg = skipwhite(p);
19174 return len;
19175 }
19176
19177 /*
19178 * Include any <SID> etc in the expanded string:
19179 * Thus the -len here.
19180 */
19181 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19182 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019183 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 *alias = temp_string;
19185 *arg = skipwhite(p);
19186 return (int)STRLEN(temp_string);
19187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188
19189 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019190 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 EMSG2(_(e_invexpr2), *arg);
19192
19193 return len;
19194}
19195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019196/*
19197 * Find the end of a variable or function name, taking care of magic braces.
19198 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19199 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019200 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 * Return a pointer to just after the name. Equal to "arg" if there is no
19202 * valid name.
19203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019205find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 char_u *arg;
19207 char_u **expr_start;
19208 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019209 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211 int mb_nest = 0;
19212 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 char_u *p;
19214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 *expr_start = NULL;
19218 *expr_end = NULL;
19219 }
19220
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019221 /* Quick check for valid starting character. */
19222 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19223 return arg;
19224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019225 for (p = arg; *p != NUL
19226 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019227 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019228 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019229 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019230 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019232 if (*p == '\'')
19233 {
19234 /* skip over 'string' to avoid counting [ and ] inside it. */
19235 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19236 ;
19237 if (*p == NUL)
19238 break;
19239 }
19240 else if (*p == '"')
19241 {
19242 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19243 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19244 if (*p == '\\' && p[1] != NUL)
19245 ++p;
19246 if (*p == NUL)
19247 break;
19248 }
19249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 if (*p == '[')
19253 ++br_nest;
19254 else if (*p == ']')
19255 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019258 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 if (*p == '{')
19261 {
19262 mb_nest++;
19263 if (expr_start != NULL && *expr_start == NULL)
19264 *expr_start = p;
19265 }
19266 else if (*p == '}')
19267 {
19268 mb_nest--;
19269 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19270 *expr_end = p;
19271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 }
19274
19275 return p;
19276}
19277
19278/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019279 * Expands out the 'magic' {}'s in a variable/function name.
19280 * Note that this can call itself recursively, to deal with
19281 * constructs like foo{bar}{baz}{bam}
19282 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19283 * "in_start" ^
19284 * "expr_start" ^
19285 * "expr_end" ^
19286 * "in_end" ^
19287 *
19288 * Returns a new allocated string, which the caller must free.
19289 * Returns NULL for failure.
19290 */
19291 static char_u *
19292make_expanded_name(in_start, expr_start, expr_end, in_end)
19293 char_u *in_start;
19294 char_u *expr_start;
19295 char_u *expr_end;
19296 char_u *in_end;
19297{
19298 char_u c1;
19299 char_u *retval = NULL;
19300 char_u *temp_result;
19301 char_u *nextcmd = NULL;
19302
19303 if (expr_end == NULL || in_end == NULL)
19304 return NULL;
19305 *expr_start = NUL;
19306 *expr_end = NUL;
19307 c1 = *in_end;
19308 *in_end = NUL;
19309
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019310 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019311 if (temp_result != NULL && nextcmd == NULL)
19312 {
19313 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19314 + (in_end - expr_end) + 1));
19315 if (retval != NULL)
19316 {
19317 STRCPY(retval, in_start);
19318 STRCAT(retval, temp_result);
19319 STRCAT(retval, expr_end + 1);
19320 }
19321 }
19322 vim_free(temp_result);
19323
19324 *in_end = c1; /* put char back for error messages */
19325 *expr_start = '{';
19326 *expr_end = '}';
19327
19328 if (retval != NULL)
19329 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019330 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019331 if (expr_start != NULL)
19332 {
19333 /* Further expansion! */
19334 temp_result = make_expanded_name(retval, expr_start,
19335 expr_end, temp_result);
19336 vim_free(retval);
19337 retval = temp_result;
19338 }
19339 }
19340
19341 return retval;
19342}
19343
19344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019346 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 */
19348 static int
19349eval_isnamec(c)
19350 int c;
19351{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019352 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19353}
19354
19355/*
19356 * Return TRUE if character "c" can be used as the first character in a
19357 * variable or function name (excluding '{' and '}').
19358 */
19359 static int
19360eval_isnamec1(c)
19361 int c;
19362{
19363 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364}
19365
19366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 * Set number v: variable to "val".
19368 */
19369 void
19370set_vim_var_nr(idx, val)
19371 int idx;
19372 long val;
19373{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019374 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375}
19376
19377/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019378 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 */
19380 long
19381get_vim_var_nr(idx)
19382 int idx;
19383{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019384 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385}
19386
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019387/*
19388 * Get string v: variable value. Uses a static buffer, can only be used once.
19389 */
19390 char_u *
19391get_vim_var_str(idx)
19392 int idx;
19393{
19394 return get_tv_string(&vimvars[idx].vv_tv);
19395}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019396
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019398 * Get List v: variable value. Caller must take care of reference count when
19399 * needed.
19400 */
19401 list_T *
19402get_vim_var_list(idx)
19403 int idx;
19404{
19405 return vimvars[idx].vv_list;
19406}
19407
19408/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019409 * Set v:char to character "c".
19410 */
19411 void
19412set_vim_var_char(c)
19413 int c;
19414{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019415 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019416
19417#ifdef FEAT_MBYTE
19418 if (has_mbyte)
19419 buf[(*mb_char2bytes)(c, buf)] = NUL;
19420 else
19421#endif
19422 {
19423 buf[0] = c;
19424 buf[1] = NUL;
19425 }
19426 set_vim_var_string(VV_CHAR, buf, -1);
19427}
19428
19429/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019430 * Set v:count to "count" and v:count1 to "count1".
19431 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 */
19433 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019434set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 long count;
19436 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019437 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019439 if (set_prevcount)
19440 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019441 vimvars[VV_COUNT].vv_nr = count;
19442 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443}
19444
19445/*
19446 * Set string v: variable to a copy of "val".
19447 */
19448 void
19449set_vim_var_string(idx, val, len)
19450 int idx;
19451 char_u *val;
19452 int len; /* length of "val" to use or -1 (whole string) */
19453{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019454 /* Need to do this (at least) once, since we can't initialize a union.
19455 * Will always be invoked when "v:progname" is set. */
19456 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19457
Bram Moolenaare9a41262005-01-15 22:18:47 +000019458 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019460 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019462 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019464 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465}
19466
19467/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019468 * Set List v: variable to "val".
19469 */
19470 void
19471set_vim_var_list(idx, val)
19472 int idx;
19473 list_T *val;
19474{
19475 list_unref(vimvars[idx].vv_list);
19476 vimvars[idx].vv_list = val;
19477 if (val != NULL)
19478 ++val->lv_refcount;
19479}
19480
19481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 * Set v:register if needed.
19483 */
19484 void
19485set_reg_var(c)
19486 int c;
19487{
19488 char_u regname;
19489
19490 if (c == 0 || c == ' ')
19491 regname = '"';
19492 else
19493 regname = c;
19494 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019495 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 set_vim_var_string(VV_REG, &regname, 1);
19497}
19498
19499/*
19500 * Get or set v:exception. If "oldval" == NULL, return the current value.
19501 * Otherwise, restore the value to "oldval" and return NULL.
19502 * Must always be called in pairs to save and restore v:exception! Does not
19503 * take care of memory allocations.
19504 */
19505 char_u *
19506v_exception(oldval)
19507 char_u *oldval;
19508{
19509 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019510 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511
Bram Moolenaare9a41262005-01-15 22:18:47 +000019512 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 return NULL;
19514}
19515
19516/*
19517 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19518 * Otherwise, restore the value to "oldval" and return NULL.
19519 * Must always be called in pairs to save and restore v:throwpoint! Does not
19520 * take care of memory allocations.
19521 */
19522 char_u *
19523v_throwpoint(oldval)
19524 char_u *oldval;
19525{
19526 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019527 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528
Bram Moolenaare9a41262005-01-15 22:18:47 +000019529 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 return NULL;
19531}
19532
19533#if defined(FEAT_AUTOCMD) || defined(PROTO)
19534/*
19535 * Set v:cmdarg.
19536 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19537 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19538 * Must always be called in pairs!
19539 */
19540 char_u *
19541set_cmdarg(eap, oldarg)
19542 exarg_T *eap;
19543 char_u *oldarg;
19544{
19545 char_u *oldval;
19546 char_u *newval;
19547 unsigned len;
19548
Bram Moolenaare9a41262005-01-15 22:18:47 +000019549 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019550 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019552 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019553 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019554 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 }
19556
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019557 if (eap->force_bin == FORCE_BIN)
19558 len = 6;
19559 else if (eap->force_bin == FORCE_NOBIN)
19560 len = 8;
19561 else
19562 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019563
19564 if (eap->read_edit)
19565 len += 7;
19566
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019567 if (eap->force_ff != 0)
19568 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19569# ifdef FEAT_MBYTE
19570 if (eap->force_enc != 0)
19571 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019572 if (eap->bad_char != 0)
19573 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019574# endif
19575
19576 newval = alloc(len + 1);
19577 if (newval == NULL)
19578 return NULL;
19579
19580 if (eap->force_bin == FORCE_BIN)
19581 sprintf((char *)newval, " ++bin");
19582 else if (eap->force_bin == FORCE_NOBIN)
19583 sprintf((char *)newval, " ++nobin");
19584 else
19585 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019586
19587 if (eap->read_edit)
19588 STRCAT(newval, " ++edit");
19589
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019590 if (eap->force_ff != 0)
19591 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19592 eap->cmd + eap->force_ff);
19593# ifdef FEAT_MBYTE
19594 if (eap->force_enc != 0)
19595 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19596 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019597 if (eap->bad_char == BAD_KEEP)
19598 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19599 else if (eap->bad_char == BAD_DROP)
19600 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19601 else if (eap->bad_char != 0)
19602 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019603# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019604 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019605 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606}
19607#endif
19608
19609/*
19610 * Get the value of internal variable "name".
19611 * Return OK or FAIL.
19612 */
19613 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019614get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 char_u *name;
19616 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019618 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619{
19620 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 typval_T *tv = NULL;
19622 typval_T atv;
19623 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625
19626 /* truncate the name, so that we can use strcmp() */
19627 cc = name[len];
19628 name[len] = NUL;
19629
19630 /*
19631 * Check for "b:changedtick".
19632 */
19633 if (STRCMP(name, "b:changedtick") == 0)
19634 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019635 atv.v_type = VAR_NUMBER;
19636 atv.vval.v_number = curbuf->b_changedtick;
19637 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 }
19639
19640 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 * Check for user-defined variables.
19642 */
19643 else
19644 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019645 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 }
19649
Bram Moolenaare9a41262005-01-15 22:18:47 +000019650 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019652 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019653 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 ret = FAIL;
19655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019657 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658
19659 name[len] = cc;
19660
19661 return ret;
19662}
19663
19664/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019665 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19666 * Also handle function call with Funcref variable: func(expr)
19667 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19668 */
19669 static int
19670handle_subscript(arg, rettv, evaluate, verbose)
19671 char_u **arg;
19672 typval_T *rettv;
19673 int evaluate; /* do more than finding the end */
19674 int verbose; /* give error messages */
19675{
19676 int ret = OK;
19677 dict_T *selfdict = NULL;
19678 char_u *s;
19679 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019680 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019681
19682 while (ret == OK
19683 && (**arg == '['
19684 || (**arg == '.' && rettv->v_type == VAR_DICT)
19685 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19686 && !vim_iswhite(*(*arg - 1)))
19687 {
19688 if (**arg == '(')
19689 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019690 /* need to copy the funcref so that we can clear rettv */
19691 functv = *rettv;
19692 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019693
19694 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019695 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019696 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019697 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19698 &len, evaluate, selfdict);
19699
19700 /* Clear the funcref afterwards, so that deleting it while
19701 * evaluating the arguments is possible (see test55). */
19702 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019703
19704 /* Stop the expression evaluation when immediately aborting on
19705 * error, or when an interrupt occurred or an exception was thrown
19706 * but not caught. */
19707 if (aborting())
19708 {
19709 if (ret == OK)
19710 clear_tv(rettv);
19711 ret = FAIL;
19712 }
19713 dict_unref(selfdict);
19714 selfdict = NULL;
19715 }
19716 else /* **arg == '[' || **arg == '.' */
19717 {
19718 dict_unref(selfdict);
19719 if (rettv->v_type == VAR_DICT)
19720 {
19721 selfdict = rettv->vval.v_dict;
19722 if (selfdict != NULL)
19723 ++selfdict->dv_refcount;
19724 }
19725 else
19726 selfdict = NULL;
19727 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19728 {
19729 clear_tv(rettv);
19730 ret = FAIL;
19731 }
19732 }
19733 }
19734 dict_unref(selfdict);
19735 return ret;
19736}
19737
19738/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019739 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019740 * value).
19741 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019742 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744{
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746}
19747
19748/*
19749 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 * The string "s" must have been allocated, it is consumed.
19751 * Return NULL for out of memory, the variable otherwise.
19752 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 char_u *s;
19756{
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 rettv = alloc_tv();
19760 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019762 rettv->v_type = VAR_STRING;
19763 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 }
19765 else
19766 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768}
19769
19770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019771 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019773 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019775 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776{
19777 if (varp != NULL)
19778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019779 switch (varp->v_type)
19780 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019781 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019782 func_unref(varp->vval.v_string);
19783 /*FALLTHROUGH*/
19784 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019785 vim_free(varp->vval.v_string);
19786 break;
19787 case VAR_LIST:
19788 list_unref(varp->vval.v_list);
19789 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019790 case VAR_DICT:
19791 dict_unref(varp->vval.v_dict);
19792 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019793 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019794#ifdef FEAT_FLOAT
19795 case VAR_FLOAT:
19796#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019797 case VAR_UNKNOWN:
19798 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019800 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019801 break;
19802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 vim_free(varp);
19804 }
19805}
19806
19807/*
19808 * Free the memory for a variable value and set the value to NULL or 0.
19809 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019810 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019811clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813{
19814 if (varp != NULL)
19815 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019816 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019818 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019819 func_unref(varp->vval.v_string);
19820 /*FALLTHROUGH*/
19821 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019822 vim_free(varp->vval.v_string);
19823 varp->vval.v_string = NULL;
19824 break;
19825 case VAR_LIST:
19826 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019827 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019828 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019829 case VAR_DICT:
19830 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019831 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019832 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019834 varp->vval.v_number = 0;
19835 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019836#ifdef FEAT_FLOAT
19837 case VAR_FLOAT:
19838 varp->vval.v_float = 0.0;
19839 break;
19840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 case VAR_UNKNOWN:
19842 break;
19843 default:
19844 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019846 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 }
19848}
19849
19850/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851 * Set the value of a variable to NULL without freeing items.
19852 */
19853 static void
19854init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019855 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856{
19857 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019859}
19860
19861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 * Get the number value of a variable.
19863 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019864 * For incompatible types, return 0.
19865 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19866 * caller of incompatible types: it sets *denote to TRUE if "denote"
19867 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 */
19869 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019870get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019871 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019873 int error = FALSE;
19874
19875 return get_tv_number_chk(varp, &error); /* return 0L on error */
19876}
19877
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019878 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019879get_tv_number_chk(varp, denote)
19880 typval_T *varp;
19881 int *denote;
19882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019883 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019885 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019888 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019889#ifdef FEAT_FLOAT
19890 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019891 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019892 break;
19893#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019894 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019895 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019896 break;
19897 case VAR_STRING:
19898 if (varp->vval.v_string != NULL)
19899 vim_str2nr(varp->vval.v_string, NULL, NULL,
19900 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019901 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019902 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019903 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019904 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019905 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019906 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019907 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019908 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019909 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019910 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019912 if (denote == NULL) /* useful for values that must be unsigned */
19913 n = -1;
19914 else
19915 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019916 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917}
19918
19919/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019920 * Get the lnum from the first argument.
19921 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019922 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 */
19924 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019926 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927{
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 linenr_T lnum;
19930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019931 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 if (lnum == 0) /* no valid number, try using line() */
19933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019934 rettv.v_type = VAR_NUMBER;
19935 f_line(argvars, &rettv);
19936 lnum = rettv.vval.v_number;
19937 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 }
19939 return lnum;
19940}
19941
19942/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019943 * Get the lnum from the first argument.
19944 * Also accepts "$", then "buf" is used.
19945 * Returns 0 on error.
19946 */
19947 static linenr_T
19948get_tv_lnum_buf(argvars, buf)
19949 typval_T *argvars;
19950 buf_T *buf;
19951{
19952 if (argvars[0].v_type == VAR_STRING
19953 && argvars[0].vval.v_string != NULL
19954 && argvars[0].vval.v_string[0] == '$'
19955 && buf != NULL)
19956 return buf->b_ml.ml_line_count;
19957 return get_tv_number_chk(&argvars[0], NULL);
19958}
19959
19960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 * Get the string value of a variable.
19962 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019963 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19964 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 * If the String variable has never been set, return an empty string.
19966 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019967 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19968 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 */
19970 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973{
19974 static char_u mybuf[NUMBUFLEN];
19975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977}
19978
19979 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019980get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019981 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019982 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019984 char_u *res = get_tv_string_buf_chk(varp, buf);
19985
19986 return res != NULL ? res : (char_u *)"";
19987}
19988
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019989 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019990get_tv_string_chk(varp)
19991 typval_T *varp;
19992{
19993 static char_u mybuf[NUMBUFLEN];
19994
19995 return get_tv_string_buf_chk(varp, mybuf);
19996}
19997
19998 static char_u *
19999get_tv_string_buf_chk(varp, buf)
20000 typval_T *varp;
20001 char_u *buf;
20002{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020005 case VAR_NUMBER:
20006 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20007 return buf;
20008 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020009 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020010 break;
20011 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020012 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020013 break;
20014 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020015 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020017#ifdef FEAT_FLOAT
20018 case VAR_FLOAT:
20019 EMSG(_("E806: using Float as a String"));
20020 break;
20021#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020022 case VAR_STRING:
20023 if (varp->vval.v_string != NULL)
20024 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020025 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020026 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020027 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020028 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020030 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031}
20032
20033/*
20034 * Find variable "name" in the list of variables.
20035 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020036 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020037 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020041find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020043 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020046 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047
Bram Moolenaara7043832005-01-21 11:56:39 +000020048 ht = find_var_ht(name, &varname);
20049 if (htp != NULL)
20050 *htp = ht;
20051 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020053 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054}
20055
20056/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000020058 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020061find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000020063 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020064 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020065{
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 hashitem_T *hi;
20067
20068 if (*varname == NUL)
20069 {
20070 /* Must be something like "s:", otherwise "ht" would be NULL. */
20071 switch (varname[-2])
20072 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020073 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 case 'g': return &globvars_var;
20075 case 'v': return &vimvars_var;
20076 case 'b': return &curbuf->b_bufvar;
20077 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020078#ifdef FEAT_WINDOWS
20079 case 't': return &curtab->tp_winvar;
20080#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020081 case 'l': return current_funccal == NULL
20082 ? NULL : &current_funccal->l_vars_var;
20083 case 'a': return current_funccal == NULL
20084 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020085 }
20086 return NULL;
20087 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020088
20089 hi = hash_find(ht, varname);
20090 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020091 {
20092 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020093 * worked find the variable again. Don't auto-load a script if it was
20094 * loaded already, otherwise it would be loaded every time when
20095 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020096 if (ht == &globvarht && !writing)
20097 {
20098 /* Note: script_autoload() may make "hi" invalid. It must either
20099 * be obtained again or not used. */
20100 if (!script_autoload(varname, FALSE) || aborting())
20101 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020102 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020104 if (HASHITEM_EMPTY(hi))
20105 return NULL;
20106 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020108}
20109
20110/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020112 * Set "varname" to the start of name without ':'.
20113 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020115find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 char_u *name;
20117 char_u **varname;
20118{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020119 hashitem_T *hi;
20120
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 if (name[1] != ':')
20122 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020123 /* The name must not start with a colon or #. */
20124 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 return NULL;
20126 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020127
20128 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020129 hi = hash_find(&compat_hashtab, name);
20130 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020131 return &compat_hashtab;
20132
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 return &globvarht; /* global variable */
20135 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 }
20137 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020138 if (*name == 'g') /* global variable */
20139 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020140 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20141 */
20142 if (vim_strchr(name + 2, ':') != NULL
20143 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020144 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020146 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020149#ifdef FEAT_WINDOWS
20150 if (*name == 't') /* tab page variable */
20151 return &curtab->tp_vars.dv_hashtab;
20152#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 if (*name == 'v') /* v: variable */
20154 return &vimvarht;
20155 if (*name == 'a' && current_funccal != NULL) /* function argument */
20156 return &current_funccal->l_avars.dv_hashtab;
20157 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20158 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 if (*name == 's' /* script variable */
20160 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20161 return &SCRIPT_VARS(current_SID);
20162 return NULL;
20163}
20164
20165/*
20166 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020167 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 * Returns NULL when it doesn't exist.
20169 */
20170 char_u *
20171get_var_value(name)
20172 char_u *name;
20173{
Bram Moolenaar33570922005-01-25 22:26:29 +000020174 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175
Bram Moolenaara7043832005-01-21 11:56:39 +000020176 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 if (v == NULL)
20178 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020179 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180}
20181
20182/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 * sourcing this script and when executing functions defined in the script.
20185 */
20186 void
20187new_script_vars(id)
20188 scid_T id;
20189{
Bram Moolenaara7043832005-01-21 11:56:39 +000020190 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020191 hashtab_T *ht;
20192 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020193
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20195 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020196 /* Re-allocating ga_data means that an ht_array pointing to
20197 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020199 for (i = 1; i <= ga_scripts.ga_len; ++i)
20200 {
20201 ht = &SCRIPT_VARS(i);
20202 if (ht->ht_mask == HT_INIT_SIZE - 1)
20203 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020204 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020205 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020206 }
20207
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 while (ga_scripts.ga_len < id)
20209 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020210 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020211 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020212 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 }
20215 }
20216}
20217
20218/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020219 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20220 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 */
20222 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020223init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020224 dict_T *dict;
20225 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020226 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227{
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020229 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020230 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020231 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020232 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020233 dict_var->di_tv.vval.v_dict = dict;
20234 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020235 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20237 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238}
20239
20240/*
20241 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 * Frees all allocated variables and the value they contain.
20243 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 */
20245 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020246vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 hashtab_T *ht;
20248{
20249 vars_clear_ext(ht, TRUE);
20250}
20251
20252/*
20253 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20254 */
20255 static void
20256vars_clear_ext(ht, free_val)
20257 hashtab_T *ht;
20258 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259{
Bram Moolenaara7043832005-01-21 11:56:39 +000020260 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 hashitem_T *hi;
20262 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263
Bram Moolenaar33570922005-01-25 22:26:29 +000020264 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020265 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020266 for (hi = ht->ht_array; todo > 0; ++hi)
20267 {
20268 if (!HASHITEM_EMPTY(hi))
20269 {
20270 --todo;
20271
Bram Moolenaar33570922005-01-25 22:26:29 +000020272 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020273 * ht_array might change then. hash_clear() takes care of it
20274 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020275 v = HI2DI(hi);
20276 if (free_val)
20277 clear_tv(&v->di_tv);
20278 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20279 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020280 }
20281 }
20282 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020283 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284}
20285
Bram Moolenaara7043832005-01-21 11:56:39 +000020286/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 * Delete a variable from hashtab "ht" at item "hi".
20288 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020291delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 hashtab_T *ht;
20293 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294{
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020296
20297 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020298 clear_tv(&di->di_tv);
20299 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300}
20301
20302/*
20303 * List the value of one internal variable.
20304 */
20305 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020306list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020307 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020309 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020311 char_u *tofree;
20312 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020313 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020314
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020315 current_copyID += COPYID_INC;
20316 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020317 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020318 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020319 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320}
20321
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020323list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 char_u *prefix;
20325 char_u *name;
20326 int type;
20327 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020328 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329{
Bram Moolenaar31859182007-08-14 20:41:13 +000020330 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20331 msg_start();
20332 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 if (name != NULL) /* "a:" vars don't have a name stored */
20334 msg_puts(name);
20335 msg_putchar(' ');
20336 msg_advance(22);
20337 if (type == VAR_NUMBER)
20338 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020339 else if (type == VAR_FUNC)
20340 msg_putchar('*');
20341 else if (type == VAR_LIST)
20342 {
20343 msg_putchar('[');
20344 if (*string == '[')
20345 ++string;
20346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020347 else if (type == VAR_DICT)
20348 {
20349 msg_putchar('{');
20350 if (*string == '{')
20351 ++string;
20352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 else
20354 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020355
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020357
20358 if (type == VAR_FUNC)
20359 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020360 if (*first)
20361 {
20362 msg_clr_eos();
20363 *first = FALSE;
20364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365}
20366
20367/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 * If the variable already exists, the value is updated.
20370 * Otherwise the variable is created.
20371 */
20372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020373set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020376 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377{
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020380 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020382 ht = find_var_ht(name, &varname);
20383 if (ht == NULL || *varname == NUL)
20384 {
20385 EMSG2(_(e_illvar), name);
20386 return;
20387 }
20388 v = find_var_in_ht(ht, varname, TRUE);
20389
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020390 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20391 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020392
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020396 if (var_check_ro(v->di_flags, name)
20397 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020398 return;
20399 if (v->di_tv.v_type != tv->v_type
20400 && !((v->di_tv.v_type == VAR_STRING
20401 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020402 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020403 || tv->v_type == VAR_NUMBER))
20404#ifdef FEAT_FLOAT
20405 && !((v->di_tv.v_type == VAR_NUMBER
20406 || v->di_tv.v_type == VAR_FLOAT)
20407 && (tv->v_type == VAR_NUMBER
20408 || tv->v_type == VAR_FLOAT))
20409#endif
20410 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020411 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020412 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020413 return;
20414 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020415
20416 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020417 * Handle setting internal v: variables separately: we don't change
20418 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020419 */
20420 if (ht == &vimvarht)
20421 {
20422 if (v->di_tv.v_type == VAR_STRING)
20423 {
20424 vim_free(v->di_tv.vval.v_string);
20425 if (copy || tv->v_type != VAR_STRING)
20426 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20427 else
20428 {
20429 /* Take over the string to avoid an extra alloc/free. */
20430 v->di_tv.vval.v_string = tv->vval.v_string;
20431 tv->vval.v_string = NULL;
20432 }
20433 }
20434 else if (v->di_tv.v_type != VAR_NUMBER)
20435 EMSG2(_(e_intern2), "set_var()");
20436 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020437 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020438 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020439 if (STRCMP(varname, "searchforward") == 0)
20440 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20441 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020442 return;
20443 }
20444
20445 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 }
20447 else /* add a new variable */
20448 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020449 /* Can't add "v:" variable. */
20450 if (ht == &vimvarht)
20451 {
20452 EMSG2(_(e_illvar), name);
20453 return;
20454 }
20455
Bram Moolenaar92124a32005-06-17 22:03:40 +000020456 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020457 if (!valid_varname(varname))
20458 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020459
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020460 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20461 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020462 if (v == NULL)
20463 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020464 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020465 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020467 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 return;
20469 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020470 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020472
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020473 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020474 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020475 else
20476 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020478 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020479 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481}
20482
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020483/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020484 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 * Also give an error message.
20486 */
20487 static int
20488var_check_ro(flags, name)
20489 int flags;
20490 char_u *name;
20491{
20492 if (flags & DI_FLAGS_RO)
20493 {
20494 EMSG2(_(e_readonlyvar), name);
20495 return TRUE;
20496 }
20497 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20498 {
20499 EMSG2(_(e_readonlysbx), name);
20500 return TRUE;
20501 }
20502 return FALSE;
20503}
20504
20505/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020506 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20507 * Also give an error message.
20508 */
20509 static int
20510var_check_fixed(flags, name)
20511 int flags;
20512 char_u *name;
20513{
20514 if (flags & DI_FLAGS_FIX)
20515 {
20516 EMSG2(_("E795: Cannot delete variable %s"), name);
20517 return TRUE;
20518 }
20519 return FALSE;
20520}
20521
20522/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020523 * Check if a funcref is assigned to a valid variable name.
20524 * Return TRUE and give an error if not.
20525 */
20526 static int
20527var_check_func_name(name, new_var)
20528 char_u *name; /* points to start of variable name */
20529 int new_var; /* TRUE when creating the variable */
20530{
20531 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20532 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20533 ? name[2] : name[0]))
20534 {
20535 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20536 name);
20537 return TRUE;
20538 }
20539 /* Don't allow hiding a function. When "v" is not NULL we might be
20540 * assigning another function to the same var, the type is checked
20541 * below. */
20542 if (new_var && function_exists(name))
20543 {
20544 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20545 name);
20546 return TRUE;
20547 }
20548 return FALSE;
20549}
20550
20551/*
20552 * Check if a variable name is valid.
20553 * Return FALSE and give an error if not.
20554 */
20555 static int
20556valid_varname(varname)
20557 char_u *varname;
20558{
20559 char_u *p;
20560
20561 for (p = varname; *p != NUL; ++p)
20562 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20563 && *p != AUTOLOAD_CHAR)
20564 {
20565 EMSG2(_(e_illvar), varname);
20566 return FALSE;
20567 }
20568 return TRUE;
20569}
20570
20571/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020572 * Return TRUE if typeval "tv" is set to be locked (immutable).
20573 * Also give an error message, using "name".
20574 */
20575 static int
20576tv_check_lock(lock, name)
20577 int lock;
20578 char_u *name;
20579{
20580 if (lock & VAR_LOCKED)
20581 {
20582 EMSG2(_("E741: Value is locked: %s"),
20583 name == NULL ? (char_u *)_("Unknown") : name);
20584 return TRUE;
20585 }
20586 if (lock & VAR_FIXED)
20587 {
20588 EMSG2(_("E742: Cannot change value of %s"),
20589 name == NULL ? (char_u *)_("Unknown") : name);
20590 return TRUE;
20591 }
20592 return FALSE;
20593}
20594
20595/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020596 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020598 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020599 * It is OK for "from" and "to" to point to the same item. This is used to
20600 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020601 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020602 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020603copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020604 typval_T *from;
20605 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020608 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 switch (from->v_type)
20610 {
20611 case VAR_NUMBER:
20612 to->vval.v_number = from->vval.v_number;
20613 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020614#ifdef FEAT_FLOAT
20615 case VAR_FLOAT:
20616 to->vval.v_float = from->vval.v_float;
20617 break;
20618#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020619 case VAR_STRING:
20620 case VAR_FUNC:
20621 if (from->vval.v_string == NULL)
20622 to->vval.v_string = NULL;
20623 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020625 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020626 if (from->v_type == VAR_FUNC)
20627 func_ref(to->vval.v_string);
20628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020629 break;
20630 case VAR_LIST:
20631 if (from->vval.v_list == NULL)
20632 to->vval.v_list = NULL;
20633 else
20634 {
20635 to->vval.v_list = from->vval.v_list;
20636 ++to->vval.v_list->lv_refcount;
20637 }
20638 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020639 case VAR_DICT:
20640 if (from->vval.v_dict == NULL)
20641 to->vval.v_dict = NULL;
20642 else
20643 {
20644 to->vval.v_dict = from->vval.v_dict;
20645 ++to->vval.v_dict->dv_refcount;
20646 }
20647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020648 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020649 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020650 break;
20651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652}
20653
20654/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020655 * Make a copy of an item.
20656 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020657 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20658 * reference to an already copied list/dict can be used.
20659 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020660 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020661 static int
20662item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 typval_T *from;
20664 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020665 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020666 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020667{
20668 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020669 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020670
Bram Moolenaar33570922005-01-25 22:26:29 +000020671 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 {
20673 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020675 }
20676 ++recurse;
20677
20678 switch (from->v_type)
20679 {
20680 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020681#ifdef FEAT_FLOAT
20682 case VAR_FLOAT:
20683#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684 case VAR_STRING:
20685 case VAR_FUNC:
20686 copy_tv(from, to);
20687 break;
20688 case VAR_LIST:
20689 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020690 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020691 if (from->vval.v_list == NULL)
20692 to->vval.v_list = NULL;
20693 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20694 {
20695 /* use the copy made earlier */
20696 to->vval.v_list = from->vval.v_list->lv_copylist;
20697 ++to->vval.v_list->lv_refcount;
20698 }
20699 else
20700 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20701 if (to->vval.v_list == NULL)
20702 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020703 break;
20704 case VAR_DICT:
20705 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020706 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020707 if (from->vval.v_dict == NULL)
20708 to->vval.v_dict = NULL;
20709 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20710 {
20711 /* use the copy made earlier */
20712 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20713 ++to->vval.v_dict->dv_refcount;
20714 }
20715 else
20716 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20717 if (to->vval.v_dict == NULL)
20718 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020719 break;
20720 default:
20721 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020722 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020723 }
20724 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020725 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020726}
20727
20728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 * ":echo expr1 ..." print each argument separated with a space, add a
20730 * newline at the end.
20731 * ":echon expr1 ..." print each argument plain.
20732 */
20733 void
20734ex_echo(eap)
20735 exarg_T *eap;
20736{
20737 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020738 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020739 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 char_u *p;
20741 int needclr = TRUE;
20742 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020743 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744
20745 if (eap->skip)
20746 ++emsg_skip;
20747 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020749 /* If eval1() causes an error message the text from the command may
20750 * still need to be cleared. E.g., "echo 22,44". */
20751 need_clr_eos = needclr;
20752
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020754 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 {
20756 /*
20757 * Report the invalid expression unless the expression evaluation
20758 * has been cancelled due to an aborting error, an interrupt, or an
20759 * exception.
20760 */
20761 if (!aborting())
20762 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020763 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 break;
20765 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020766 need_clr_eos = FALSE;
20767
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 if (!eap->skip)
20769 {
20770 if (atstart)
20771 {
20772 atstart = FALSE;
20773 /* Call msg_start() after eval1(), evaluating the expression
20774 * may cause a message to appear. */
20775 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020776 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020777 /* Mark the saved text as finishing the line, so that what
20778 * follows is displayed on a new line when scrolling back
20779 * at the more prompt. */
20780 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 }
20784 else if (eap->cmdidx == CMD_echo)
20785 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020786 current_copyID += COPYID_INC;
20787 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020788 if (p != NULL)
20789 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020791 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020793 if (*p != TAB && needclr)
20794 {
20795 /* remove any text still there from the command */
20796 msg_clr_eos();
20797 needclr = FALSE;
20798 }
20799 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 }
20801 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020802 {
20803#ifdef FEAT_MBYTE
20804 if (has_mbyte)
20805 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020806 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020807
20808 (void)msg_outtrans_len_attr(p, i, echo_attr);
20809 p += i - 1;
20810 }
20811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020813 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020816 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020818 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 arg = skipwhite(arg);
20820 }
20821 eap->nextcmd = check_nextcmd(arg);
20822
20823 if (eap->skip)
20824 --emsg_skip;
20825 else
20826 {
20827 /* remove text that may still be there from the command */
20828 if (needclr)
20829 msg_clr_eos();
20830 if (eap->cmdidx == CMD_echo)
20831 msg_end();
20832 }
20833}
20834
20835/*
20836 * ":echohl {name}".
20837 */
20838 void
20839ex_echohl(eap)
20840 exarg_T *eap;
20841{
20842 int id;
20843
20844 id = syn_name2id(eap->arg);
20845 if (id == 0)
20846 echo_attr = 0;
20847 else
20848 echo_attr = syn_id2attr(id);
20849}
20850
20851/*
20852 * ":execute expr1 ..." execute the result of an expression.
20853 * ":echomsg expr1 ..." Print a message
20854 * ":echoerr expr1 ..." Print an error
20855 * Each gets spaces around each argument and a newline at the end for
20856 * echo commands
20857 */
20858 void
20859ex_execute(eap)
20860 exarg_T *eap;
20861{
20862 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020863 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 int ret = OK;
20865 char_u *p;
20866 garray_T ga;
20867 int len;
20868 int save_did_emsg;
20869
20870 ga_init2(&ga, 1, 80);
20871
20872 if (eap->skip)
20873 ++emsg_skip;
20874 while (*arg != NUL && *arg != '|' && *arg != '\n')
20875 {
20876 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020877 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 {
20879 /*
20880 * Report the invalid expression unless the expression evaluation
20881 * has been cancelled due to an aborting error, an interrupt, or an
20882 * exception.
20883 */
20884 if (!aborting())
20885 EMSG2(_(e_invexpr2), p);
20886 ret = FAIL;
20887 break;
20888 }
20889
20890 if (!eap->skip)
20891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020892 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 len = (int)STRLEN(p);
20894 if (ga_grow(&ga, len + 2) == FAIL)
20895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 ret = FAIL;
20898 break;
20899 }
20900 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 ga.ga_len += len;
20904 }
20905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 arg = skipwhite(arg);
20908 }
20909
20910 if (ret != FAIL && ga.ga_data != NULL)
20911 {
20912 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020915 out_flush();
20916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 else if (eap->cmdidx == CMD_echoerr)
20918 {
20919 /* We don't want to abort following commands, restore did_emsg. */
20920 save_did_emsg = did_emsg;
20921 EMSG((char_u *)ga.ga_data);
20922 if (!force_abort)
20923 did_emsg = save_did_emsg;
20924 }
20925 else if (eap->cmdidx == CMD_execute)
20926 do_cmdline((char_u *)ga.ga_data,
20927 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20928 }
20929
20930 ga_clear(&ga);
20931
20932 if (eap->skip)
20933 --emsg_skip;
20934
20935 eap->nextcmd = check_nextcmd(arg);
20936}
20937
20938/*
20939 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20940 * "arg" points to the "&" or '+' when called, to "option" when returning.
20941 * Returns NULL when no option name found. Otherwise pointer to the char
20942 * after the option name.
20943 */
20944 static char_u *
20945find_option_end(arg, opt_flags)
20946 char_u **arg;
20947 int *opt_flags;
20948{
20949 char_u *p = *arg;
20950
20951 ++p;
20952 if (*p == 'g' && p[1] == ':')
20953 {
20954 *opt_flags = OPT_GLOBAL;
20955 p += 2;
20956 }
20957 else if (*p == 'l' && p[1] == ':')
20958 {
20959 *opt_flags = OPT_LOCAL;
20960 p += 2;
20961 }
20962 else
20963 *opt_flags = 0;
20964
20965 if (!ASCII_ISALPHA(*p))
20966 return NULL;
20967 *arg = p;
20968
20969 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20970 p += 4; /* termcap option */
20971 else
20972 while (ASCII_ISALPHA(*p))
20973 ++p;
20974 return p;
20975}
20976
20977/*
20978 * ":function"
20979 */
20980 void
20981ex_function(eap)
20982 exarg_T *eap;
20983{
20984 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020985 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 int j;
20987 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 char_u *name = NULL;
20990 char_u *p;
20991 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020992 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 garray_T newargs;
20994 garray_T newlines;
20995 int varargs = FALSE;
20996 int mustend = FALSE;
20997 int flags = 0;
20998 ufunc_T *fp;
20999 int indent;
21000 int nesting;
21001 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 dictitem_T *v;
21003 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021004 static int func_nr = 0; /* number for nameless function */
21005 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021006 hashtab_T *ht;
21007 int todo;
21008 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021009 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010
21011 /*
21012 * ":function" without argument: list functions.
21013 */
21014 if (ends_excmd(*eap->arg))
21015 {
21016 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021018 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021019 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021020 {
21021 if (!HASHITEM_EMPTY(hi))
21022 {
21023 --todo;
21024 fp = HI2UF(hi);
21025 if (!isdigit(*fp->uf_name))
21026 list_func_head(fp, FALSE);
21027 }
21028 }
21029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 eap->nextcmd = check_nextcmd(eap->arg);
21031 return;
21032 }
21033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021034 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021035 * ":function /pat": list functions matching pattern.
21036 */
21037 if (*eap->arg == '/')
21038 {
21039 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21040 if (!eap->skip)
21041 {
21042 regmatch_T regmatch;
21043
21044 c = *p;
21045 *p = NUL;
21046 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21047 *p = c;
21048 if (regmatch.regprog != NULL)
21049 {
21050 regmatch.rm_ic = p_ic;
21051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021052 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021053 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21054 {
21055 if (!HASHITEM_EMPTY(hi))
21056 {
21057 --todo;
21058 fp = HI2UF(hi);
21059 if (!isdigit(*fp->uf_name)
21060 && vim_regexec(&regmatch, fp->uf_name, 0))
21061 list_func_head(fp, FALSE);
21062 }
21063 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021064 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021065 }
21066 }
21067 if (*p == '/')
21068 ++p;
21069 eap->nextcmd = check_nextcmd(p);
21070 return;
21071 }
21072
21073 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021074 * Get the function name. There are these situations:
21075 * func normal function name
21076 * "name" == func, "fudi.fd_dict" == NULL
21077 * dict.func new dictionary entry
21078 * "name" == NULL, "fudi.fd_dict" set,
21079 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21080 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021081 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021082 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21083 * dict.func existing dict entry that's not a Funcref
21084 * "name" == NULL, "fudi.fd_dict" set,
21085 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021088 name = trans_function_name(&p, eap->skip, 0, &fudi);
21089 paren = (vim_strchr(p, '(') != NULL);
21090 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 {
21092 /*
21093 * Return on an invalid expression in braces, unless the expression
21094 * evaluation has been cancelled due to an aborting error, an
21095 * interrupt, or an exception.
21096 */
21097 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021099 if (!eap->skip && fudi.fd_newkey != NULL)
21100 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021101 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 else
21105 eap->skip = TRUE;
21106 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021107
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 /* An error in a function call during evaluation of an expression in magic
21109 * braces should not cause the function not to be defined. */
21110 saved_did_emsg = did_emsg;
21111 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112
21113 /*
21114 * ":function func" with only function name: list function.
21115 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021116 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 {
21118 if (!ends_excmd(*skipwhite(p)))
21119 {
21120 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021121 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 }
21123 eap->nextcmd = check_nextcmd(p);
21124 if (eap->nextcmd != NULL)
21125 *p = NUL;
21126 if (!eap->skip && !got_int)
21127 {
21128 fp = find_func(name);
21129 if (fp != NULL)
21130 {
21131 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021132 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021134 if (FUNCLINE(fp, j) == NULL)
21135 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 msg_putchar('\n');
21137 msg_outnum((long)(j + 1));
21138 if (j < 9)
21139 msg_putchar(' ');
21140 if (j < 99)
21141 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021142 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 out_flush(); /* show a line at a time */
21144 ui_breakcheck();
21145 }
21146 if (!got_int)
21147 {
21148 msg_putchar('\n');
21149 msg_puts((char_u *)" endfunction");
21150 }
21151 }
21152 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021153 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021155 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 }
21157
21158 /*
21159 * ":function name(arg1, arg2)" Define function.
21160 */
21161 p = skipwhite(p);
21162 if (*p != '(')
21163 {
21164 if (!eap->skip)
21165 {
21166 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021167 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 }
21169 /* attempt to continue by skipping some text */
21170 if (vim_strchr(p, '(') != NULL)
21171 p = vim_strchr(p, '(');
21172 }
21173 p = skipwhite(p + 1);
21174
21175 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21176 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21177
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021178 if (!eap->skip)
21179 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021180 /* Check the name of the function. Unless it's a dictionary function
21181 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021182 if (name != NULL)
21183 arg = name;
21184 else
21185 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021186 if (arg != NULL && (fudi.fd_di == NULL
21187 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021188 {
21189 if (*arg == K_SPECIAL)
21190 j = 3;
21191 else
21192 j = 0;
21193 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21194 : eval_isnamec(arg[j])))
21195 ++j;
21196 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021197 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021198 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021199 /* Disallow using the g: dict. */
21200 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21201 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021202 }
21203
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 /*
21205 * Isolate the arguments: "arg1, arg2, ...)"
21206 */
21207 while (*p != ')')
21208 {
21209 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21210 {
21211 varargs = TRUE;
21212 p += 3;
21213 mustend = TRUE;
21214 }
21215 else
21216 {
21217 arg = p;
21218 while (ASCII_ISALNUM(*p) || *p == '_')
21219 ++p;
21220 if (arg == p || isdigit(*arg)
21221 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21222 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21223 {
21224 if (!eap->skip)
21225 EMSG2(_("E125: Illegal argument: %s"), arg);
21226 break;
21227 }
21228 if (ga_grow(&newargs, 1) == FAIL)
21229 goto erret;
21230 c = *p;
21231 *p = NUL;
21232 arg = vim_strsave(arg);
21233 if (arg == NULL)
21234 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021235
21236 /* Check for duplicate argument name. */
21237 for (i = 0; i < newargs.ga_len; ++i)
21238 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21239 {
21240 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21241 goto erret;
21242 }
21243
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21245 *p = c;
21246 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 if (*p == ',')
21248 ++p;
21249 else
21250 mustend = TRUE;
21251 }
21252 p = skipwhite(p);
21253 if (mustend && *p != ')')
21254 {
21255 if (!eap->skip)
21256 EMSG2(_(e_invarg2), eap->arg);
21257 break;
21258 }
21259 }
21260 ++p; /* skip the ')' */
21261
Bram Moolenaare9a41262005-01-15 22:18:47 +000021262 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 for (;;)
21264 {
21265 p = skipwhite(p);
21266 if (STRNCMP(p, "range", 5) == 0)
21267 {
21268 flags |= FC_RANGE;
21269 p += 5;
21270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021271 else if (STRNCMP(p, "dict", 4) == 0)
21272 {
21273 flags |= FC_DICT;
21274 p += 4;
21275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 else if (STRNCMP(p, "abort", 5) == 0)
21277 {
21278 flags |= FC_ABORT;
21279 p += 5;
21280 }
21281 else
21282 break;
21283 }
21284
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021285 /* When there is a line break use what follows for the function body.
21286 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21287 if (*p == '\n')
21288 line_arg = p + 1;
21289 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 EMSG(_(e_trailing));
21291
21292 /*
21293 * Read the body of the function, until ":endfunction" is found.
21294 */
21295 if (KeyTyped)
21296 {
21297 /* Check if the function already exists, don't let the user type the
21298 * whole function before telling him it doesn't work! For a script we
21299 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021300 if (!eap->skip && !eap->forceit)
21301 {
21302 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21303 EMSG(_(e_funcdict));
21304 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021305 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021308 if (!eap->skip && did_emsg)
21309 goto erret;
21310
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 msg_putchar('\n'); /* don't overwrite the function name */
21312 cmdline_row = msg_row;
21313 }
21314
21315 indent = 2;
21316 nesting = 0;
21317 for (;;)
21318 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021319 if (KeyTyped)
21320 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021322 sourcing_lnum_off = sourcing_lnum;
21323
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021324 if (line_arg != NULL)
21325 {
21326 /* Use eap->arg, split up in parts by line breaks. */
21327 theline = line_arg;
21328 p = vim_strchr(theline, '\n');
21329 if (p == NULL)
21330 line_arg += STRLEN(line_arg);
21331 else
21332 {
21333 *p = NUL;
21334 line_arg = p + 1;
21335 }
21336 }
21337 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 theline = getcmdline(':', 0L, indent);
21339 else
21340 theline = eap->getline(':', eap->cookie, indent);
21341 if (KeyTyped)
21342 lines_left = Rows - 1;
21343 if (theline == NULL)
21344 {
21345 EMSG(_("E126: Missing :endfunction"));
21346 goto erret;
21347 }
21348
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021349 /* Detect line continuation: sourcing_lnum increased more than one. */
21350 if (sourcing_lnum > sourcing_lnum_off + 1)
21351 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21352 else
21353 sourcing_lnum_off = 0;
21354
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 if (skip_until != NULL)
21356 {
21357 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21358 * don't check for ":endfunc". */
21359 if (STRCMP(theline, skip_until) == 0)
21360 {
21361 vim_free(skip_until);
21362 skip_until = NULL;
21363 }
21364 }
21365 else
21366 {
21367 /* skip ':' and blanks*/
21368 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21369 ;
21370
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021371 /* Check for "endfunction". */
21372 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021374 if (line_arg == NULL)
21375 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 break;
21377 }
21378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021379 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 * at "end". */
21381 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21382 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021383 else if (STRNCMP(p, "if", 2) == 0
21384 || STRNCMP(p, "wh", 2) == 0
21385 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 || STRNCMP(p, "try", 3) == 0)
21387 indent += 2;
21388
21389 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021390 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021392 if (*p == '!')
21393 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 p += eval_fname_script(p);
21395 if (ASCII_ISALPHA(*p))
21396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021397 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 if (*skipwhite(p) == '(')
21399 {
21400 ++nesting;
21401 indent += 2;
21402 }
21403 }
21404 }
21405
21406 /* Check for ":append" or ":insert". */
21407 p = skip_range(p, NULL);
21408 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21409 || (p[0] == 'i'
21410 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21411 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21412 skip_until = vim_strsave((char_u *)".");
21413
21414 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21415 arg = skipwhite(skiptowhite(p));
21416 if (arg[0] == '<' && arg[1] =='<'
21417 && ((p[0] == 'p' && p[1] == 'y'
21418 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21419 || (p[0] == 'p' && p[1] == 'e'
21420 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21421 || (p[0] == 't' && p[1] == 'c'
21422 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021423 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21424 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21426 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021427 || (p[0] == 'm' && p[1] == 'z'
21428 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 ))
21430 {
21431 /* ":python <<" continues until a dot, like ":append" */
21432 p = skipwhite(arg + 2);
21433 if (*p == NUL)
21434 skip_until = vim_strsave((char_u *)".");
21435 else
21436 skip_until = vim_strsave(p);
21437 }
21438 }
21439
21440 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021441 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021442 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021443 if (line_arg == NULL)
21444 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021446 }
21447
21448 /* Copy the line to newly allocated memory. get_one_sourceline()
21449 * allocates 250 bytes per line, this saves 80% on average. The cost
21450 * is an extra alloc/free. */
21451 p = vim_strsave(theline);
21452 if (p != NULL)
21453 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021454 if (line_arg == NULL)
21455 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021456 theline = p;
21457 }
21458
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021459 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21460
21461 /* Add NULL lines for continuation lines, so that the line count is
21462 * equal to the index in the growarray. */
21463 while (sourcing_lnum_off-- > 0)
21464 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021465
21466 /* Check for end of eap->arg. */
21467 if (line_arg != NULL && *line_arg == NUL)
21468 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 }
21470
21471 /* Don't define the function when skipping commands or when an error was
21472 * detected. */
21473 if (eap->skip || did_emsg)
21474 goto erret;
21475
21476 /*
21477 * If there are no errors, add the function
21478 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021479 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021480 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021481 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021482 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021483 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021484 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021485 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021486 goto erret;
21487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021489 fp = find_func(name);
21490 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021492 if (!eap->forceit)
21493 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021494 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 goto erret;
21496 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021497 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021499 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 name);
21501 goto erret;
21502 }
21503 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021504 ga_clear_strings(&(fp->uf_args));
21505 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 vim_free(name);
21507 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 }
21510 else
21511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 char numbuf[20];
21513
21514 fp = NULL;
21515 if (fudi.fd_newkey == NULL && !eap->forceit)
21516 {
21517 EMSG(_(e_funcdict));
21518 goto erret;
21519 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021520 if (fudi.fd_di == NULL)
21521 {
21522 /* Can't add a function to a locked dictionary */
21523 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21524 goto erret;
21525 }
21526 /* Can't change an existing function if it is locked */
21527 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21528 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021529
21530 /* Give the function a sequential number. Can only be used with a
21531 * Funcref! */
21532 vim_free(name);
21533 sprintf(numbuf, "%d", ++func_nr);
21534 name = vim_strsave((char_u *)numbuf);
21535 if (name == NULL)
21536 goto erret;
21537 }
21538
21539 if (fp == NULL)
21540 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021541 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021542 {
21543 int slen, plen;
21544 char_u *scriptname;
21545
21546 /* Check that the autoload name matches the script name. */
21547 j = FAIL;
21548 if (sourcing_name != NULL)
21549 {
21550 scriptname = autoload_name(name);
21551 if (scriptname != NULL)
21552 {
21553 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021554 plen = (int)STRLEN(p);
21555 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021556 if (slen > plen && fnamecmp(p,
21557 sourcing_name + slen - plen) == 0)
21558 j = OK;
21559 vim_free(scriptname);
21560 }
21561 }
21562 if (j == FAIL)
21563 {
21564 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21565 goto erret;
21566 }
21567 }
21568
21569 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 if (fp == NULL)
21571 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021572
21573 if (fudi.fd_dict != NULL)
21574 {
21575 if (fudi.fd_di == NULL)
21576 {
21577 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021578 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021579 if (fudi.fd_di == NULL)
21580 {
21581 vim_free(fp);
21582 goto erret;
21583 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021584 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21585 {
21586 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021587 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021588 goto erret;
21589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021590 }
21591 else
21592 /* overwrite existing dict entry */
21593 clear_tv(&fudi.fd_di->di_tv);
21594 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021595 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021597 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021598
21599 /* behave like "dict" was used */
21600 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021601 }
21602
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021604 STRCPY(fp->uf_name, name);
21605 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 fp->uf_args = newargs;
21608 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021609#ifdef FEAT_PROFILE
21610 fp->uf_tml_count = NULL;
21611 fp->uf_tml_total = NULL;
21612 fp->uf_tml_self = NULL;
21613 fp->uf_profiling = FALSE;
21614 if (prof_def_func())
21615 func_do_profile(fp);
21616#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021617 fp->uf_varargs = varargs;
21618 fp->uf_flags = flags;
21619 fp->uf_calls = 0;
21620 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021621 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622
21623erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 ga_clear_strings(&newargs);
21625 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626ret_free:
21627 vim_free(skip_until);
21628 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631}
21632
21633/*
21634 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021635 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021637 * flags:
21638 * TFN_INT: internal function name OK
21639 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 * Advances "pp" to just after the function name (if no error).
21641 */
21642 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021643trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 char_u **pp;
21645 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021646 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021647 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021649 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 char_u *start;
21651 char_u *end;
21652 int lead;
21653 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021655 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021656
21657 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021658 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021660
21661 /* Check for hard coded <SNR>: already translated function ID (from a user
21662 * command). */
21663 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21664 && (*pp)[2] == (int)KE_SNR)
21665 {
21666 *pp += 3;
21667 len = get_id_len(pp) + 3;
21668 return vim_strnsave(start, len);
21669 }
21670
21671 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21672 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021674 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021676
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021677 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21678 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021679 if (end == start)
21680 {
21681 if (!skip)
21682 EMSG(_("E129: Function name required"));
21683 goto theend;
21684 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021685 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021686 {
21687 /*
21688 * Report an invalid expression in braces, unless the expression
21689 * evaluation has been cancelled due to an aborting error, an
21690 * interrupt, or an exception.
21691 */
21692 if (!aborting())
21693 {
21694 if (end != NULL)
21695 EMSG2(_(e_invarg2), start);
21696 }
21697 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021698 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021699 goto theend;
21700 }
21701
21702 if (lv.ll_tv != NULL)
21703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021704 if (fdp != NULL)
21705 {
21706 fdp->fd_dict = lv.ll_dict;
21707 fdp->fd_newkey = lv.ll_newkey;
21708 lv.ll_newkey = NULL;
21709 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021711 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21712 {
21713 name = vim_strsave(lv.ll_tv->vval.v_string);
21714 *pp = end;
21715 }
21716 else
21717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021718 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21719 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720 EMSG(_(e_funcref));
21721 else
21722 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021723 name = NULL;
21724 }
21725 goto theend;
21726 }
21727
21728 if (lv.ll_name == NULL)
21729 {
21730 /* Error found, but continue after the function name. */
21731 *pp = end;
21732 goto theend;
21733 }
21734
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021735 /* Check if the name is a Funcref. If so, use the value. */
21736 if (lv.ll_exp_name != NULL)
21737 {
21738 len = (int)STRLEN(lv.ll_exp_name);
21739 name = deref_func_name(lv.ll_exp_name, &len);
21740 if (name == lv.ll_exp_name)
21741 name = NULL;
21742 }
21743 else
21744 {
21745 len = (int)(end - *pp);
21746 name = deref_func_name(*pp, &len);
21747 if (name == *pp)
21748 name = NULL;
21749 }
21750 if (name != NULL)
21751 {
21752 name = vim_strsave(name);
21753 *pp = end;
21754 goto theend;
21755 }
21756
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021757 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021758 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021759 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021760 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21761 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21762 {
21763 /* When there was "s:" already or the name expanded to get a
21764 * leading "s:" then remove it. */
21765 lv.ll_name += 2;
21766 len -= 2;
21767 lead = 2;
21768 }
21769 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021770 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021771 {
21772 if (lead == 2) /* skip over "s:" */
21773 lv.ll_name += 2;
21774 len = (int)(end - lv.ll_name);
21775 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021776
21777 /*
21778 * Copy the function name to allocated memory.
21779 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21780 * Accept <SNR>123_name() outside a script.
21781 */
21782 if (skip)
21783 lead = 0; /* do nothing */
21784 else if (lead > 0)
21785 {
21786 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021787 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21788 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021789 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021790 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021791 if (current_SID <= 0)
21792 {
21793 EMSG(_(e_usingsid));
21794 goto theend;
21795 }
21796 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21797 lead += (int)STRLEN(sid_buf);
21798 }
21799 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021800 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021801 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021802 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021803 goto theend;
21804 }
21805 name = alloc((unsigned)(len + lead + 1));
21806 if (name != NULL)
21807 {
21808 if (lead > 0)
21809 {
21810 name[0] = K_SPECIAL;
21811 name[1] = KS_EXTRA;
21812 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021813 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021814 STRCPY(name + 3, sid_buf);
21815 }
21816 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21817 name[len + lead] = NUL;
21818 }
21819 *pp = end;
21820
21821theend:
21822 clear_lval(&lv);
21823 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824}
21825
21826/*
21827 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21828 * Return 2 if "p" starts with "s:".
21829 * Return 0 otherwise.
21830 */
21831 static int
21832eval_fname_script(p)
21833 char_u *p;
21834{
21835 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21836 || STRNICMP(p + 1, "SNR>", 4) == 0))
21837 return 5;
21838 if (p[0] == 's' && p[1] == ':')
21839 return 2;
21840 return 0;
21841}
21842
21843/*
21844 * Return TRUE if "p" starts with "<SID>" or "s:".
21845 * Only works if eval_fname_script() returned non-zero for "p"!
21846 */
21847 static int
21848eval_fname_sid(p)
21849 char_u *p;
21850{
21851 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21852}
21853
21854/*
21855 * List the head of the function: "name(arg1, arg2)".
21856 */
21857 static void
21858list_func_head(fp, indent)
21859 ufunc_T *fp;
21860 int indent;
21861{
21862 int j;
21863
21864 msg_start();
21865 if (indent)
21866 MSG_PUTS(" ");
21867 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021868 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 {
21870 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021871 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 }
21873 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021874 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021876 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 {
21878 if (j)
21879 MSG_PUTS(", ");
21880 msg_puts(FUNCARG(fp, j));
21881 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021882 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 {
21884 if (j)
21885 MSG_PUTS(", ");
21886 MSG_PUTS("...");
21887 }
21888 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021889 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021890 if (p_verbose > 0)
21891 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892}
21893
21894/*
21895 * Find a function by name, return pointer to it in ufuncs.
21896 * Return NULL for unknown function.
21897 */
21898 static ufunc_T *
21899find_func(name)
21900 char_u *name;
21901{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021902 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021904 hi = hash_find(&func_hashtab, name);
21905 if (!HASHITEM_EMPTY(hi))
21906 return HI2UF(hi);
21907 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908}
21909
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021910#if defined(EXITFREE) || defined(PROTO)
21911 void
21912free_all_functions()
21913{
21914 hashitem_T *hi;
21915
21916 /* Need to start all over every time, because func_free() may change the
21917 * hash table. */
21918 while (func_hashtab.ht_used > 0)
21919 for (hi = func_hashtab.ht_array; ; ++hi)
21920 if (!HASHITEM_EMPTY(hi))
21921 {
21922 func_free(HI2UF(hi));
21923 break;
21924 }
21925}
21926#endif
21927
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928/*
21929 * Return TRUE if a function "name" exists.
21930 */
21931 static int
21932function_exists(name)
21933 char_u *name;
21934{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021935 char_u *nm = name;
21936 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021937 int n = FALSE;
21938
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021939 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021940 nm = skipwhite(nm);
21941
21942 /* Only accept "funcname", "funcname ", "funcname (..." and
21943 * "funcname(...", not "funcname!...". */
21944 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021945 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021946 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021947 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021948 else
21949 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021951 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021952 return n;
21953}
21954
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021955/*
21956 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021957 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021958 */
21959 static int
21960builtin_function(name)
21961 char_u *name;
21962{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021963 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21964 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021965}
21966
Bram Moolenaar05159a02005-02-26 23:04:13 +000021967#if defined(FEAT_PROFILE) || defined(PROTO)
21968/*
21969 * Start profiling function "fp".
21970 */
21971 static void
21972func_do_profile(fp)
21973 ufunc_T *fp;
21974{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021975 int len = fp->uf_lines.ga_len;
21976
21977 if (len == 0)
21978 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021979 fp->uf_tm_count = 0;
21980 profile_zero(&fp->uf_tm_self);
21981 profile_zero(&fp->uf_tm_total);
21982 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021983 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021984 if (fp->uf_tml_total == NULL)
21985 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021986 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021987 if (fp->uf_tml_self == NULL)
21988 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021989 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021990 fp->uf_tml_idx = -1;
21991 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21992 || fp->uf_tml_self == NULL)
21993 return; /* out of memory */
21994
21995 fp->uf_profiling = TRUE;
21996}
21997
21998/*
21999 * Dump the profiling results for all functions in file "fd".
22000 */
22001 void
22002func_dump_profile(fd)
22003 FILE *fd;
22004{
22005 hashitem_T *hi;
22006 int todo;
22007 ufunc_T *fp;
22008 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022009 ufunc_T **sorttab;
22010 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022011
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022012 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022013 if (todo == 0)
22014 return; /* nothing to dump */
22015
Bram Moolenaar73830342005-02-28 22:48:19 +000022016 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22017
Bram Moolenaar05159a02005-02-26 23:04:13 +000022018 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22019 {
22020 if (!HASHITEM_EMPTY(hi))
22021 {
22022 --todo;
22023 fp = HI2UF(hi);
22024 if (fp->uf_profiling)
22025 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022026 if (sorttab != NULL)
22027 sorttab[st_len++] = fp;
22028
Bram Moolenaar05159a02005-02-26 23:04:13 +000022029 if (fp->uf_name[0] == K_SPECIAL)
22030 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22031 else
22032 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22033 if (fp->uf_tm_count == 1)
22034 fprintf(fd, "Called 1 time\n");
22035 else
22036 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22037 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22038 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22039 fprintf(fd, "\n");
22040 fprintf(fd, "count total (s) self (s)\n");
22041
22042 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22043 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022044 if (FUNCLINE(fp, i) == NULL)
22045 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022046 prof_func_line(fd, fp->uf_tml_count[i],
22047 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022048 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22049 }
22050 fprintf(fd, "\n");
22051 }
22052 }
22053 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022054
22055 if (sorttab != NULL && st_len > 0)
22056 {
22057 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22058 prof_total_cmp);
22059 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22060 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22061 prof_self_cmp);
22062 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22063 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022064
22065 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022066}
Bram Moolenaar73830342005-02-28 22:48:19 +000022067
22068 static void
22069prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22070 FILE *fd;
22071 ufunc_T **sorttab;
22072 int st_len;
22073 char *title;
22074 int prefer_self; /* when equal print only self time */
22075{
22076 int i;
22077 ufunc_T *fp;
22078
22079 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22080 fprintf(fd, "count total (s) self (s) function\n");
22081 for (i = 0; i < 20 && i < st_len; ++i)
22082 {
22083 fp = sorttab[i];
22084 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22085 prefer_self);
22086 if (fp->uf_name[0] == K_SPECIAL)
22087 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22088 else
22089 fprintf(fd, " %s()\n", fp->uf_name);
22090 }
22091 fprintf(fd, "\n");
22092}
22093
22094/*
22095 * Print the count and times for one function or function line.
22096 */
22097 static void
22098prof_func_line(fd, count, total, self, prefer_self)
22099 FILE *fd;
22100 int count;
22101 proftime_T *total;
22102 proftime_T *self;
22103 int prefer_self; /* when equal print only self time */
22104{
22105 if (count > 0)
22106 {
22107 fprintf(fd, "%5d ", count);
22108 if (prefer_self && profile_equal(total, self))
22109 fprintf(fd, " ");
22110 else
22111 fprintf(fd, "%s ", profile_msg(total));
22112 if (!prefer_self && profile_equal(total, self))
22113 fprintf(fd, " ");
22114 else
22115 fprintf(fd, "%s ", profile_msg(self));
22116 }
22117 else
22118 fprintf(fd, " ");
22119}
22120
22121/*
22122 * Compare function for total time sorting.
22123 */
22124 static int
22125#ifdef __BORLANDC__
22126_RTLENTRYF
22127#endif
22128prof_total_cmp(s1, s2)
22129 const void *s1;
22130 const void *s2;
22131{
22132 ufunc_T *p1, *p2;
22133
22134 p1 = *(ufunc_T **)s1;
22135 p2 = *(ufunc_T **)s2;
22136 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22137}
22138
22139/*
22140 * Compare function for self time sorting.
22141 */
22142 static int
22143#ifdef __BORLANDC__
22144_RTLENTRYF
22145#endif
22146prof_self_cmp(s1, s2)
22147 const void *s1;
22148 const void *s2;
22149{
22150 ufunc_T *p1, *p2;
22151
22152 p1 = *(ufunc_T **)s1;
22153 p2 = *(ufunc_T **)s2;
22154 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22155}
22156
Bram Moolenaar05159a02005-02-26 23:04:13 +000022157#endif
22158
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022159/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022160 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022161 * Return TRUE if a package was loaded.
22162 */
22163 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022164script_autoload(name, reload)
22165 char_u *name;
22166 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022167{
22168 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022169 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022170 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022171 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022172
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022173 /* Return quickly when autoload disabled. */
22174 if (no_autoload)
22175 return FALSE;
22176
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022177 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022178 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022179 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022180 return FALSE;
22181
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022182 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022183
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022184 /* Find the name in the list of previously loaded package names. Skip
22185 * "autoload/", it's always the same. */
22186 for (i = 0; i < ga_loaded.ga_len; ++i)
22187 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22188 break;
22189 if (!reload && i < ga_loaded.ga_len)
22190 ret = FALSE; /* was loaded already */
22191 else
22192 {
22193 /* Remember the name if it wasn't loaded already. */
22194 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22195 {
22196 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22197 tofree = NULL;
22198 }
22199
22200 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022201 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022202 ret = TRUE;
22203 }
22204
22205 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022206 return ret;
22207}
22208
22209/*
22210 * Return the autoload script name for a function or variable name.
22211 * Returns NULL when out of memory.
22212 */
22213 static char_u *
22214autoload_name(name)
22215 char_u *name;
22216{
22217 char_u *p;
22218 char_u *scriptname;
22219
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022220 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022221 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22222 if (scriptname == NULL)
22223 return FALSE;
22224 STRCPY(scriptname, "autoload/");
22225 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022226 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022227 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022228 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022229 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022230 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022231}
22232
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22234
22235/*
22236 * Function given to ExpandGeneric() to obtain the list of user defined
22237 * function names.
22238 */
22239 char_u *
22240get_user_func_name(xp, idx)
22241 expand_T *xp;
22242 int idx;
22243{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022244 static long_u done;
22245 static hashitem_T *hi;
22246 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247
22248 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 done = 0;
22251 hi = func_hashtab.ht_array;
22252 }
22253 if (done < func_hashtab.ht_used)
22254 {
22255 if (done++ > 0)
22256 ++hi;
22257 while (HASHITEM_EMPTY(hi))
22258 ++hi;
22259 fp = HI2UF(hi);
22260
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022261 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022262 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022263
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022264 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22265 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266
22267 cat_func_name(IObuff, fp);
22268 if (xp->xp_context != EXPAND_USER_FUNC)
22269 {
22270 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022271 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 STRCAT(IObuff, ")");
22273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 return IObuff;
22275 }
22276 return NULL;
22277}
22278
22279#endif /* FEAT_CMDL_COMPL */
22280
22281/*
22282 * Copy the function name of "fp" to buffer "buf".
22283 * "buf" must be able to hold the function name plus three bytes.
22284 * Takes care of script-local function names.
22285 */
22286 static void
22287cat_func_name(buf, fp)
22288 char_u *buf;
22289 ufunc_T *fp;
22290{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022291 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 {
22293 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022294 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 }
22296 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022297 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298}
22299
22300/*
22301 * ":delfunction {name}"
22302 */
22303 void
22304ex_delfunction(eap)
22305 exarg_T *eap;
22306{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022307 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 char_u *p;
22309 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022310 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311
22312 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022313 name = trans_function_name(&p, eap->skip, 0, &fudi);
22314 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022316 {
22317 if (fudi.fd_dict != NULL && !eap->skip)
22318 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 if (!ends_excmd(*skipwhite(p)))
22322 {
22323 vim_free(name);
22324 EMSG(_(e_trailing));
22325 return;
22326 }
22327 eap->nextcmd = check_nextcmd(p);
22328 if (eap->nextcmd != NULL)
22329 *p = NUL;
22330
22331 if (!eap->skip)
22332 fp = find_func(name);
22333 vim_free(name);
22334
22335 if (!eap->skip)
22336 {
22337 if (fp == NULL)
22338 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022339 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 return;
22341 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022342 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 {
22344 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22345 return;
22346 }
22347
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022350 /* Delete the dict item that refers to the function, it will
22351 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022352 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 else
22355 func_free(fp);
22356 }
22357}
22358
22359/*
22360 * Free a function and remove it from the list of functions.
22361 */
22362 static void
22363func_free(fp)
22364 ufunc_T *fp;
22365{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022366 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022367
22368 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022369 ga_clear_strings(&(fp->uf_args));
22370 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022371#ifdef FEAT_PROFILE
22372 vim_free(fp->uf_tml_count);
22373 vim_free(fp->uf_tml_total);
22374 vim_free(fp->uf_tml_self);
22375#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022376
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022377 /* remove the function from the function hashtable */
22378 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22379 if (HASHITEM_EMPTY(hi))
22380 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022381 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022382 hash_remove(&func_hashtab, hi);
22383
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022384 vim_free(fp);
22385}
22386
22387/*
22388 * Unreference a Function: decrement the reference count and free it when it
22389 * becomes zero. Only for numbered functions.
22390 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022391 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022392func_unref(name)
22393 char_u *name;
22394{
22395 ufunc_T *fp;
22396
22397 if (name != NULL && isdigit(*name))
22398 {
22399 fp = find_func(name);
22400 if (fp == NULL)
22401 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022402 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022403 {
22404 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022405 * when "uf_calls" becomes zero. */
22406 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022407 func_free(fp);
22408 }
22409 }
22410}
22411
22412/*
22413 * Count a reference to a Function.
22414 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022415 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022416func_ref(name)
22417 char_u *name;
22418{
22419 ufunc_T *fp;
22420
22421 if (name != NULL && isdigit(*name))
22422 {
22423 fp = find_func(name);
22424 if (fp == NULL)
22425 EMSG2(_(e_intern2), "func_ref()");
22426 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022427 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429}
22430
22431/*
22432 * Call a user function.
22433 */
22434 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022435call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 ufunc_T *fp; /* pointer to function */
22437 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 typval_T *argvars; /* arguments */
22439 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 linenr_T firstline; /* first line of range */
22441 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443{
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 char_u *save_sourcing_name;
22445 linenr_T save_sourcing_lnum;
22446 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022447 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 int save_did_emsg;
22449 static int depth = 0;
22450 dictitem_T *v;
22451 int fixvar_idx = 0; /* index in fixvar[] */
22452 int i;
22453 int ai;
22454 char_u numbuf[NUMBUFLEN];
22455 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022456#ifdef FEAT_PROFILE
22457 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022458 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460
22461 /* If depth of calling is getting too high, don't execute the function */
22462 if (depth >= p_mfd)
22463 {
22464 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022465 rettv->v_type = VAR_NUMBER;
22466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 return;
22468 }
22469 ++depth;
22470
22471 line_breakcheck(); /* check for CTRL-C hit */
22472
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022473 fc = (funccall_T *)alloc(sizeof(funccall_T));
22474 fc->caller = current_funccal;
22475 current_funccal = fc;
22476 fc->func = fp;
22477 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022478 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022479 fc->linenr = 0;
22480 fc->returned = FALSE;
22481 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022483 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22484 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485
Bram Moolenaar33570922005-01-25 22:26:29 +000022486 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022487 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022488 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22489 * each argument variable and saves a lot of time.
22490 */
22491 /*
22492 * Init l: variables.
22493 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022494 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022495 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022496 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022497 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22498 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022499 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022500 name = v->di_key;
22501 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022503 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022504 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022505 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 v->di_tv.vval.v_dict = selfdict;
22507 ++selfdict->dv_refcount;
22508 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022509
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 /*
22511 * Init a: variables.
22512 * Set a:0 to "argcount".
22513 * Set a:000 to a list with room for the "..." arguments.
22514 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022515 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022516 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022517 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022518 /* Use "name" to avoid a warning from some compiler that checks the
22519 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022520 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022521 name = v->di_key;
22522 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022523 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022524 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022525 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022526 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022527 v->di_tv.vval.v_list = &fc->l_varlist;
22528 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22529 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22530 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022531
22532 /*
22533 * Set a:firstline to "firstline" and a:lastline to "lastline".
22534 * Set a:name to named arguments.
22535 * Set a:N to the "..." arguments.
22536 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022537 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022538 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022539 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 (varnumber_T)lastline);
22541 for (i = 0; i < argcount; ++i)
22542 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022543 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 if (ai < 0)
22545 /* named argument a:name */
22546 name = FUNCARG(fp, i);
22547 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022548 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 /* "..." argument a:1, a:2, etc. */
22550 sprintf((char *)numbuf, "%d", ai + 1);
22551 name = numbuf;
22552 }
22553 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22554 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022555 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22557 }
22558 else
22559 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022560 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22561 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022562 if (v == NULL)
22563 break;
22564 v->di_flags = DI_FLAGS_RO;
22565 }
22566 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022567 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022568
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022569 /* Note: the values are copied directly to avoid alloc/free.
22570 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022571 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022572 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022573
22574 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22575 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022576 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22577 fc->l_listitems[ai].li_tv = argvars[i];
22578 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022579 }
22580 }
22581
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 /* Don't redraw while executing the function. */
22583 ++RedrawingDisabled;
22584 save_sourcing_name = sourcing_name;
22585 save_sourcing_lnum = sourcing_lnum;
22586 sourcing_lnum = 1;
22587 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022588 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 if (sourcing_name != NULL)
22590 {
22591 if (save_sourcing_name != NULL
22592 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22593 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22594 else
22595 STRCPY(sourcing_name, "function ");
22596 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22597
22598 if (p_verbose >= 12)
22599 {
22600 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022601 verbose_enter_scroll();
22602
Bram Moolenaar555b2802005-05-19 21:08:39 +000022603 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 if (p_verbose >= 14)
22605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022607 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022608 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022609 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610
22611 msg_puts((char_u *)"(");
22612 for (i = 0; i < argcount; ++i)
22613 {
22614 if (i > 0)
22615 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 if (argvars[i].v_type == VAR_NUMBER)
22617 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 else
22619 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022620 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22621 if (s != NULL)
22622 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022623 if (vim_strsize(s) > MSG_BUF_CLEN)
22624 {
22625 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22626 s = buf;
22627 }
22628 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022629 vim_free(tofree);
22630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 }
22632 }
22633 msg_puts((char_u *)")");
22634 }
22635 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022636
22637 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 --no_wait_return;
22639 }
22640 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022641#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022642 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022643 {
22644 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22645 func_do_profile(fp);
22646 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022647 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022648 {
22649 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022650 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022651 profile_zero(&fp->uf_tm_children);
22652 }
22653 script_prof_save(&wait_start);
22654 }
22655#endif
22656
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022658 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 save_did_emsg = did_emsg;
22660 did_emsg = FALSE;
22661
22662 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022663 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22665
22666 --RedrawingDisabled;
22667
22668 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022669 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022671 clear_tv(rettv);
22672 rettv->v_type = VAR_NUMBER;
22673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 }
22675
Bram Moolenaar05159a02005-02-26 23:04:13 +000022676#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022677 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022678 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022679 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022680 profile_end(&call_start);
22681 profile_sub_wait(&wait_start, &call_start);
22682 profile_add(&fp->uf_tm_total, &call_start);
22683 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022684 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022685 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022686 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22687 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022688 }
22689 }
22690#endif
22691
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 /* when being verbose, mention the return value */
22693 if (p_verbose >= 12)
22694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022696 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022699 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022700 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022701 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022702 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022705 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022706 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022707 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022708 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022709
Bram Moolenaar555b2802005-05-19 21:08:39 +000022710 /* The value may be very long. Skip the middle part, so that we
22711 * have some idea how it starts and ends. smsg() would always
22712 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022713 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022714 if (s != NULL)
22715 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022716 if (vim_strsize(s) > MSG_BUF_CLEN)
22717 {
22718 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22719 s = buf;
22720 }
22721 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022722 vim_free(tofree);
22723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 }
22725 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022726
22727 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 --no_wait_return;
22729 }
22730
22731 vim_free(sourcing_name);
22732 sourcing_name = save_sourcing_name;
22733 sourcing_lnum = save_sourcing_lnum;
22734 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022735#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022736 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022737 script_prof_restore(&wait_start);
22738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739
22740 if (p_verbose >= 12 && sourcing_name != NULL)
22741 {
22742 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022743 verbose_enter_scroll();
22744
Bram Moolenaar555b2802005-05-19 21:08:39 +000022745 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022747
22748 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 --no_wait_return;
22750 }
22751
22752 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022753 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022755
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022756 /* If the a:000 list and the l: and a: dicts are not referenced we can
22757 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022758 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22759 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22760 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22761 {
22762 free_funccal(fc, FALSE);
22763 }
22764 else
22765 {
22766 hashitem_T *hi;
22767 listitem_T *li;
22768 int todo;
22769
22770 /* "fc" is still in use. This can happen when returning "a:000" or
22771 * assigning "l:" to a global variable.
22772 * Link "fc" in the list for garbage collection later. */
22773 fc->caller = previous_funccal;
22774 previous_funccal = fc;
22775
22776 /* Make a copy of the a: variables, since we didn't do that above. */
22777 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22778 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22779 {
22780 if (!HASHITEM_EMPTY(hi))
22781 {
22782 --todo;
22783 v = HI2DI(hi);
22784 copy_tv(&v->di_tv, &v->di_tv);
22785 }
22786 }
22787
22788 /* Make a copy of the a:000 items, since we didn't do that above. */
22789 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22790 copy_tv(&li->li_tv, &li->li_tv);
22791 }
22792}
22793
22794/*
22795 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022796 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022797 */
22798 static int
22799can_free_funccal(fc, copyID)
22800 funccall_T *fc;
22801 int copyID;
22802{
22803 return (fc->l_varlist.lv_copyID != copyID
22804 && fc->l_vars.dv_copyID != copyID
22805 && fc->l_avars.dv_copyID != copyID);
22806}
22807
22808/*
22809 * Free "fc" and what it contains.
22810 */
22811 static void
22812free_funccal(fc, free_val)
22813 funccall_T *fc;
22814 int free_val; /* a: vars were allocated */
22815{
22816 listitem_T *li;
22817
22818 /* The a: variables typevals may not have been allocated, only free the
22819 * allocated variables. */
22820 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22821
22822 /* free all l: variables */
22823 vars_clear(&fc->l_vars.dv_hashtab);
22824
22825 /* Free the a:000 variables if they were allocated. */
22826 if (free_val)
22827 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22828 clear_tv(&li->li_tv);
22829
22830 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831}
22832
22833/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 * Add a number variable "name" to dict "dp" with value "nr".
22835 */
22836 static void
22837add_nr_var(dp, v, name, nr)
22838 dict_T *dp;
22839 dictitem_T *v;
22840 char *name;
22841 varnumber_T nr;
22842{
22843 STRCPY(v->di_key, name);
22844 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22845 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22846 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022847 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022848 v->di_tv.vval.v_number = nr;
22849}
22850
22851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 * ":return [expr]"
22853 */
22854 void
22855ex_return(eap)
22856 exarg_T *eap;
22857{
22858 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022859 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 int returning = FALSE;
22861
22862 if (current_funccal == NULL)
22863 {
22864 EMSG(_("E133: :return not inside a function"));
22865 return;
22866 }
22867
22868 if (eap->skip)
22869 ++emsg_skip;
22870
22871 eap->nextcmd = NULL;
22872 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022873 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 {
22875 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022876 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022878 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 }
22880 /* It's safer to return also on error. */
22881 else if (!eap->skip)
22882 {
22883 /*
22884 * Return unless the expression evaluation has been cancelled due to an
22885 * aborting error, an interrupt, or an exception.
22886 */
22887 if (!aborting())
22888 returning = do_return(eap, FALSE, TRUE, NULL);
22889 }
22890
22891 /* When skipping or the return gets pending, advance to the next command
22892 * in this line (!returning). Otherwise, ignore the rest of the line.
22893 * Following lines will be ignored by get_func_line(). */
22894 if (returning)
22895 eap->nextcmd = NULL;
22896 else if (eap->nextcmd == NULL) /* no argument */
22897 eap->nextcmd = check_nextcmd(arg);
22898
22899 if (eap->skip)
22900 --emsg_skip;
22901}
22902
22903/*
22904 * Return from a function. Possibly makes the return pending. Also called
22905 * for a pending return at the ":endtry" or after returning from an extra
22906 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022908 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 * FALSE when the return gets pending.
22910 */
22911 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022912do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 exarg_T *eap;
22914 int reanimate;
22915 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022916 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917{
22918 int idx;
22919 struct condstack *cstack = eap->cstack;
22920
22921 if (reanimate)
22922 /* Undo the return. */
22923 current_funccal->returned = FALSE;
22924
22925 /*
22926 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22927 * not in its finally clause (which then is to be executed next) is found.
22928 * In this case, make the ":return" pending for execution at the ":endtry".
22929 * Otherwise, return normally.
22930 */
22931 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22932 if (idx >= 0)
22933 {
22934 cstack->cs_pending[idx] = CSTP_RETURN;
22935
22936 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022937 /* A pending return again gets pending. "rettv" points to an
22938 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022940 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 else
22942 {
22943 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022944 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022946 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022948 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 {
22950 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022951 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022952 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 else
22954 EMSG(_(e_outofmem));
22955 }
22956 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022957 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958
22959 if (reanimate)
22960 {
22961 /* The pending return value could be overwritten by a ":return"
22962 * without argument in a finally clause; reset the default
22963 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022964 current_funccal->rettv->v_type = VAR_NUMBER;
22965 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 }
22967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022968 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 }
22970 else
22971 {
22972 current_funccal->returned = TRUE;
22973
22974 /* If the return is carried out now, store the return value. For
22975 * a return immediately after reanimation, the value is already
22976 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022977 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022979 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022980 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022982 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 }
22984 }
22985
22986 return idx < 0;
22987}
22988
22989/*
22990 * Free the variable with a pending return value.
22991 */
22992 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022993discard_pending_return(rettv)
22994 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995{
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997}
22998
22999/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023000 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 * is an allocated string. Used by report_pending() for verbose messages.
23002 */
23003 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023004get_return_cmd(rettv)
23005 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023007 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023008 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023009 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023011 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023012 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023013 if (s == NULL)
23014 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023015
23016 STRCPY(IObuff, ":return ");
23017 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23018 if (STRLEN(s) + 8 >= IOSIZE)
23019 STRCPY(IObuff + IOSIZE - 4, "...");
23020 vim_free(tofree);
23021 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022}
23023
23024/*
23025 * Get next function line.
23026 * Called by do_cmdline() to get the next line.
23027 * Returns allocated string, or NULL for end of function.
23028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 char_u *
23030get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023031 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023033 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034{
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023036 ufunc_T *fp = fcp->func;
23037 char_u *retval;
23038 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039
23040 /* If breakpoints have been added/deleted need to check for it. */
23041 if (fcp->dbg_tick != debug_tick)
23042 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023043 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 sourcing_lnum);
23045 fcp->dbg_tick = debug_tick;
23046 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023047#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023048 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023049 func_line_end(cookie);
23050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051
Bram Moolenaar05159a02005-02-26 23:04:13 +000023052 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023053 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23054 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 retval = NULL;
23056 else
23057 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023058 /* Skip NULL lines (continuation lines). */
23059 while (fcp->linenr < gap->ga_len
23060 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23061 ++fcp->linenr;
23062 if (fcp->linenr >= gap->ga_len)
23063 retval = NULL;
23064 else
23065 {
23066 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23067 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023068#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023069 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023070 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023071#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 }
23074
23075 /* Did we encounter a breakpoint? */
23076 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23077 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023078 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023080 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 sourcing_lnum);
23082 fcp->dbg_tick = debug_tick;
23083 }
23084
23085 return retval;
23086}
23087
Bram Moolenaar05159a02005-02-26 23:04:13 +000023088#if defined(FEAT_PROFILE) || defined(PROTO)
23089/*
23090 * Called when starting to read a function line.
23091 * "sourcing_lnum" must be correct!
23092 * When skipping lines it may not actually be executed, but we won't find out
23093 * until later and we need to store the time now.
23094 */
23095 void
23096func_line_start(cookie)
23097 void *cookie;
23098{
23099 funccall_T *fcp = (funccall_T *)cookie;
23100 ufunc_T *fp = fcp->func;
23101
23102 if (fp->uf_profiling && sourcing_lnum >= 1
23103 && sourcing_lnum <= fp->uf_lines.ga_len)
23104 {
23105 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023106 /* Skip continuation lines. */
23107 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23108 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023109 fp->uf_tml_execed = FALSE;
23110 profile_start(&fp->uf_tml_start);
23111 profile_zero(&fp->uf_tml_children);
23112 profile_get_wait(&fp->uf_tml_wait);
23113 }
23114}
23115
23116/*
23117 * Called when actually executing a function line.
23118 */
23119 void
23120func_line_exec(cookie)
23121 void *cookie;
23122{
23123 funccall_T *fcp = (funccall_T *)cookie;
23124 ufunc_T *fp = fcp->func;
23125
23126 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23127 fp->uf_tml_execed = TRUE;
23128}
23129
23130/*
23131 * Called when done with a function line.
23132 */
23133 void
23134func_line_end(cookie)
23135 void *cookie;
23136{
23137 funccall_T *fcp = (funccall_T *)cookie;
23138 ufunc_T *fp = fcp->func;
23139
23140 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23141 {
23142 if (fp->uf_tml_execed)
23143 {
23144 ++fp->uf_tml_count[fp->uf_tml_idx];
23145 profile_end(&fp->uf_tml_start);
23146 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023147 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023148 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23149 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023150 }
23151 fp->uf_tml_idx = -1;
23152 }
23153}
23154#endif
23155
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156/*
23157 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023158 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 */
23160 int
23161func_has_ended(cookie)
23162 void *cookie;
23163{
Bram Moolenaar33570922005-01-25 22:26:29 +000023164 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165
23166 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23167 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023168 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 || fcp->returned);
23170}
23171
23172/*
23173 * return TRUE if cookie indicates a function which "abort"s on errors.
23174 */
23175 int
23176func_has_abort(cookie)
23177 void *cookie;
23178{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023179 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180}
23181
23182#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23183typedef enum
23184{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023185 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23186 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23187 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188} var_flavour_T;
23189
23190static var_flavour_T var_flavour __ARGS((char_u *varname));
23191
23192 static var_flavour_T
23193var_flavour(varname)
23194 char_u *varname;
23195{
23196 char_u *p = varname;
23197
23198 if (ASCII_ISUPPER(*p))
23199 {
23200 while (*(++p))
23201 if (ASCII_ISLOWER(*p))
23202 return VAR_FLAVOUR_SESSION;
23203 return VAR_FLAVOUR_VIMINFO;
23204 }
23205 else
23206 return VAR_FLAVOUR_DEFAULT;
23207}
23208#endif
23209
23210#if defined(FEAT_VIMINFO) || defined(PROTO)
23211/*
23212 * Restore global vars that start with a capital from the viminfo file
23213 */
23214 int
23215read_viminfo_varlist(virp, writing)
23216 vir_T *virp;
23217 int writing;
23218{
23219 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023220 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222
23223 if (!writing && (find_viminfo_parameter('!') != NULL))
23224 {
23225 tab = vim_strchr(virp->vir_line + 1, '\t');
23226 if (tab != NULL)
23227 {
23228 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023229 switch (*tab)
23230 {
23231 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023232#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023233 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023234#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023235 case 'D': type = VAR_DICT; break;
23236 case 'L': type = VAR_LIST; break;
23237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238
23239 tab = vim_strchr(tab, '\t');
23240 if (tab != NULL)
23241 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023242 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023243 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023244 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023246#ifdef FEAT_FLOAT
23247 else if (type == VAR_FLOAT)
23248 (void)string2float(tab + 1, &tv.vval.v_float);
23249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023251 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023252 if (type == VAR_DICT || type == VAR_LIST)
23253 {
23254 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23255
23256 if (etv == NULL)
23257 /* Failed to parse back the dict or list, use it as a
23258 * string. */
23259 tv.v_type = VAR_STRING;
23260 else
23261 {
23262 vim_free(tv.vval.v_string);
23263 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023264 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023265 }
23266 }
23267
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023268 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023269
23270 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023271 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023272 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23273 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 }
23275 }
23276 }
23277
23278 return viminfo_readline(virp);
23279}
23280
23281/*
23282 * Write global vars that start with a capital to the viminfo file
23283 */
23284 void
23285write_viminfo_varlist(fp)
23286 FILE *fp;
23287{
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 hashitem_T *hi;
23289 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023290 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023291 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023292 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023293 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023294 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295
23296 if (find_viminfo_parameter('!') == NULL)
23297 return;
23298
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023299 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023301 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023302 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023304 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023306 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 this_var = HI2DI(hi);
23308 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023309 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023311 {
23312 case VAR_STRING: s = "STR"; break;
23313 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023314#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023315 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023316#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023317 case VAR_DICT: s = "DIC"; break;
23318 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023319 default: continue;
23320 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023321 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023322 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023323 if (p != NULL)
23324 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023325 vim_free(tofree);
23326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 }
23328 }
23329}
23330#endif
23331
23332#if defined(FEAT_SESSION) || defined(PROTO)
23333 int
23334store_session_globals(fd)
23335 FILE *fd;
23336{
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 hashitem_T *hi;
23338 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023339 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 char_u *p, *t;
23341
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023342 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023343 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023345 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023347 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023348 this_var = HI2DI(hi);
23349 if ((this_var->di_tv.v_type == VAR_NUMBER
23350 || this_var->di_tv.v_type == VAR_STRING)
23351 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023352 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023353 /* Escape special characters with a backslash. Turn a LF and
23354 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023356 (char_u *)"\\\"\n\r");
23357 if (p == NULL) /* out of memory */
23358 break;
23359 for (t = p; *t != NUL; ++t)
23360 if (*t == '\n')
23361 *t = 'n';
23362 else if (*t == '\r')
23363 *t = 'r';
23364 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023365 this_var->di_key,
23366 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23367 : ' ',
23368 p,
23369 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23370 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023371 || put_eol(fd) == FAIL)
23372 {
23373 vim_free(p);
23374 return FAIL;
23375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 vim_free(p);
23377 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023378#ifdef FEAT_FLOAT
23379 else if (this_var->di_tv.v_type == VAR_FLOAT
23380 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23381 {
23382 float_T f = this_var->di_tv.vval.v_float;
23383 int sign = ' ';
23384
23385 if (f < 0)
23386 {
23387 f = -f;
23388 sign = '-';
23389 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023390 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023391 this_var->di_key, sign, f) < 0)
23392 || put_eol(fd) == FAIL)
23393 return FAIL;
23394 }
23395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 }
23397 }
23398 return OK;
23399}
23400#endif
23401
Bram Moolenaar661b1822005-07-28 22:36:45 +000023402/*
23403 * Display script name where an item was last set.
23404 * Should only be invoked when 'verbose' is non-zero.
23405 */
23406 void
23407last_set_msg(scriptID)
23408 scid_T scriptID;
23409{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023410 char_u *p;
23411
Bram Moolenaar661b1822005-07-28 22:36:45 +000023412 if (scriptID != 0)
23413 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023414 p = home_replace_save(NULL, get_scriptname(scriptID));
23415 if (p != NULL)
23416 {
23417 verbose_enter();
23418 MSG_PUTS(_("\n\tLast set from "));
23419 MSG_PUTS(p);
23420 vim_free(p);
23421 verbose_leave();
23422 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023423 }
23424}
23425
Bram Moolenaard812df62008-11-09 12:46:09 +000023426/*
23427 * List v:oldfiles in a nice way.
23428 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023429 void
23430ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023431 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023432{
23433 list_T *l = vimvars[VV_OLDFILES].vv_list;
23434 listitem_T *li;
23435 int nr = 0;
23436
23437 if (l == NULL)
23438 msg((char_u *)_("No old files"));
23439 else
23440 {
23441 msg_start();
23442 msg_scroll = TRUE;
23443 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23444 {
23445 msg_outnum((long)++nr);
23446 MSG_PUTS(": ");
23447 msg_outtrans(get_tv_string(&li->li_tv));
23448 msg_putchar('\n');
23449 out_flush(); /* output one line at a time */
23450 ui_breakcheck();
23451 }
23452 /* Assume "got_int" was set to truncate the listing. */
23453 got_int = FALSE;
23454
23455#ifdef FEAT_BROWSE_CMD
23456 if (cmdmod.browse)
23457 {
23458 quit_more = FALSE;
23459 nr = prompt_for_number(FALSE);
23460 msg_starthere();
23461 if (nr > 0)
23462 {
23463 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23464 (long)nr);
23465
23466 if (p != NULL)
23467 {
23468 p = expand_env_save(p);
23469 eap->arg = p;
23470 eap->cmdidx = CMD_edit;
23471 cmdmod.browse = FALSE;
23472 do_exedit(eap, NULL);
23473 vim_free(p);
23474 }
23475 }
23476 }
23477#endif
23478 }
23479}
23480
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481#endif /* FEAT_EVAL */
23482
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023484#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485
23486#ifdef WIN3264
23487/*
23488 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23489 */
23490static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23491static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23492static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23493
23494/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023495 * Get the short path (8.3) for the filename in "fnamep".
23496 * Only works for a valid file name.
23497 * When the path gets longer "fnamep" is changed and the allocated buffer
23498 * is put in "bufp".
23499 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23500 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 */
23502 static int
23503get_short_pathname(fnamep, bufp, fnamelen)
23504 char_u **fnamep;
23505 char_u **bufp;
23506 int *fnamelen;
23507{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023508 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 char_u *newbuf;
23510
23511 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 l = GetShortPathName(*fnamep, *fnamep, len);
23513 if (l > len - 1)
23514 {
23515 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023516 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 newbuf = vim_strnsave(*fnamep, l);
23518 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023519 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520
23521 vim_free(*bufp);
23522 *fnamep = *bufp = newbuf;
23523
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023524 /* Really should always succeed, as the buffer is big enough. */
23525 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 }
23527
23528 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023529 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530}
23531
23532/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023533 * Get the short path (8.3) for the filename in "fname". The converted
23534 * path is returned in "bufp".
23535 *
23536 * Some of the directories specified in "fname" may not exist. This function
23537 * will shorten the existing directories at the beginning of the path and then
23538 * append the remaining non-existing path.
23539 *
23540 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023541 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023542 * bufp - Pointer to an allocated buffer for the filename.
23543 * fnamelen - Length of the filename pointed to by fname
23544 *
23545 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 */
23547 static int
23548shortpath_for_invalid_fname(fname, bufp, fnamelen)
23549 char_u **fname;
23550 char_u **bufp;
23551 int *fnamelen;
23552{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023553 char_u *short_fname, *save_fname, *pbuf_unused;
23554 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023556 int old_len, len;
23557 int new_len, sfx_len;
23558 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559
23560 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023561 old_len = *fnamelen;
23562 save_fname = vim_strnsave(*fname, old_len);
23563 pbuf_unused = NULL;
23564 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023566 endp = save_fname + old_len - 1; /* Find the end of the copy */
23567 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023569 /*
23570 * Try shortening the supplied path till it succeeds by removing one
23571 * directory at a time from the tail of the path.
23572 */
23573 len = 0;
23574 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023576 /* go back one path-separator */
23577 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23578 --endp;
23579 if (endp <= save_fname)
23580 break; /* processed the complete path */
23581
23582 /*
23583 * Replace the path separator with a NUL and try to shorten the
23584 * resulting path.
23585 */
23586 ch = *endp;
23587 *endp = 0;
23588 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023589 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023590 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23591 {
23592 retval = FAIL;
23593 goto theend;
23594 }
23595 *endp = ch; /* preserve the string */
23596
23597 if (len > 0)
23598 break; /* successfully shortened the path */
23599
23600 /* failed to shorten the path. Skip the path separator */
23601 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 }
23603
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023604 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023606 /*
23607 * Succeeded in shortening the path. Now concatenate the shortened
23608 * path with the remaining path at the tail.
23609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023611 /* Compute the length of the new path. */
23612 sfx_len = (int)(save_endp - endp) + 1;
23613 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023615 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023617 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023619 /* There is not enough space in the currently allocated string,
23620 * copy it to a buffer big enough. */
23621 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023623 {
23624 retval = FAIL;
23625 goto theend;
23626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 }
23628 else
23629 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023630 /* Transfer short_fname to the main buffer (it's big enough),
23631 * unless get_short_pathname() did its work in-place. */
23632 *fname = *bufp = save_fname;
23633 if (short_fname != save_fname)
23634 vim_strncpy(save_fname, short_fname, len);
23635 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023637
23638 /* concat the not-shortened part of the path */
23639 vim_strncpy(*fname + len, endp, sfx_len);
23640 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023642
23643theend:
23644 vim_free(pbuf_unused);
23645 vim_free(save_fname);
23646
23647 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648}
23649
23650/*
23651 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023652 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 */
23654 static int
23655shortpath_for_partial(fnamep, bufp, fnamelen)
23656 char_u **fnamep;
23657 char_u **bufp;
23658 int *fnamelen;
23659{
23660 int sepcount, len, tflen;
23661 char_u *p;
23662 char_u *pbuf, *tfname;
23663 int hasTilde;
23664
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023665 /* Count up the path separators from the RHS.. so we know which part
23666 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023668 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 if (vim_ispathsep(*p))
23670 ++sepcount;
23671
23672 /* Need full path first (use expand_env() to remove a "~/") */
23673 hasTilde = (**fnamep == '~');
23674 if (hasTilde)
23675 pbuf = tfname = expand_env_save(*fnamep);
23676 else
23677 pbuf = tfname = FullName_save(*fnamep, FALSE);
23678
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023679 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023681 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683
23684 if (len == 0)
23685 {
23686 /* Don't have a valid filename, so shorten the rest of the
23687 * path if we can. This CAN give us invalid 8.3 filenames, but
23688 * there's not a lot of point in guessing what it might be.
23689 */
23690 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023691 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 }
23694
23695 /* Count the paths backward to find the beginning of the desired string. */
23696 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023697 {
23698#ifdef FEAT_MBYTE
23699 if (has_mbyte)
23700 p -= mb_head_off(tfname, p);
23701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 if (vim_ispathsep(*p))
23703 {
23704 if (sepcount == 0 || (hasTilde && sepcount == 1))
23705 break;
23706 else
23707 sepcount --;
23708 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 if (hasTilde)
23711 {
23712 --p;
23713 if (p >= tfname)
23714 *p = '~';
23715 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023716 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 }
23718 else
23719 ++p;
23720
23721 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23722 vim_free(*bufp);
23723 *fnamelen = (int)STRLEN(p);
23724 *bufp = pbuf;
23725 *fnamep = p;
23726
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023727 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728}
23729#endif /* WIN3264 */
23730
23731/*
23732 * Adjust a filename, according to a string of modifiers.
23733 * *fnamep must be NUL terminated when called. When returning, the length is
23734 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023735 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 * When there is an error, *fnamep is set to NULL.
23737 */
23738 int
23739modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23740 char_u *src; /* string with modifiers */
23741 int *usedlen; /* characters after src that are used */
23742 char_u **fnamep; /* file name so far */
23743 char_u **bufp; /* buffer for allocated file name or NULL */
23744 int *fnamelen; /* length of fnamep */
23745{
23746 int valid = 0;
23747 char_u *tail;
23748 char_u *s, *p, *pbuf;
23749 char_u dirname[MAXPATHL];
23750 int c;
23751 int has_fullname = 0;
23752#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023753 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 int has_shortname = 0;
23755#endif
23756
23757repeat:
23758 /* ":p" - full path/file_name */
23759 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23760 {
23761 has_fullname = 1;
23762
23763 valid |= VALID_PATH;
23764 *usedlen += 2;
23765
23766 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23767 if ((*fnamep)[0] == '~'
23768#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23769 && ((*fnamep)[1] == '/'
23770# ifdef BACKSLASH_IN_FILENAME
23771 || (*fnamep)[1] == '\\'
23772# endif
23773 || (*fnamep)[1] == NUL)
23774
23775#endif
23776 )
23777 {
23778 *fnamep = expand_env_save(*fnamep);
23779 vim_free(*bufp); /* free any allocated file name */
23780 *bufp = *fnamep;
23781 if (*fnamep == NULL)
23782 return -1;
23783 }
23784
23785 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023786 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 {
23788 if (vim_ispathsep(*p)
23789 && p[1] == '.'
23790 && (p[2] == NUL
23791 || vim_ispathsep(p[2])
23792 || (p[2] == '.'
23793 && (p[3] == NUL || vim_ispathsep(p[3])))))
23794 break;
23795 }
23796
23797 /* FullName_save() is slow, don't use it when not needed. */
23798 if (*p != NUL || !vim_isAbsName(*fnamep))
23799 {
23800 *fnamep = FullName_save(*fnamep, *p != NUL);
23801 vim_free(*bufp); /* free any allocated file name */
23802 *bufp = *fnamep;
23803 if (*fnamep == NULL)
23804 return -1;
23805 }
23806
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023807#ifdef WIN3264
23808# if _WIN32_WINNT >= 0x0500
23809 if (vim_strchr(*fnamep, '~') != NULL)
23810 {
23811 /* Expand 8.3 filename to full path. Needed to make sure the same
23812 * file does not have two different names.
23813 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23814 p = alloc(_MAX_PATH + 1);
23815 if (p != NULL)
23816 {
23817 if (GetLongPathName(*fnamep, p, MAXPATHL))
23818 {
23819 vim_free(*bufp);
23820 *bufp = *fnamep = p;
23821 }
23822 else
23823 vim_free(p);
23824 }
23825 }
23826# endif
23827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 /* Append a path separator to a directory. */
23829 if (mch_isdir(*fnamep))
23830 {
23831 /* Make room for one or two extra characters. */
23832 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23833 vim_free(*bufp); /* free any allocated file name */
23834 *bufp = *fnamep;
23835 if (*fnamep == NULL)
23836 return -1;
23837 add_pathsep(*fnamep);
23838 }
23839 }
23840
23841 /* ":." - path relative to the current directory */
23842 /* ":~" - path relative to the home directory */
23843 /* ":8" - shortname path - postponed till after */
23844 while (src[*usedlen] == ':'
23845 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23846 {
23847 *usedlen += 2;
23848 if (c == '8')
23849 {
23850#ifdef WIN3264
23851 has_shortname = 1; /* Postpone this. */
23852#endif
23853 continue;
23854 }
23855 pbuf = NULL;
23856 /* Need full path first (use expand_env() to remove a "~/") */
23857 if (!has_fullname)
23858 {
23859 if (c == '.' && **fnamep == '~')
23860 p = pbuf = expand_env_save(*fnamep);
23861 else
23862 p = pbuf = FullName_save(*fnamep, FALSE);
23863 }
23864 else
23865 p = *fnamep;
23866
23867 has_fullname = 0;
23868
23869 if (p != NULL)
23870 {
23871 if (c == '.')
23872 {
23873 mch_dirname(dirname, MAXPATHL);
23874 s = shorten_fname(p, dirname);
23875 if (s != NULL)
23876 {
23877 *fnamep = s;
23878 if (pbuf != NULL)
23879 {
23880 vim_free(*bufp); /* free any allocated file name */
23881 *bufp = pbuf;
23882 pbuf = NULL;
23883 }
23884 }
23885 }
23886 else
23887 {
23888 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23889 /* Only replace it when it starts with '~' */
23890 if (*dirname == '~')
23891 {
23892 s = vim_strsave(dirname);
23893 if (s != NULL)
23894 {
23895 *fnamep = s;
23896 vim_free(*bufp);
23897 *bufp = s;
23898 }
23899 }
23900 }
23901 vim_free(pbuf);
23902 }
23903 }
23904
23905 tail = gettail(*fnamep);
23906 *fnamelen = (int)STRLEN(*fnamep);
23907
23908 /* ":h" - head, remove "/file_name", can be repeated */
23909 /* Don't remove the first "/" or "c:\" */
23910 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23911 {
23912 valid |= VALID_HEAD;
23913 *usedlen += 2;
23914 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023915 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023916 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 *fnamelen = (int)(tail - *fnamep);
23918#ifdef VMS
23919 if (*fnamelen > 0)
23920 *fnamelen += 1; /* the path separator is part of the path */
23921#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023922 if (*fnamelen == 0)
23923 {
23924 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23925 p = vim_strsave((char_u *)".");
23926 if (p == NULL)
23927 return -1;
23928 vim_free(*bufp);
23929 *bufp = *fnamep = tail = p;
23930 *fnamelen = 1;
23931 }
23932 else
23933 {
23934 while (tail > s && !after_pathsep(s, tail))
23935 mb_ptr_back(*fnamep, tail);
23936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 }
23938
23939 /* ":8" - shortname */
23940 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23941 {
23942 *usedlen += 2;
23943#ifdef WIN3264
23944 has_shortname = 1;
23945#endif
23946 }
23947
23948#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023949 /*
23950 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 */
23952 if (has_shortname)
23953 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023954 /* Copy the string if it is shortened by :h and when it wasn't copied
23955 * yet, because we are going to change it in place. Avoids changing
23956 * the buffer name for "%:8". */
23957 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 {
23959 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023960 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 return -1;
23962 vim_free(*bufp);
23963 *bufp = *fnamep = p;
23964 }
23965
23966 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023967 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 if (!has_fullname && !vim_isAbsName(*fnamep))
23969 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023970 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 return -1;
23972 }
23973 else
23974 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023975 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976
Bram Moolenaardc935552011-08-17 15:23:23 +020023977 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023979 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 return -1;
23981
23982 if (l == 0)
23983 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023984 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023986 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 return -1;
23988 }
23989 *fnamelen = l;
23990 }
23991 }
23992#endif /* WIN3264 */
23993
23994 /* ":t" - tail, just the basename */
23995 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23996 {
23997 *usedlen += 2;
23998 *fnamelen -= (int)(tail - *fnamep);
23999 *fnamep = tail;
24000 }
24001
24002 /* ":e" - extension, can be repeated */
24003 /* ":r" - root, without extension, can be repeated */
24004 while (src[*usedlen] == ':'
24005 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24006 {
24007 /* find a '.' in the tail:
24008 * - for second :e: before the current fname
24009 * - otherwise: The last '.'
24010 */
24011 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24012 s = *fnamep - 2;
24013 else
24014 s = *fnamep + *fnamelen - 1;
24015 for ( ; s > tail; --s)
24016 if (s[0] == '.')
24017 break;
24018 if (src[*usedlen + 1] == 'e') /* :e */
24019 {
24020 if (s > tail)
24021 {
24022 *fnamelen += (int)(*fnamep - (s + 1));
24023 *fnamep = s + 1;
24024#ifdef VMS
24025 /* cut version from the extension */
24026 s = *fnamep + *fnamelen - 1;
24027 for ( ; s > *fnamep; --s)
24028 if (s[0] == ';')
24029 break;
24030 if (s > *fnamep)
24031 *fnamelen = s - *fnamep;
24032#endif
24033 }
24034 else if (*fnamep <= tail)
24035 *fnamelen = 0;
24036 }
24037 else /* :r */
24038 {
24039 if (s > tail) /* remove one extension */
24040 *fnamelen = (int)(s - *fnamep);
24041 }
24042 *usedlen += 2;
24043 }
24044
24045 /* ":s?pat?foo?" - substitute */
24046 /* ":gs?pat?foo?" - global substitute */
24047 if (src[*usedlen] == ':'
24048 && (src[*usedlen + 1] == 's'
24049 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24050 {
24051 char_u *str;
24052 char_u *pat;
24053 char_u *sub;
24054 int sep;
24055 char_u *flags;
24056 int didit = FALSE;
24057
24058 flags = (char_u *)"";
24059 s = src + *usedlen + 2;
24060 if (src[*usedlen + 1] == 'g')
24061 {
24062 flags = (char_u *)"g";
24063 ++s;
24064 }
24065
24066 sep = *s++;
24067 if (sep)
24068 {
24069 /* find end of pattern */
24070 p = vim_strchr(s, sep);
24071 if (p != NULL)
24072 {
24073 pat = vim_strnsave(s, (int)(p - s));
24074 if (pat != NULL)
24075 {
24076 s = p + 1;
24077 /* find end of substitution */
24078 p = vim_strchr(s, sep);
24079 if (p != NULL)
24080 {
24081 sub = vim_strnsave(s, (int)(p - s));
24082 str = vim_strnsave(*fnamep, *fnamelen);
24083 if (sub != NULL && str != NULL)
24084 {
24085 *usedlen = (int)(p + 1 - src);
24086 s = do_string_sub(str, pat, sub, flags);
24087 if (s != NULL)
24088 {
24089 *fnamep = s;
24090 *fnamelen = (int)STRLEN(s);
24091 vim_free(*bufp);
24092 *bufp = s;
24093 didit = TRUE;
24094 }
24095 }
24096 vim_free(sub);
24097 vim_free(str);
24098 }
24099 vim_free(pat);
24100 }
24101 }
24102 /* after using ":s", repeat all the modifiers */
24103 if (didit)
24104 goto repeat;
24105 }
24106 }
24107
24108 return valid;
24109}
24110
24111/*
24112 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24113 * "flags" can be "g" to do a global substitute.
24114 * Returns an allocated string, NULL for error.
24115 */
24116 char_u *
24117do_string_sub(str, pat, sub, flags)
24118 char_u *str;
24119 char_u *pat;
24120 char_u *sub;
24121 char_u *flags;
24122{
24123 int sublen;
24124 regmatch_T regmatch;
24125 int i;
24126 int do_all;
24127 char_u *tail;
24128 garray_T ga;
24129 char_u *ret;
24130 char_u *save_cpo;
24131
24132 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24133 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024134 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135
24136 ga_init2(&ga, 1, 200);
24137
24138 do_all = (flags[0] == 'g');
24139
24140 regmatch.rm_ic = p_ic;
24141 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24142 if (regmatch.regprog != NULL)
24143 {
24144 tail = str;
24145 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24146 {
24147 /*
24148 * Get some space for a temporary buffer to do the substitution
24149 * into. It will contain:
24150 * - The text up to where the match is.
24151 * - The substituted text.
24152 * - The text after the match.
24153 */
24154 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24155 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24156 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24157 {
24158 ga_clear(&ga);
24159 break;
24160 }
24161
24162 /* copy the text up to where the match is */
24163 i = (int)(regmatch.startp[0] - tail);
24164 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24165 /* add the substituted text */
24166 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24167 + ga.ga_len + i, TRUE, TRUE, FALSE);
24168 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 /* avoid getting stuck on a match with an empty string */
24170 if (tail == regmatch.endp[0])
24171 {
24172 if (*tail == NUL)
24173 break;
24174 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24175 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 }
24177 else
24178 {
24179 tail = regmatch.endp[0];
24180 if (*tail == NUL)
24181 break;
24182 }
24183 if (!do_all)
24184 break;
24185 }
24186
24187 if (ga.ga_data != NULL)
24188 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24189
24190 vim_free(regmatch.regprog);
24191 }
24192
24193 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24194 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024195 if (p_cpo == empty_option)
24196 p_cpo = save_cpo;
24197 else
24198 /* Darn, evaluating {sub} expression changed the value. */
24199 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200
24201 return ret;
24202}
24203
24204#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */