blob: f1285ca392e75e2b138a7350a87d9d54f344aed0 [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 Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200129/* When using exists() don't auto-load a script. */
130static int no_autoload = FALSE;
131
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 * When recursively copying lists and dicts we need to remember which ones we
134 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136 */
137static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000138#define COPYID_INC 2
139#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140
141/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000142 * Array to hold the hashtab with variables local to each sourced script.
143 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000145typedef struct
146{
147 dictitem_T sv_var;
148 dict_T sv_dict;
149} scriptvar_T;
150
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200151static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
152#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
153#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155static int echo_attr = 0; /* attributes used for ":echo" */
156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000157/* Values for trans_function_name() argument: */
158#define TFN_INT 1 /* internal function name OK */
159#define TFN_QUIET 2 /* no error messages */
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Structure to hold info for a user function.
163 */
164typedef struct ufunc ufunc_T;
165
166struct ufunc
167{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000168 int uf_varargs; /* variable nr of arguments */
169 int uf_flags;
170 int uf_calls; /* nr of active calls */
171 garray_T uf_args; /* arguments */
172 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173#ifdef FEAT_PROFILE
174 int uf_profiling; /* TRUE when func is being profiled */
175 /* profiling the function as a whole */
176 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000177 proftime_T uf_tm_total; /* time spent in function + children */
178 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000179 proftime_T uf_tm_children; /* time spent in children this call */
180 /* profiling the function per line */
181 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T *uf_tml_total; /* time spent in a line + children */
183 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tml_start; /* start time for current line */
185 proftime_T uf_tml_children; /* time spent in children for this line */
186 proftime_T uf_tml_wait; /* start wait time for current line */
187 int uf_tml_idx; /* index of line being timed; -1 if none */
188 int uf_tml_execed; /* line being timed was executed */
189#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000190 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 int uf_refcount; /* for numbered function: reference count */
193 char_u uf_name[1]; /* name of function (actually longer); can
194 start with <SNR>123_ (<SNR> is K_SPECIAL
195 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196};
197
198/* function flags */
199#define FC_ABORT 1 /* abort function on error */
200#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000201#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000206static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000208/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000209static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
210
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000211/* list heads for garbage collection */
212static dict_T *first_dict = NULL; /* list of all dicts */
213static list_T *first_list = NULL; /* list of all lists */
214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000215/* From user function to hashitem and back. */
216static ufunc_T dumuf;
217#define UF2HIKEY(fp) ((fp)->uf_name)
218#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
219#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
220
221#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
222#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar33570922005-01-25 22:26:29 +0000224#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
225#define VAR_SHORT_LEN 20 /* short variable name length */
226#define FIXVAR_CNT 12 /* number of fixed variables */
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000229typedef struct funccall_S funccall_T;
230
231struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 ufunc_T *func; /* function being called */
234 int linenr; /* next line to be executed */
235 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000236 struct /* fixed variables for arguments */
237 {
238 dictitem_T var; /* variable (without room for name) */
239 char_u room[VAR_SHORT_LEN]; /* room for the name */
240 } fixvar[FIXVAR_CNT];
241 dict_T l_vars; /* l: local function variables */
242 dictitem_T l_vars_var; /* variable for l: scope */
243 dict_T l_avars; /* a: argument variables */
244 dictitem_T l_avars_var; /* variable for a: scope */
245 list_T l_varlist; /* list for a:000 */
246 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 linenr_T breakpoint; /* next line with breakpoint or zero */
249 int dbg_tick; /* debug_tick when breakpoint was set */
250 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000251#ifdef FEAT_PROFILE
252 proftime_T prof_child; /* time spent in a child */
253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000254 funccall_T *caller; /* calling function or NULL */
255};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258 * Info used by a ":for" loop.
259 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261{
262 int fi_semicolon; /* TRUE if ending in '; var]' */
263 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 listwatch_T fi_lw; /* keep an eye on the item used. */
265 list_T *fi_list; /* list being used */
266} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000269 * Struct used by trans_function_name()
270 */
271typedef struct
272{
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000274 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dictitem_T *fd_di; /* Dictionary item used */
276} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000277
Bram Moolenaara7043832005-01-21 11:56:39 +0000278
279/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 * Array to hold the value of v: variables.
281 * The value is in a dictitem, so that it can also be used in the v: scope.
282 * The reason to use this table anyway is for very quick access to the
283 * variables with the VV_ defines.
284 */
285#include "version.h"
286
287/* values for vv_flags: */
288#define VV_COMPAT 1 /* compatible, also used without "v:" */
289#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000290#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000292#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294static struct vimvar
295{
296 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297 dictitem_T vv_di; /* value and name for key */
298 char vv_filler[16]; /* space for LONGEST name below!!! */
299 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300} vimvars[VV_LEN] =
301{
302 /*
303 * The order here must match the VV_ defines in vim.h!
304 * Initializing a union does not work, leave tv.vval empty to get zero's.
305 */
306 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("count1", VAR_NUMBER), VV_RO},
308 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
309 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
310 {VV_NAME("warningmsg", VAR_STRING), 0},
311 {VV_NAME("statusmsg", VAR_STRING), 0},
312 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
313 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
314 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
316 {VV_NAME("termresponse", VAR_STRING), VV_RO},
317 {VV_NAME("fname", VAR_STRING), VV_RO},
318 {VV_NAME("lang", VAR_STRING), VV_RO},
319 {VV_NAME("lc_time", VAR_STRING), VV_RO},
320 {VV_NAME("ctype", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
323 {VV_NAME("fname_in", VAR_STRING), VV_RO},
324 {VV_NAME("fname_out", VAR_STRING), VV_RO},
325 {VV_NAME("fname_new", VAR_STRING), VV_RO},
326 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
327 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
328 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
331 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("progname", VAR_STRING), VV_RO},
333 {VV_NAME("servername", VAR_STRING), VV_RO},
334 {VV_NAME("dying", VAR_NUMBER), VV_RO},
335 {VV_NAME("exception", VAR_STRING), VV_RO},
336 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
337 {VV_NAME("register", VAR_STRING), VV_RO},
338 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
339 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000340 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
341 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000342 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000343 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
344 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000345 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000350 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000351 {VV_NAME("swapname", VAR_STRING), VV_RO},
352 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000353 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000354 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000355 {VV_NAME("mouse_win", VAR_NUMBER), 0},
356 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
357 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000358 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000359 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000360 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
371/*
372 * The v: variables are stored in dictionary "vimvardict".
373 * "vimvars_var" is the variable that is used for the "l:" scope.
374 */
375static dict_T vimvardict;
376static dictitem_T vimvars_var;
377#define vimvarht vimvardict.dv_hashtab
378
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
380static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
381#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
382static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
383#endif
384static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
385static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
386static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
388static void list_glob_vars __ARGS((int *first));
389static void list_buf_vars __ARGS((int *first));
390static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_vim_vars __ARGS((int *first));
395static void list_script_vars __ARGS((int *first));
396static void list_func_vars __ARGS((int *first));
397static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000398static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
399static int check_changedtick __ARGS((char_u *arg));
400static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
401static void clear_lval __ARGS((lval_T *lp));
402static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
403static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
404static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
405static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
406static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
407static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
408static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
409static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
410static void item_lock __ARGS((typval_T *tv, int deep, int lock));
411static int tv_islocked __ARGS((typval_T *tv));
412
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
414static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000419static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
420static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000422static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000427static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static listitem_T *listitem_alloc __ARGS((void));
429static void listitem_free __ARGS((listitem_T *item));
430static void listitem_remove __ARGS((list_T *l, listitem_T *item));
431static long list_len __ARGS((list_T *l));
432static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
433static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
434static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000436static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000439static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
441static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
442static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *list2string __ARGS((typval_T *tv, int copyID));
446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000453static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
455static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000456static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
461static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
464static int string2float __ARGS((char_u *text, float_T *value));
465#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
467static int find_internal_func __ARGS((char_u *name));
468static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
469static 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 +0200470static 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 +0000471static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000472static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000473
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
475static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636#ifdef vim_mkdir
637static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100640#ifdef FEAT_MZSCHEME
641static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000645static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000646#ifdef FEAT_FLOAT
647static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000650static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000651static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000654static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
667static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000687static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200691static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000694static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000695static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#ifdef FEAT_FLOAT
699static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
701#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000702static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200703static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704#ifdef HAVE_STRFTIME
705static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
707static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200713static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200714static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000720static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200721static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000723static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000724static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000726static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000727static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000729static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200730#ifdef FEAT_FLOAT
731static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#ifdef FEAT_FLOAT
738static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200741static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200742static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000757static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000758static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static int get_env_len __ARGS((char_u **arg));
760static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000762static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
763#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
764#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
765 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000766static 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 +0000767static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000769static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
770static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static typval_T *alloc_tv __ARGS((void));
772static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void init_tv __ARGS((typval_T *varp));
774static long get_tv_number __ARGS((typval_T *varp));
775static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000776static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static char_u *get_tv_string __ARGS((typval_T *varp));
778static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000779static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000781static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
783static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
784static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000785static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
786static 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 +0000787static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
788static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000789static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
819static void func_unref __ARGS((char_u *name));
820static void func_ref __ARGS((char_u *name));
821static 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 +0000822static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
823static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
826static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000827static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000828static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200831
832#ifdef EBCDIC
833static int compare_func_name __ARGS((const void *s1, const void *s2));
834static void sortFunctions __ARGS(());
835#endif
836
837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838/* Character used as separated in autoload function/variable names. */
839#define AUTOLOAD_CHAR '#'
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
850 init_var_dict(&globvardict, &globvars_var);
851 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000852 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000853 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000854
855 for (i = 0; i < VV_LEN; ++i)
856 {
857 p = &vimvars[i];
858 STRCPY(p->vv_di.di_key, p->vv_name);
859 if (p->vv_flags & VV_RO)
860 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
861 else if (p->vv_flags & VV_RO_SBX)
862 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
863 else
864 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000865
866 /* add to v: scope dict, unless the value is not always available */
867 if (p->vv_type != VAR_UNKNOWN)
868 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000870 /* add to compat scope dict */
871 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000873 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874
875#ifdef EBCDIC
876 /*
877 * Sort the function table, to enable binary sort.
878 */
879 sortFunctions();
880#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000881}
882
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000883#if defined(EXITFREE) || defined(PROTO)
884 void
885eval_clear()
886{
887 int i;
888 struct vimvar *p;
889
890 for (i = 0; i < VV_LEN; ++i)
891 {
892 p = &vimvars[i];
893 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000894 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000895 vim_free(p->vv_str);
896 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000897 }
898 else if (p->vv_di.di_tv.v_type == VAR_LIST)
899 {
900 list_unref(p->vv_list);
901 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000903 }
904 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000905 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906 hash_clear(&compat_hashtab);
907
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909
910 /* global variables */
911 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000913 /* autoloaded script names */
914 ga_clear_strings(&ga_loaded);
915
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200916 /* script-local variables */
917 for (i = 1; i <= ga_scripts.ga_len; ++i)
918 {
919 vars_clear(&SCRIPT_VARS(i));
920 vim_free(SCRIPT_SV(i));
921 }
922 ga_clear(&ga_scripts);
923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000924 /* unreferenced lists and dicts */
925 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000926
927 /* functions */
928 free_all_functions();
929 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930}
931#endif
932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Return the name of the executed function.
935 */
936 char_u *
937func_name(cookie)
938 void *cookie;
939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942
943/*
944 * Return the address holding the next breakpoint line for a funccall cookie.
945 */
946 linenr_T *
947func_breakpoint(cookie)
948 void *cookie;
949{
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951}
952
953/*
954 * Return the address holding the debug tick for a funccall cookie.
955 */
956 int *
957func_dbg_tick(cookie)
958 void *cookie;
959{
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the nesting level for a funccall cookie.
965 */
966 int
967func_level(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000974funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000976/* pointer to list of previously used funccal, still around because some
977 * item in it is still being used. */
978funccall_T *previous_funccal = NULL;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
981 * Return TRUE when a function was ended by a ":return" command.
982 */
983 int
984current_func_returned()
985{
986 return current_funccal->returned;
987}
988
989
990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * Set an internal variable to a string value. Creates the variable if it does
992 * not already exist.
993 */
994 void
995set_internal_string_var(name, value)
996 char_u *name;
997 char_u *value;
998{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 val = vim_strsave(value);
1003 if (val != NULL)
1004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012}
1013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001014static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static char_u *redir_endp = NULL;
1017static char_u *redir_varname = NULL;
1018
1019/*
1020 * Start recording command output to a variable
1021 * Returns OK if successfully completed the setup. FAIL otherwise.
1022 */
1023 int
1024var_redir_start(name, append)
1025 char_u *name;
1026 int append; /* append to an existing variable */
1027{
1028 int save_emsg;
1029 int err;
1030 typval_T tv;
1031
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001032 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001033 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 {
1035 EMSG(_(e_invarg));
1036 return FAIL;
1037 }
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 redir_varname = vim_strsave(name);
1041 if (redir_varname == NULL)
1042 return FAIL;
1043
1044 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1045 if (redir_lval == NULL)
1046 {
1047 var_redir_stop();
1048 return FAIL;
1049 }
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 /* The output is stored in growarray "redir_ga" until redirection ends. */
1052 ga_init2(&redir_ga, (int)sizeof(char), 500);
1053
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1056 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1058 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001059 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp != NULL && *redir_endp != NUL)
1061 /* Trailing characters are present after the variable name */
1062 EMSG(_(e_trailing));
1063 else
1064 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001065 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 var_redir_stop();
1067 return FAIL;
1068 }
1069
1070 /* check if we can write to the variable: set it to or append an empty
1071 * string */
1072 save_emsg = did_emsg;
1073 did_emsg = FALSE;
1074 tv.v_type = VAR_STRING;
1075 tv.vval.v_string = (char_u *)"";
1076 if (append)
1077 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1078 else
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001080 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001082 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 if (err)
1084 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 var_redir_stop();
1087 return FAIL;
1088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089
1090 return OK;
1091}
1092
1093/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 * Append "value[value_len]" to the variable set by var_redir_start().
1095 * The actual appending is postponed until redirection ends, because the value
1096 * appended may in fact be the string we write to, changing it may cause freed
1097 * memory to be used:
1098 * :redir => foo
1099 * :let foo
1100 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 */
1102 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001107 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108
1109 if (redir_lval == NULL)
1110 return;
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 {
1119 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 }
1122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124}
1125
1126/*
1127 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
1131var_redir_stop()
1132{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 typval_T tv;
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_lval != NULL)
1136 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 /* If there was no error: assign the text to the variable. */
1138 if (redir_endp != NULL)
1139 {
1140 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 /* Call get_lval() again, if it's inside a Dict or List it may
1144 * have changed. */
1145 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1146 FALSE, FALSE, FALSE, FNE_CHECK_START);
1147 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1148 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1149 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* free the collected output */
1153 vim_free(redir_ga.ga_data);
1154 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 vim_free(redir_lval);
1157 redir_lval = NULL;
1158 }
1159 vim_free(redir_varname);
1160 redir_varname = NULL;
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163# if defined(FEAT_MBYTE) || defined(PROTO)
1164 int
1165eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1166 char_u *enc_from;
1167 char_u *enc_to;
1168 char_u *fname_from;
1169 char_u *fname_to;
1170{
1171 int err = FALSE;
1172
1173 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1174 set_vim_var_string(VV_CC_TO, enc_to, -1);
1175 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1176 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1177 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1178 err = TRUE;
1179 set_vim_var_string(VV_CC_FROM, NULL, -1);
1180 set_vim_var_string(VV_CC_TO, NULL, -1);
1181 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1182 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1183
1184 if (err)
1185 return FAIL;
1186 return OK;
1187}
1188# endif
1189
1190# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1191 int
1192eval_printexpr(fname, args)
1193 char_u *fname;
1194 char_u *args;
1195{
1196 int err = FALSE;
1197
1198 set_vim_var_string(VV_FNAME_IN, fname, -1);
1199 set_vim_var_string(VV_CMDARG, args, -1);
1200 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1201 err = TRUE;
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_CMDARG, NULL, -1);
1204
1205 if (err)
1206 {
1207 mch_remove(fname);
1208 return FAIL;
1209 }
1210 return OK;
1211}
1212# endif
1213
1214# if defined(FEAT_DIFF) || defined(PROTO)
1215 void
1216eval_diff(origfile, newfile, outfile)
1217 char_u *origfile;
1218 char_u *newfile;
1219 char_u *outfile;
1220{
1221 int err = FALSE;
1222
1223 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1224 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1225 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1226 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1229 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1230}
1231
1232 void
1233eval_patch(origfile, difffile, outfile)
1234 char_u *origfile;
1235 char_u *difffile;
1236 char_u *outfile;
1237{
1238 int err;
1239
1240 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1241 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1242 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1243 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1244 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1246 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1247}
1248# endif
1249
1250/*
1251 * Top level evaluation function, returning a boolean.
1252 * Sets "error" to TRUE if there was an error.
1253 * Return TRUE or FALSE.
1254 */
1255 int
1256eval_to_bool(arg, error, nextcmd, skip)
1257 char_u *arg;
1258 int *error;
1259 char_u **nextcmd;
1260 int skip; /* only parse, don't execute */
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval = FALSE;
1264
1265 if (skip)
1266 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 else
1270 {
1271 *error = FALSE;
1272 if (!skip)
1273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001275 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 }
1278 if (skip)
1279 --emsg_skip;
1280
1281 return retval;
1282}
1283
1284/*
1285 * Top level evaluation function, returning a string. If "skip" is TRUE,
1286 * only parsing to "nextcmd" is done, without reporting errors. Return
1287 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1288 */
1289 char_u *
1290eval_to_string_skip(arg, nextcmd, skip)
1291 char_u *arg;
1292 char_u **nextcmd;
1293 int skip; /* only parse, don't execute */
1294{
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 char_u *retval;
1297
1298 if (skip)
1299 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 retval = NULL;
1302 else
1303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 retval = vim_strsave(get_tv_string(&tv));
1305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 if (skip)
1308 --emsg_skip;
1309
1310 return retval;
1311}
1312
1313/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001314 * Skip over an expression at "*pp".
1315 * Return FAIL for an error, OK otherwise.
1316 */
1317 int
1318skip_expr(pp)
1319 char_u **pp;
1320{
Bram Moolenaar33570922005-01-25 22:26:29 +00001321 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322
1323 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325}
1326
1327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329 * When "convert" is TRUE convert a List into a sequence of lines and convert
1330 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Return pointer to allocated memory, or NULL for failure.
1332 */
1333 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *arg;
1336 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001342#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 retval = NULL;
1348 else
1349 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001351 {
1352 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001353 if (tv.vval.v_list != NULL)
1354 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001355 ga_append(&ga, NUL);
1356 retval = (char_u *)ga.ga_data;
1357 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358#ifdef FEAT_FLOAT
1359 else if (convert && tv.v_type == VAR_FLOAT)
1360 {
1361 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1362 retval = vim_strsave(numbuf);
1363 }
1364#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 else
1366 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369
1370 return retval;
1371}
1372
1373/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001374 * Call eval_to_string() without using current local variables and using
1375 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 */
1377 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 char_u *arg;
1380 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 char_u *retval;
1384 void *save_funccalp;
1385
1386 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 if (use_sandbox)
1388 ++sandbox;
1389 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 --sandbox;
1393 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 restore_funccal(save_funccalp);
1395 return retval;
1396}
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * Top level evaluation function, returning a number.
1400 * Evaluates "expr" silently.
1401 * Returns -1 for an error.
1402 */
1403 int
1404eval_to_number(expr)
1405 char_u *expr;
1406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001409 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 ++emsg_off;
1412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 retval = -1;
1415 else
1416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001417 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 --emsg_off;
1421
1422 return retval;
1423}
1424
Bram Moolenaara40058a2005-07-11 22:42:07 +00001425/*
1426 * Prepare v: variable "idx" to be used.
1427 * Save the current typeval in "save_tv".
1428 * When not used yet add the variable to the v: hashtable.
1429 */
1430 static void
1431prepare_vimvar(idx, save_tv)
1432 int idx;
1433 typval_T *save_tv;
1434{
1435 *save_tv = vimvars[idx].vv_tv;
1436 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1437 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1438}
1439
1440/*
1441 * Restore v: variable "idx" to typeval "save_tv".
1442 * When no longer defined, remove the variable from the v: hashtable.
1443 */
1444 static void
1445restore_vimvar(idx, save_tv)
1446 int idx;
1447 typval_T *save_tv;
1448{
1449 hashitem_T *hi;
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451 vimvars[idx].vv_tv = *save_tv;
1452 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1453 {
1454 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1455 if (HASHITEM_EMPTY(hi))
1456 EMSG2(_(e_intern2), "restore_vimvar()");
1457 else
1458 hash_remove(&vimvarht, hi);
1459 }
1460}
1461
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001462#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001463/*
1464 * Evaluate an expression to a list with suggestions.
1465 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001466 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467 */
1468 list_T *
1469eval_spell_expr(badword, expr)
1470 char_u *badword;
1471 char_u *expr;
1472{
1473 typval_T save_val;
1474 typval_T rettv;
1475 list_T *list = NULL;
1476 char_u *p = skipwhite(expr);
1477
1478 /* Set "v:val" to the bad word. */
1479 prepare_vimvar(VV_VAL, &save_val);
1480 vimvars[VV_VAL].vv_type = VAR_STRING;
1481 vimvars[VV_VAL].vv_str = badword;
1482 if (p_verbose == 0)
1483 ++emsg_off;
1484
1485 if (eval1(&p, &rettv, TRUE) == OK)
1486 {
1487 if (rettv.v_type != VAR_LIST)
1488 clear_tv(&rettv);
1489 else
1490 list = rettv.vval.v_list;
1491 }
1492
1493 if (p_verbose == 0)
1494 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001495 restore_vimvar(VV_VAL, &save_val);
1496
1497 return list;
1498}
1499
1500/*
1501 * "list" is supposed to contain two items: a word and a number. Return the
1502 * word in "pp" and the number as the return value.
1503 * Return -1 if anything isn't right.
1504 * Used to get the good word and score from the eval_spell_expr() result.
1505 */
1506 int
1507get_spellword(list, pp)
1508 list_T *list;
1509 char_u **pp;
1510{
1511 listitem_T *li;
1512
1513 li = list->lv_first;
1514 if (li == NULL)
1515 return -1;
1516 *pp = get_tv_string(&li->li_tv);
1517
1518 li = li->li_next;
1519 if (li == NULL)
1520 return -1;
1521 return get_tv_number(&li->li_tv);
1522}
1523#endif
1524
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001525/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001526 * Top level evaluation function.
1527 * Returns an allocated typval_T with the result.
1528 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529 */
1530 typval_T *
1531eval_expr(arg, nextcmd)
1532 char_u *arg;
1533 char_u **nextcmd;
1534{
1535 typval_T *tv;
1536
1537 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001538 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 {
1540 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001541 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 }
1543
1544 return tv;
1545}
1546
1547
Bram Moolenaar4f688582007-07-24 12:34:30 +00001548#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1549 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001551 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001552 * Uses argv[argc] for the function arguments. Only Number and String
1553 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 static int
1557call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 char_u *func;
1559 int argc;
1560 char_u **argv;
1561 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 long n;
1566 int len;
1567 int i;
1568 int doesrange;
1569 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001572 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 for (i = 0; i < argc; i++)
1577 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 /* Pass a NULL or empty argument as an empty string */
1579 if (argv[i] == NULL || *argv[i] == NUL)
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_STRING;
1582 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 continue;
1584 }
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 /* Recognize a number argument, the others must be strings. */
1587 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1588 if (len != 0 && len == (int)STRLEN(argv[i]))
1589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001590 argvars[i].v_type = VAR_NUMBER;
1591 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 else
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_STRING;
1596 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
1599
1600 if (safe)
1601 {
1602 save_funccalp = save_funccal();
1603 ++sandbox;
1604 }
1605
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1607 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (safe)
1611 {
1612 --sandbox;
1613 restore_funccal(save_funccalp);
1614 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 vim_free(argvars);
1616
1617 if (ret == FAIL)
1618 clear_tv(rettv);
1619
1620 return ret;
1621}
1622
Bram Moolenaar4f688582007-07-24 12:34:30 +00001623# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001625 * Call vimL function "func" and return the result as a string.
1626 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 * Uses argv[argc] for the function arguments.
1628 */
1629 void *
1630call_func_retstr(func, argc, argv, safe)
1631 char_u *func;
1632 int argc;
1633 char_u **argv;
1634 int safe; /* use the sandbox */
1635{
1636 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001637 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638
1639 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1640 return NULL;
1641
1642 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 return retval;
1645}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001646# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647
Bram Moolenaar4f688582007-07-24 12:34:30 +00001648# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001650 * Call vimL function "func" and return the result as a number.
1651 * Returns -1 when calling the function fails.
1652 * Uses argv[argc] for the function arguments.
1653 */
1654 long
1655call_func_retnr(func, argc, argv, safe)
1656 char_u *func;
1657 int argc;
1658 char_u **argv;
1659 int safe; /* use the sandbox */
1660{
1661 typval_T rettv;
1662 long retval;
1663
1664 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1665 return -1;
1666
1667 retval = get_tv_number_chk(&rettv, NULL);
1668 clear_tv(&rettv);
1669 return retval;
1670}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001672
1673/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001674 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001676 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 */
1678 void *
1679call_func_retlist(func, argc, argv, safe)
1680 char_u *func;
1681 int argc;
1682 char_u **argv;
1683 int safe; /* use the sandbox */
1684{
1685 typval_T rettv;
1686
1687 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1688 return NULL;
1689
1690 if (rettv.v_type != VAR_LIST)
1691 {
1692 clear_tv(&rettv);
1693 return NULL;
1694 }
1695
1696 return rettv.vval.v_list;
1697}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
1699
Bram Moolenaar4f688582007-07-24 12:34:30 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Save the current function call pointer, and set it to NULL.
1703 * Used when executing autocommands and for ":source".
1704 */
1705 void *
1706save_funccal()
1707{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001708 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 current_funccal = NULL;
1711 return (void *)fc;
1712}
1713
1714 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001715restore_funccal(vfc)
1716 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = (funccall_T *)vfc;
1719
1720 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721}
1722
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723#if defined(FEAT_PROFILE) || defined(PROTO)
1724/*
1725 * Prepare profiling for entering a child or something else that is not
1726 * counted for the script/function itself.
1727 * Should always be called in pair with prof_child_exit().
1728 */
1729 void
1730prof_child_enter(tm)
1731 proftime_T *tm; /* place to store waittime */
1732{
1733 funccall_T *fc = current_funccal;
1734
1735 if (fc != NULL && fc->func->uf_profiling)
1736 profile_start(&fc->prof_child);
1737 script_prof_save(tm);
1738}
1739
1740/*
1741 * Take care of time spent in a child.
1742 * Should always be called after prof_child_enter().
1743 */
1744 void
1745prof_child_exit(tm)
1746 proftime_T *tm; /* where waittime was stored */
1747{
1748 funccall_T *fc = current_funccal;
1749
1750 if (fc != NULL && fc->func->uf_profiling)
1751 {
1752 profile_end(&fc->prof_child);
1753 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1754 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1755 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1756 }
1757 script_prof_restore(tm);
1758}
1759#endif
1760
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762#ifdef FEAT_FOLDING
1763/*
1764 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1765 * it in "*cp". Doesn't give error messages.
1766 */
1767 int
1768eval_foldexpr(arg, cp)
1769 char_u *arg;
1770 int *cp;
1771{
Bram Moolenaar33570922005-01-25 22:26:29 +00001772 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 int retval;
1774 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001775 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1776 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001779 if (use_sandbox)
1780 ++sandbox;
1781 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 retval = 0;
1785 else
1786 {
1787 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (tv.v_type == VAR_NUMBER)
1789 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001790 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 retval = 0;
1792 else
1793 {
1794 /* If the result is a string, check if there is a non-digit before
1795 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (!VIM_ISDIGIT(*s) && *s != '-')
1798 *cp = *s++;
1799 retval = atol((char *)s);
1800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001804 if (use_sandbox)
1805 --sandbox;
1806 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 return retval;
1809}
1810#endif
1811
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 * ":let" list all variable values
1814 * ":let var1 var2" list variable values
1815 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001816 * ":let var += expr" assignment command.
1817 * ":let var -= expr" assignment command.
1818 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 void
1822ex_let(eap)
1823 exarg_T *eap;
1824{
1825 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 int var_count = 0;
1830 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001832 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001833 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaardb552d602006-03-23 22:59:57 +00001835 argend = skip_var_list(arg, &var_count, &semicolon);
1836 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001838 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1839 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001843 /*
1844 * ":let" without "=": list variables
1845 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (*arg == '[')
1847 EMSG(_(e_invarg));
1848 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001850 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 list_glob_vars(&first);
1855 list_buf_vars(&first);
1856 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001857#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001859#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_script_vars(&first);
1861 list_func_vars(&first);
1862 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 eap->nextcmd = check_nextcmd(arg);
1865 }
1866 else
1867 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 op[0] = '=';
1869 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001870 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 {
1872 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1873 op[0] = expr[-1]; /* +=, -= or .= */
1874 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (eap->skip)
1878 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (eap->skip)
1881 {
1882 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 --emsg_skip;
1885 }
1886 else if (i != FAIL)
1887 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892 }
1893}
1894
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895/*
1896 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1897 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 * When "nextchars" is not NULL it points to a string with characters that
1899 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1900 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 * Returns OK or FAIL;
1902 */
1903 static int
1904ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1905 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 int copy; /* copy values from "tv", don't move */
1908 int semicolon; /* from skip_var_list() */
1909 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911{
1912 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 listitem_T *item;
1916 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917
1918 if (*arg != '[')
1919 {
1920 /*
1921 * ":let var = expr" or ":for var in list"
1922 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 return FAIL;
1925 return OK;
1926 }
1927
1928 /*
1929 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1930 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001931 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 {
1933 EMSG(_(e_listreq));
1934 return FAIL;
1935 }
1936
1937 i = list_len(l);
1938 if (semicolon == 0 && var_count < i)
1939 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001940 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return FAIL;
1942 }
1943 if (var_count - semicolon > i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948
1949 item = l->lv_first;
1950 while (*arg != ']')
1951 {
1952 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 item = item->li_next;
1955 if (arg == NULL)
1956 return FAIL;
1957
1958 arg = skipwhite(arg);
1959 if (*arg == ';')
1960 {
1961 /* Put the rest of the list (may be empty) in the var after ';'.
1962 * Create a new list for this. */
1963 l = list_alloc();
1964 if (l == NULL)
1965 return FAIL;
1966 while (item != NULL)
1967 {
1968 list_append_tv(l, &item->li_tv);
1969 item = item->li_next;
1970 }
1971
1972 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001973 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 ltv.vval.v_list = l;
1975 l->lv_refcount = 1;
1976
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1978 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 clear_tv(&ltv);
1980 if (arg == NULL)
1981 return FAIL;
1982 break;
1983 }
1984 else if (*arg != ',' && *arg != ']')
1985 {
1986 EMSG2(_(e_intern2), "ex_let_vars()");
1987 return FAIL;
1988 }
1989 }
1990
1991 return OK;
1992}
1993
1994/*
1995 * Skip over assignable variable "var" or list of variables "[var, var]".
1996 * Used for ":let varvar = expr" and ":for varvar in expr".
1997 * For "[var, var]" increment "*var_count" for each variable.
1998 * for "[var, var; var]" set "semicolon".
1999 * Return NULL for an error.
2000 */
2001 static char_u *
2002skip_var_list(arg, var_count, semicolon)
2003 char_u *arg;
2004 int *var_count;
2005 int *semicolon;
2006{
2007 char_u *p, *s;
2008
2009 if (*arg == '[')
2010 {
2011 /* "[var, var]": find the matching ']'. */
2012 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002013 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 {
2015 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2016 s = skip_var_one(p);
2017 if (s == p)
2018 {
2019 EMSG2(_(e_invarg2), p);
2020 return NULL;
2021 }
2022 ++*var_count;
2023
2024 p = skipwhite(s);
2025 if (*p == ']')
2026 break;
2027 else if (*p == ';')
2028 {
2029 if (*semicolon == 1)
2030 {
2031 EMSG(_("Double ; in list of variables"));
2032 return NULL;
2033 }
2034 *semicolon = 1;
2035 }
2036 else if (*p != ',')
2037 {
2038 EMSG2(_(e_invarg2), p);
2039 return NULL;
2040 }
2041 }
2042 return p + 1;
2043 }
2044 else
2045 return skip_var_one(arg);
2046}
2047
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002048/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002049 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002050 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002051 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 static char_u *
2053skip_var_one(arg)
2054 char_u *arg;
2055{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 if (*arg == '@' && arg[1] != NUL)
2057 return arg + 2;
2058 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2059 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002060}
2061
Bram Moolenaara7043832005-01-21 11:56:39 +00002062/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002063 * List variables for hashtab "ht" with prefix "prefix".
2064 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002067list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002072{
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashitem_T *hi;
2074 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 int todo;
2076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2079 {
2080 if (!HASHITEM_EMPTY(hi))
2081 {
2082 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 di = HI2DI(hi);
2084 if (empty || di->di_tv.v_type != VAR_STRING
2085 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
2088 }
2089}
2090
2091/*
2092 * List global variables.
2093 */
2094 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095list_glob_vars(first)
2096 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002097{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002099}
2100
2101/*
2102 * List buffer variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_buf_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108 char_u numbuf[NUMBUFLEN];
2109
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2111 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112
2113 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2115 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002116}
2117
2118/*
2119 * List window variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_win_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2126 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002129#ifdef FEAT_WINDOWS
2130/*
2131 * List tab page variables.
2132 */
2133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134list_tab_vars(first)
2135 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002136{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2138 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139}
2140#endif
2141
Bram Moolenaara7043832005-01-21 11:56:39 +00002142/*
2143 * List Vim variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_vim_vars(first)
2147 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150}
2151
2152/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002153 * List script-local variables, if there is a script.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_script_vars(first)
2157 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158{
2159 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2161 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162}
2163
2164/*
2165 * List function variables, if there is a function.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_func_vars(first)
2169 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170{
2171 if (current_funccal != NULL)
2172 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174}
2175
2176/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 * List variables in "arg".
2178 */
2179 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 exarg_T *eap;
2182 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184{
2185 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 char_u *name_start;
2189 char_u *arg_subsc;
2190 char_u *tofree;
2191 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192
2193 while (!ends_excmd(*arg) && !got_int)
2194 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002197 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2199 {
2200 emsg_severe = TRUE;
2201 EMSG(_(e_trailing));
2202 break;
2203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 /* get_name_len() takes care of expanding curly braces */
2208 name_start = name = arg;
2209 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2210 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* This is mainly to keep test 49 working: when expanding
2213 * curly braces fails overrule the exception error message. */
2214 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 emsg_severe = TRUE;
2217 EMSG2(_(e_invarg2), arg);
2218 break;
2219 }
2220 error = TRUE;
2221 }
2222 else
2223 {
2224 if (tofree != NULL)
2225 name = tofree;
2226 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 else
2229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* handle d.key, l[idx], f(expr) */
2231 arg_subsc = arg;
2232 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 case 'g': list_glob_vars(first); break;
2241 case 'b': list_buf_vars(first); break;
2242 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'v': list_vim_vars(first); break;
2247 case 's': list_script_vars(first); break;
2248 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 default:
2250 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002252 }
2253 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 {
2255 char_u numbuf[NUMBUFLEN];
2256 char_u *tf;
2257 int c;
2258 char_u *s;
2259
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002260 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 c = *arg;
2262 *arg = NUL;
2263 list_one_var_a((char_u *)"",
2264 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002265 tv.v_type,
2266 s == NULL ? (char_u *)"" : s,
2267 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 *arg = c;
2269 vim_free(tf);
2270 }
2271 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 }
2274 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275
2276 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278
2279 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 }
2281
2282 return arg;
2283}
2284
2285/*
2286 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2287 * Returns a pointer to the char just after the var name.
2288 * Returns NULL if there is an error.
2289 */
2290 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002295 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297{
2298 int c1;
2299 char_u *name;
2300 char_u *p;
2301 char_u *arg_end = NULL;
2302 int len;
2303 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305
2306 /*
2307 * ":let $VAR = expr": Set environment variable.
2308 */
2309 if (*arg == '$')
2310 {
2311 /* Find the end of the name. */
2312 ++arg;
2313 name = arg;
2314 len = get_env_len(&arg);
2315 if (len == 0)
2316 EMSG2(_(e_invarg2), name - 1);
2317 else
2318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 if (op != NULL && (*op == '+' || *op == '-'))
2320 EMSG2(_(e_letwrong), op);
2321 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 EMSG(_(e_letunexp));
2324 else
2325 {
2326 c1 = name[len];
2327 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002328 p = get_tv_string_chk(tv);
2329 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 {
2331 int mustfree = FALSE;
2332 char_u *s = vim_getenv(name, &mustfree);
2333
2334 if (s != NULL)
2335 {
2336 p = tofree = concat_str(s, p);
2337 if (mustfree)
2338 vim_free(s);
2339 }
2340 }
2341 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 if (STRICMP(name, "HOME") == 0)
2345 init_homedir();
2346 else if (didset_vim && STRICMP(name, "VIM") == 0)
2347 didset_vim = FALSE;
2348 else if (didset_vimruntime
2349 && STRICMP(name, "VIMRUNTIME") == 0)
2350 didset_vimruntime = FALSE;
2351 arg_end = arg;
2352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 }
2356 }
2357 }
2358
2359 /*
2360 * ":let &option = expr": Set option value.
2361 * ":let &l:option = expr": Set local option value.
2362 * ":let &g:option = expr": Set global option value.
2363 */
2364 else if (*arg == '&')
2365 {
2366 /* Find the end of the name. */
2367 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 if (p == NULL || (endchars != NULL
2369 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 EMSG(_(e_letunexp));
2371 else
2372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 long n;
2374 int opt_type;
2375 long numval;
2376 char_u *stringval = NULL;
2377 char_u *s;
2378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 c1 = *p;
2380 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381
2382 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002383 s = get_tv_string_chk(tv); /* != NULL if number or string */
2384 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 {
2386 opt_type = get_option_value(arg, &numval,
2387 &stringval, opt_flags);
2388 if ((opt_type == 1 && *op == '.')
2389 || (opt_type == 0 && *op != '.'))
2390 EMSG2(_(e_letwrong), op);
2391 else
2392 {
2393 if (opt_type == 1) /* number */
2394 {
2395 if (*op == '+')
2396 n = numval + n;
2397 else
2398 n = numval - n;
2399 }
2400 else if (opt_type == 0 && stringval != NULL) /* string */
2401 {
2402 s = concat_str(stringval, s);
2403 vim_free(stringval);
2404 stringval = s;
2405 }
2406 }
2407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (s != NULL)
2409 {
2410 set_option_value(arg, n, s, opt_flags);
2411 arg_end = p;
2412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 }
2416 }
2417
2418 /*
2419 * ":let @r = expr": Set register contents.
2420 */
2421 else if (*arg == '@')
2422 {
2423 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 if (op != NULL && (*op == '+' || *op == '-'))
2425 EMSG2(_(e_letwrong), op);
2426 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002431 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 char_u *s;
2433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002434 p = get_tv_string_chk(tv);
2435 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002437 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 if (s != NULL)
2439 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002440 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(s);
2442 }
2443 }
2444 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 arg_end = arg + 1;
2448 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 }
2451 }
2452
2453 /*
2454 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2465 EMSG(_(e_letunexp));
2466 else
2467 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474
2475 else
2476 EMSG2(_(e_invarg2), arg);
2477
2478 return arg_end;
2479}
2480
2481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2483 */
2484 static int
2485check_changedtick(arg)
2486 char_u *arg;
2487{
2488 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2489 {
2490 EMSG2(_(e_readonlyvar), arg);
2491 return TRUE;
2492 }
2493 return FALSE;
2494}
2495
2496/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * Get an lval: variable, Dict item or List item that can be assigned a value
2498 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2499 * "name.key", "name.key[expr]" etc.
2500 * Indexing only works if "name" is an existing List or Dictionary.
2501 * "name" points to the start of the name.
2502 * If "rettv" is not NULL it points to the value to be assigned.
2503 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2504 * wrong; must end in space or cmd separator.
2505 *
2506 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002507 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 */
2510 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002511get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 typval_T *rettv;
2514 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 int unlet;
2516 int skip;
2517 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 char_u *expr_start, *expr_end;
2522 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 dictitem_T *v;
2524 typval_T var1;
2525 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534
2535 if (skip)
2536 {
2537 /* When skipping just find the end of the name. */
2538 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002539 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 }
2541
2542 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002543 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (expr_start != NULL)
2545 {
2546 /* Don't expand the name when we already know there is an error. */
2547 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2548 && *p != '[' && *p != '.')
2549 {
2550 EMSG(_(e_trailing));
2551 return NULL;
2552 }
2553
2554 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2555 if (lp->ll_exp_name == NULL)
2556 {
2557 /* Report an invalid expression in braces, unless the
2558 * expression evaluation has been cancelled due to an
2559 * aborting error, an interrupt, or an exception. */
2560 if (!aborting() && !quiet)
2561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002562 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 EMSG2(_(e_invarg2), name);
2564 return NULL;
2565 }
2566 }
2567 lp->ll_name = lp->ll_exp_name;
2568 }
2569 else
2570 lp->ll_name = name;
2571
2572 /* Without [idx] or .key we are done. */
2573 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2574 return p;
2575
2576 cc = *p;
2577 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002578 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (v == NULL && !quiet)
2580 EMSG2(_(e_undefvar), lp->ll_name);
2581 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 if (v == NULL)
2583 return NULL;
2584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 /*
2586 * Loop until no more [idx] or .key is following.
2587 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2592 && !(lp->ll_tv->v_type == VAR_DICT
2593 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (!quiet)
2596 EMSG(_("E689: Can only index a List or Dictionary"));
2597 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E708: [:] must come last"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 len = -1;
2607 if (*p == '.')
2608 {
2609 key = p + 1;
2610 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2611 ;
2612 if (len == 0)
2613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!quiet)
2615 EMSG(_(e_emptykey));
2616 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 }
2618 p = key + len;
2619 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 else
2621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 if (*p == ':')
2625 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 empty1 = FALSE;
2629 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002631 if (get_tv_string_chk(&var1) == NULL)
2632 {
2633 /* not a number or string */
2634 clear_tv(&var1);
2635 return NULL;
2636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638
2639 /* Optionally get the second index [ :expr]. */
2640 if (*p == ':')
2641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002645 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 if (!empty1)
2647 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (rettv != NULL && (rettv->v_type != VAR_LIST
2651 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 if (!empty1)
2656 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659 p = skipwhite(p + 1);
2660 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 else
2663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (!empty1)
2668 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002671 if (get_tv_string_chk(&var2) == NULL)
2672 {
2673 /* not a number or string */
2674 if (!empty1)
2675 clear_tv(&var1);
2676 clear_tv(&var2);
2677 return NULL;
2678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (!quiet)
2688 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (!empty1)
2690 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
2695
2696 /* Skip to past ']'. */
2697 ++p;
2698 }
2699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
2702 if (len == -1)
2703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002705 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (*key == NUL)
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
2713 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 lp->ll_list = NULL;
2715 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002716 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002719 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (len == -1)
2725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
2728 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (len == -1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 p = NULL;
2736 break;
2737 }
2738 if (len == -1)
2739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742 else
2743 {
2744 /*
2745 * Get the number and item for the only or first index of the List.
2746 */
2747 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 else
2750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var1);
2753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002754 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_list = lp->ll_tv->vval.v_list;
2756 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2757 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002759 if (lp->ll_n1 < 0)
2760 {
2761 lp->ll_n1 = 0;
2762 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2763 }
2764 }
2765 if (lp->ll_li == NULL)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771
2772 /*
2773 * May need to find the item or absolute index for the second
2774 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 * When no index given: "lp->ll_empty2" is TRUE.
2776 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 }
2789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2791 if (lp->ll_n1 < 0)
2792 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2793 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002795 }
2796
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 return p;
2802}
2803
2804/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 */
2807 static void
2808clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810{
2811 vim_free(lp->ll_exp_name);
2812 vim_free(lp->ll_newkey);
2813}
2814
2815/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002816 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 */
2820 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002826 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827{
2828 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 listitem_T *ri;
2830 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831
2832 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 cc = *endp;
2837 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 if (op != NULL && *op != '=')
2839 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002840 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002841
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002842 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002843 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002844 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 {
2846 if (tv_op(&tv, rettv, op) == OK)
2847 set_var(lp->ll_name, &tv, FALSE);
2848 clear_tv(&tv);
2849 }
2850 }
2851 else
2852 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002856 else if (tv_check_lock(lp->ll_newkey == NULL
2857 ? lp->ll_tv->v_lock
2858 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2859 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 else if (lp->ll_range)
2861 {
2862 /*
2863 * Assign the List values to the list items.
2864 */
2865 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002866 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 if (op != NULL && *op != '=')
2868 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2869 else
2870 {
2871 clear_tv(&lp->ll_li->li_tv);
2872 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2873 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ri = ri->li_next;
2875 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2876 break;
2877 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002880 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 ri = NULL;
2883 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002885 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_li = lp->ll_li->li_next;
2887 ++lp->ll_n1;
2888 }
2889 if (ri != NULL)
2890 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 else if (lp->ll_empty2
2892 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 : lp->ll_n1 != lp->ll_n2)
2894 EMSG(_("E711: List value has not enough items"));
2895 }
2896 else
2897 {
2898 /*
2899 * Assign to a List or Dictionary item.
2900 */
2901 if (lp->ll_newkey != NULL)
2902 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 if (op != NULL && *op != '=')
2904 {
2905 EMSG2(_(e_letwrong), op);
2906 return;
2907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002910 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 if (di == NULL)
2912 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002913 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2914 {
2915 vim_free(di);
2916 return;
2917 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 else if (op != NULL && *op != '=')
2921 {
2922 tv_op(lp->ll_tv, rettv, op);
2923 return;
2924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002925 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 /*
2929 * Assign the value to the variable or list item.
2930 */
2931 if (copy)
2932 copy_tv(rettv, lp->ll_tv);
2933 else
2934 {
2935 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002936 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 }
2939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940}
2941
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2944 * Returns OK or FAIL.
2945 */
2946 static int
2947tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 typval_T *tv1;
2949 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 char_u *op;
2951{
2952 long n;
2953 char_u numbuf[NUMBUFLEN];
2954 char_u *s;
2955
2956 /* Can't do anything with a Funcref or a Dict on the right. */
2957 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2958 {
2959 switch (tv1->v_type)
2960 {
2961 case VAR_DICT:
2962 case VAR_FUNC:
2963 break;
2964
2965 case VAR_LIST:
2966 if (*op != '+' || tv2->v_type != VAR_LIST)
2967 break;
2968 /* List += List */
2969 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2970 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2971 return OK;
2972
2973 case VAR_NUMBER:
2974 case VAR_STRING:
2975 if (tv2->v_type == VAR_LIST)
2976 break;
2977 if (*op == '+' || *op == '-')
2978 {
2979 /* nr += nr or nr -= nr*/
2980 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981#ifdef FEAT_FLOAT
2982 if (tv2->v_type == VAR_FLOAT)
2983 {
2984 float_T f = n;
2985
2986 if (*op == '+')
2987 f += tv2->vval.v_float;
2988 else
2989 f -= tv2->vval.v_float;
2990 clear_tv(tv1);
2991 tv1->v_type = VAR_FLOAT;
2992 tv1->vval.v_float = f;
2993 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002995#endif
2996 {
2997 if (*op == '+')
2998 n += get_tv_number(tv2);
2999 else
3000 n -= get_tv_number(tv2);
3001 clear_tv(tv1);
3002 tv1->v_type = VAR_NUMBER;
3003 tv1->vval.v_number = n;
3004 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 }
3006 else
3007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003008 if (tv2->v_type == VAR_FLOAT)
3009 break;
3010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 /* str .= str */
3012 s = get_tv_string(tv1);
3013 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3014 clear_tv(tv1);
3015 tv1->v_type = VAR_STRING;
3016 tv1->vval.v_string = s;
3017 }
3018 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019
3020#ifdef FEAT_FLOAT
3021 case VAR_FLOAT:
3022 {
3023 float_T f;
3024
3025 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3026 && tv2->v_type != VAR_NUMBER
3027 && tv2->v_type != VAR_STRING))
3028 break;
3029 if (tv2->v_type == VAR_FLOAT)
3030 f = tv2->vval.v_float;
3031 else
3032 f = get_tv_number(tv2);
3033 if (*op == '+')
3034 tv1->vval.v_float += f;
3035 else
3036 tv1->vval.v_float -= f;
3037 }
3038 return OK;
3039#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 }
3041 }
3042
3043 EMSG2(_(e_letwrong), op);
3044 return FAIL;
3045}
3046
3047/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 * Add a watcher to a list.
3049 */
3050 static void
3051list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003052 list_T *l;
3053 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054{
3055 lw->lw_next = l->lv_watch;
3056 l->lv_watch = lw;
3057}
3058
3059/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061 * No warning when it isn't found...
3062 */
3063 static void
3064list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003065 list_T *l;
3066 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003067{
Bram Moolenaar33570922005-01-25 22:26:29 +00003068 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069
3070 lwp = &l->lv_watch;
3071 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3072 {
3073 if (lw == lwrem)
3074 {
3075 *lwp = lw->lw_next;
3076 break;
3077 }
3078 lwp = &lw->lw_next;
3079 }
3080}
3081
3082/*
3083 * Just before removing an item from a list: advance watchers to the next
3084 * item.
3085 */
3086 static void
3087list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 list_T *l;
3089 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090{
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092
3093 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3094 if (lw->lw_item == item)
3095 lw->lw_item = item->li_next;
3096}
3097
3098/*
3099 * Evaluate the expression used in a ":for var in expr" command.
3100 * "arg" points to "var".
3101 * Set "*errp" to TRUE for an error, FALSE otherwise;
3102 * Return a pointer that holds the info. Null when there is an error.
3103 */
3104 void *
3105eval_for_line(arg, errp, nextcmdp, skip)
3106 char_u *arg;
3107 int *errp;
3108 char_u **nextcmdp;
3109 int skip;
3110{
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 typval_T tv;
3114 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115
3116 *errp = TRUE; /* default: there is an error */
3117
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 if (fi == NULL)
3120 return NULL;
3121
3122 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3123 if (expr == NULL)
3124 return fi;
3125
3126 expr = skipwhite(expr);
3127 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3128 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003129 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 return fi;
3131 }
3132
3133 if (skip)
3134 ++emsg_skip;
3135 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3136 {
3137 *errp = FALSE;
3138 if (!skip)
3139 {
3140 l = tv.vval.v_list;
3141 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003142 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003144 clear_tv(&tv);
3145 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 else
3147 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003148 /* No need to increment the refcount, it's already set for the
3149 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 fi->fi_list = l;
3151 list_add_watch(l, &fi->fi_lw);
3152 fi->fi_lw.lw_item = l->lv_first;
3153 }
3154 }
3155 }
3156 if (skip)
3157 --emsg_skip;
3158
3159 return fi;
3160}
3161
3162/*
3163 * Use the first item in a ":for" list. Advance to the next.
3164 * Assign the values to the variable (list). "arg" points to the first one.
3165 * Return TRUE when a valid item was found, FALSE when at end of list or
3166 * something wrong.
3167 */
3168 int
3169next_for_item(fi_void, arg)
3170 void *fi_void;
3171 char_u *arg;
3172{
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176
3177 item = fi->fi_lw.lw_item;
3178 if (item == NULL)
3179 result = FALSE;
3180 else
3181 {
3182 fi->fi_lw.lw_item = item->li_next;
3183 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3184 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3185 }
3186 return result;
3187}
3188
3189/*
3190 * Free the structure used to store info used by ":for".
3191 */
3192 void
3193free_for_info(fi_void)
3194 void *fi_void;
3195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003198 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 list_unref(fi->fi_list);
3202 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203 vim_free(fi);
3204}
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3207
3208 void
3209set_context_for_expression(xp, arg, cmdidx)
3210 expand_T *xp;
3211 char_u *arg;
3212 cmdidx_T cmdidx;
3213{
3214 int got_eq = FALSE;
3215 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 if (cmdidx == CMD_let)
3219 {
3220 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003221 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 {
3223 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003224 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 {
3226 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 if (vim_iswhite(*p))
3229 break;
3230 }
3231 return;
3232 }
3233 }
3234 else
3235 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3236 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 while ((xp->xp_pattern = vim_strpbrk(arg,
3238 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3239 {
3240 c = *xp->xp_pattern;
3241 if (c == '&')
3242 {
3243 c = xp->xp_pattern[1];
3244 if (c == '&')
3245 {
3246 ++xp->xp_pattern;
3247 xp->xp_context = cmdidx != CMD_let || got_eq
3248 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3249 }
3250 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003253 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3254 xp->xp_pattern += 2;
3255
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 else if (c == '$')
3259 {
3260 /* environment variable */
3261 xp->xp_context = EXPAND_ENV_VARS;
3262 }
3263 else if (c == '=')
3264 {
3265 got_eq = TRUE;
3266 xp->xp_context = EXPAND_EXPRESSION;
3267 }
3268 else if (c == '<'
3269 && xp->xp_context == EXPAND_FUNCTIONS
3270 && vim_strchr(xp->xp_pattern, '(') == NULL)
3271 {
3272 /* Function name can start with "<SNR>" */
3273 break;
3274 }
3275 else if (cmdidx != CMD_let || got_eq)
3276 {
3277 if (c == '"') /* string */
3278 {
3279 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3280 if (c == '\\' && xp->xp_pattern[1] != NUL)
3281 ++xp->xp_pattern;
3282 xp->xp_context = EXPAND_NOTHING;
3283 }
3284 else if (c == '\'') /* literal string */
3285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3288 /* skip */ ;
3289 xp->xp_context = EXPAND_NOTHING;
3290 }
3291 else if (c == '|')
3292 {
3293 if (xp->xp_pattern[1] == '|')
3294 {
3295 ++xp->xp_pattern;
3296 xp->xp_context = EXPAND_EXPRESSION;
3297 }
3298 else
3299 xp->xp_context = EXPAND_COMMANDS;
3300 }
3301 else
3302 xp->xp_context = EXPAND_EXPRESSION;
3303 }
3304 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 /* Doesn't look like something valid, expand as an expression
3306 * anyway. */
3307 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 arg = xp->xp_pattern;
3309 if (*arg != NUL)
3310 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3311 /* skip */ ;
3312 }
3313 xp->xp_pattern = arg;
3314}
3315
3316#endif /* FEAT_CMDL_COMPL */
3317
3318/*
3319 * ":1,25call func(arg1, arg2)" function call.
3320 */
3321 void
3322ex_call(eap)
3323 exarg_T *eap;
3324{
3325 char_u *arg = eap->arg;
3326 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003328 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 linenr_T lnum;
3332 int doesrange;
3333 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003334 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003336 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003337 if (fudi.fd_newkey != NULL)
3338 {
3339 /* Still need to give an error message for missing key. */
3340 EMSG2(_(e_dictkey), fudi.fd_newkey);
3341 vim_free(fudi.fd_newkey);
3342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003343 if (tofree == NULL)
3344 return;
3345
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003346 /* Increase refcount on dictionary, it could get deleted when evaluating
3347 * the arguments. */
3348 if (fudi.fd_dict != NULL)
3349 ++fudi.fd_dict->dv_refcount;
3350
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003351 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003352 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003353 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaar532c7802005-01-27 14:44:31 +00003355 /* Skip white space to allow ":call func ()". Not good, but required for
3356 * backward compatibility. */
3357 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 if (*startarg != '(')
3361 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003362 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 goto end;
3364 }
3365
3366 /*
3367 * When skipping, evaluate the function once, to find the end of the
3368 * arguments.
3369 * When the function takes a range, this is discovered after the first
3370 * call, and the loop is broken.
3371 */
3372 if (eap->skip)
3373 {
3374 ++emsg_skip;
3375 lnum = eap->line2; /* do it once, also with an invalid range */
3376 }
3377 else
3378 lnum = eap->line1;
3379 for ( ; lnum <= eap->line2; ++lnum)
3380 {
3381 if (!eap->skip && eap->addr_count > 0)
3382 {
3383 curwin->w_cursor.lnum = lnum;
3384 curwin->w_cursor.col = 0;
3385 }
3386 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003387 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003388 eap->line1, eap->line2, &doesrange,
3389 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 failed = TRUE;
3392 break;
3393 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003394
3395 /* Handle a function returning a Funcref, Dictionary or List. */
3396 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3397 {
3398 failed = TRUE;
3399 break;
3400 }
3401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (doesrange || eap->skip)
3404 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003409 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (aborting())
3411 break;
3412 }
3413 if (eap->skip)
3414 --emsg_skip;
3415
3416 if (!failed)
3417 {
3418 /* Check for trailing illegal characters and a following command. */
3419 if (!ends_excmd(*arg))
3420 {
3421 emsg_severe = TRUE;
3422 EMSG(_(e_trailing));
3423 }
3424 else
3425 eap->nextcmd = check_nextcmd(arg);
3426 }
3427
3428end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003429 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003430 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431}
3432
3433/*
3434 * ":unlet[!] var1 ... " command.
3435 */
3436 void
3437ex_unlet(eap)
3438 exarg_T *eap;
3439{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003440 ex_unletlock(eap, eap->arg, 0);
3441}
3442
3443/*
3444 * ":lockvar" and ":unlockvar" commands
3445 */
3446 void
3447ex_lockvar(eap)
3448 exarg_T *eap;
3449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003451 int deep = 2;
3452
3453 if (eap->forceit)
3454 deep = -1;
3455 else if (vim_isdigit(*arg))
3456 {
3457 deep = getdigits(&arg);
3458 arg = skipwhite(arg);
3459 }
3460
3461 ex_unletlock(eap, arg, deep);
3462}
3463
3464/*
3465 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3466 */
3467 static void
3468ex_unletlock(eap, argstart, deep)
3469 exarg_T *eap;
3470 char_u *argstart;
3471 int deep;
3472{
3473 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003476 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 do
3479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003480 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003481 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3482 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003483 if (lv.ll_name == NULL)
3484 error = TRUE; /* error but continue parsing */
3485 if (name_end == NULL || (!vim_iswhite(*name_end)
3486 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003488 if (name_end != NULL)
3489 {
3490 emsg_severe = TRUE;
3491 EMSG(_(e_trailing));
3492 }
3493 if (!(eap->skip || error))
3494 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 break;
3496 }
3497
3498 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003499 {
3500 if (eap->cmdidx == CMD_unlet)
3501 {
3502 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3503 error = TRUE;
3504 }
3505 else
3506 {
3507 if (do_lock_var(&lv, name_end, deep,
3508 eap->cmdidx == CMD_lockvar) == FAIL)
3509 error = TRUE;
3510 }
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003513 if (!eap->skip)
3514 clear_lval(&lv);
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 arg = skipwhite(name_end);
3517 } while (!ends_excmd(*arg));
3518
3519 eap->nextcmd = check_nextcmd(arg);
3520}
3521
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003523do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003525 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 int forceit;
3527{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528 int ret = OK;
3529 int cc;
3530
3531 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003533 cc = *name_end;
3534 *name_end = NUL;
3535
3536 /* Normal name or expanded name. */
3537 if (check_changedtick(lp->ll_name))
3538 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003539 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003541 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003543 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3544 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 else if (lp->ll_range)
3546 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003548
3549 /* Delete a range of List items. */
3550 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3551 {
3552 li = lp->ll_li->li_next;
3553 listitem_remove(lp->ll_list, lp->ll_li);
3554 lp->ll_li = li;
3555 ++lp->ll_n1;
3556 }
3557 }
3558 else
3559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 /* unlet a List item. */
3562 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003565 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 }
3567
3568 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569}
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571/*
3572 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 */
3575 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 hashtab_T *ht;
3581 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003582 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003583 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 ht = find_var_ht(name, &varname);
3586 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003588 hi = hash_find(ht, varname);
3589 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003590 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003591 di = HI2DI(hi);
3592 if (var_check_fixed(di->di_flags, name)
3593 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 return FAIL;
3595 delete_var(ht, hi);
3596 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 if (forceit)
3600 return OK;
3601 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 return FAIL;
3603}
3604
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605/*
3606 * Lock or unlock variable indicated by "lp".
3607 * "deep" is the levels to go (-1 for unlimited);
3608 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3609 */
3610 static int
3611do_lock_var(lp, name_end, deep, lock)
3612 lval_T *lp;
3613 char_u *name_end;
3614 int deep;
3615 int lock;
3616{
3617 int ret = OK;
3618 int cc;
3619 dictitem_T *di;
3620
3621 if (deep == 0) /* nothing to do */
3622 return OK;
3623
3624 if (lp->ll_tv == NULL)
3625 {
3626 cc = *name_end;
3627 *name_end = NUL;
3628
3629 /* Normal name or expanded name. */
3630 if (check_changedtick(lp->ll_name))
3631 ret = FAIL;
3632 else
3633 {
3634 di = find_var(lp->ll_name, NULL);
3635 if (di == NULL)
3636 ret = FAIL;
3637 else
3638 {
3639 if (lock)
3640 di->di_flags |= DI_FLAGS_LOCK;
3641 else
3642 di->di_flags &= ~DI_FLAGS_LOCK;
3643 item_lock(&di->di_tv, deep, lock);
3644 }
3645 }
3646 *name_end = cc;
3647 }
3648 else if (lp->ll_range)
3649 {
3650 listitem_T *li = lp->ll_li;
3651
3652 /* (un)lock a range of List items. */
3653 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3654 {
3655 item_lock(&li->li_tv, deep, lock);
3656 li = li->li_next;
3657 ++lp->ll_n1;
3658 }
3659 }
3660 else if (lp->ll_list != NULL)
3661 /* (un)lock a List item. */
3662 item_lock(&lp->ll_li->li_tv, deep, lock);
3663 else
3664 /* un(lock) a Dictionary item. */
3665 item_lock(&lp->ll_di->di_tv, deep, lock);
3666
3667 return ret;
3668}
3669
3670/*
3671 * Lock or unlock an item. "deep" is nr of levels to go.
3672 */
3673 static void
3674item_lock(tv, deep, lock)
3675 typval_T *tv;
3676 int deep;
3677 int lock;
3678{
3679 static int recurse = 0;
3680 list_T *l;
3681 listitem_T *li;
3682 dict_T *d;
3683 hashitem_T *hi;
3684 int todo;
3685
3686 if (recurse >= DICT_MAXNEST)
3687 {
3688 EMSG(_("E743: variable nested too deep for (un)lock"));
3689 return;
3690 }
3691 if (deep == 0)
3692 return;
3693 ++recurse;
3694
3695 /* lock/unlock the item itself */
3696 if (lock)
3697 tv->v_lock |= VAR_LOCKED;
3698 else
3699 tv->v_lock &= ~VAR_LOCKED;
3700
3701 switch (tv->v_type)
3702 {
3703 case VAR_LIST:
3704 if ((l = tv->vval.v_list) != NULL)
3705 {
3706 if (lock)
3707 l->lv_lock |= VAR_LOCKED;
3708 else
3709 l->lv_lock &= ~VAR_LOCKED;
3710 if (deep < 0 || deep > 1)
3711 /* recursive: lock/unlock the items the List contains */
3712 for (li = l->lv_first; li != NULL; li = li->li_next)
3713 item_lock(&li->li_tv, deep - 1, lock);
3714 }
3715 break;
3716 case VAR_DICT:
3717 if ((d = tv->vval.v_dict) != NULL)
3718 {
3719 if (lock)
3720 d->dv_lock |= VAR_LOCKED;
3721 else
3722 d->dv_lock &= ~VAR_LOCKED;
3723 if (deep < 0 || deep > 1)
3724 {
3725 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003726 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3728 {
3729 if (!HASHITEM_EMPTY(hi))
3730 {
3731 --todo;
3732 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3733 }
3734 }
3735 }
3736 }
3737 }
3738 --recurse;
3739}
3740
Bram Moolenaara40058a2005-07-11 22:42:07 +00003741/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003742 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3743 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003744 */
3745 static int
3746tv_islocked(tv)
3747 typval_T *tv;
3748{
3749 return (tv->v_lock & VAR_LOCKED)
3750 || (tv->v_type == VAR_LIST
3751 && tv->vval.v_list != NULL
3752 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3753 || (tv->v_type == VAR_DICT
3754 && tv->vval.v_dict != NULL
3755 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3756}
3757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3759/*
3760 * Delete all "menutrans_" variables.
3761 */
3762 void
3763del_menutrans_vars()
3764{
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003769 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 {
3772 if (!HASHITEM_EMPTY(hi))
3773 {
3774 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3776 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003777 }
3778 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780}
3781#endif
3782
3783#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3784
3785/*
3786 * Local string buffer for the next two functions to store a variable name
3787 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3788 * get_user_var_name().
3789 */
3790
3791static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3792
3793static char_u *varnamebuf = NULL;
3794static int varnamebuflen = 0;
3795
3796/*
3797 * Function to concatenate a prefix and a variable name.
3798 */
3799 static char_u *
3800cat_prefix_varname(prefix, name)
3801 int prefix;
3802 char_u *name;
3803{
3804 int len;
3805
3806 len = (int)STRLEN(name) + 3;
3807 if (len > varnamebuflen)
3808 {
3809 vim_free(varnamebuf);
3810 len += 10; /* some additional space */
3811 varnamebuf = alloc(len);
3812 if (varnamebuf == NULL)
3813 {
3814 varnamebuflen = 0;
3815 return NULL;
3816 }
3817 varnamebuflen = len;
3818 }
3819 *varnamebuf = prefix;
3820 varnamebuf[1] = ':';
3821 STRCPY(varnamebuf + 2, name);
3822 return varnamebuf;
3823}
3824
3825/*
3826 * Function given to ExpandGeneric() to obtain the list of user defined
3827 * (global/buffer/window/built-in) variable names.
3828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 char_u *
3830get_user_var_name(xp, idx)
3831 expand_T *xp;
3832 int idx;
3833{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003834 static long_u gdone;
3835 static long_u bdone;
3836 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003837#ifdef FEAT_WINDOWS
3838 static long_u tdone;
3839#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003840 static int vidx;
3841 static hashitem_T *hi;
3842 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003845 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003847#ifdef FEAT_WINDOWS
3848 tdone = 0;
3849#endif
3850 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003851
3852 /* Global variables */
3853 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003857 else
3858 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003859 while (HASHITEM_EMPTY(hi))
3860 ++hi;
3861 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3862 return cat_prefix_varname('g', hi->hi_key);
3863 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003865
3866 /* b: variables */
3867 ht = &curbuf->b_vars.dv_hashtab;
3868 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003872 else
3873 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003874 while (HASHITEM_EMPTY(hi))
3875 ++hi;
3876 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003878 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003880 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return (char_u *)"b:changedtick";
3882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003883
3884 /* w: variables */
3885 ht = &curwin->w_vars.dv_hashtab;
3886 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003888 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003890 else
3891 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003892 while (HASHITEM_EMPTY(hi))
3893 ++hi;
3894 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003896
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003897#ifdef FEAT_WINDOWS
3898 /* t: variables */
3899 ht = &curtab->tp_vars.dv_hashtab;
3900 if (tdone < ht->ht_used)
3901 {
3902 if (tdone++ == 0)
3903 hi = ht->ht_array;
3904 else
3905 ++hi;
3906 while (HASHITEM_EMPTY(hi))
3907 ++hi;
3908 return cat_prefix_varname('t', hi->hi_key);
3909 }
3910#endif
3911
Bram Moolenaar33570922005-01-25 22:26:29 +00003912 /* v: variables */
3913 if (vidx < VV_LEN)
3914 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 vim_free(varnamebuf);
3917 varnamebuf = NULL;
3918 varnamebuflen = 0;
3919 return NULL;
3920}
3921
3922#endif /* FEAT_CMDL_COMPL */
3923
3924/*
3925 * types for expressions.
3926 */
3927typedef enum
3928{
3929 TYPE_UNKNOWN = 0
3930 , TYPE_EQUAL /* == */
3931 , TYPE_NEQUAL /* != */
3932 , TYPE_GREATER /* > */
3933 , TYPE_GEQUAL /* >= */
3934 , TYPE_SMALLER /* < */
3935 , TYPE_SEQUAL /* <= */
3936 , TYPE_MATCH /* =~ */
3937 , TYPE_NOMATCH /* !~ */
3938} exptype_T;
3939
3940/*
3941 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3944 */
3945
3946/*
3947 * Handle zero level expression.
3948 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003950 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 * Return OK or FAIL.
3952 */
3953 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 char_u **nextcmd;
3958 int evaluate;
3959{
3960 int ret;
3961 char_u *p;
3962
3963 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (ret == FAIL || !ends_excmd(*p))
3966 {
3967 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 /*
3970 * Report the invalid expression unless the expression evaluation has
3971 * been cancelled due to an aborting error, an interrupt, or an
3972 * exception.
3973 */
3974 if (!aborting())
3975 EMSG2(_(e_invexpr2), arg);
3976 ret = FAIL;
3977 }
3978 if (nextcmd != NULL)
3979 *nextcmd = check_nextcmd(p);
3980
3981 return ret;
3982}
3983
3984/*
3985 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003986 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 *
3988 * "arg" must point to the first non-white of the expression.
3989 * "arg" is advanced to the next non-white after the recognized expression.
3990 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003991 * Note: "rettv.v_lock" is not set.
3992 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * Return OK or FAIL.
3994 */
3995 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int evaluate;
4000{
4001 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 /*
4005 * Get the first variable.
4006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return FAIL;
4009
4010 if ((*arg)[0] == '?')
4011 {
4012 result = FALSE;
4013 if (evaluate)
4014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 int error = FALSE;
4016
4017 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 if (error)
4021 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023
4024 /*
4025 * Get the second variable.
4026 */
4027 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030
4031 /*
4032 * Check for the ":".
4033 */
4034 if ((*arg)[0] != ':')
4035 {
4036 EMSG(_("E109: Missing ':' after '?'"));
4037 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 return FAIL;
4040 }
4041
4042 /*
4043 * Get the third variable.
4044 */
4045 *arg = skipwhite(*arg + 1);
4046 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4047 {
4048 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return FAIL;
4051 }
4052 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055
4056 return OK;
4057}
4058
4059/*
4060 * Handle first level expression:
4061 * expr2 || expr2 || expr2 logical OR
4062 *
4063 * "arg" must point to the first non-white of the expression.
4064 * "arg" is advanced to the next non-white after the recognized expression.
4065 *
4066 * Return OK or FAIL.
4067 */
4068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int evaluate;
4073{
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 long result;
4076 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 /*
4080 * Get the first variable.
4081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return FAIL;
4084
4085 /*
4086 * Repeat until there is no following "||".
4087 */
4088 first = TRUE;
4089 result = FALSE;
4090 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4091 {
4092 if (evaluate && first)
4093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004097 if (error)
4098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 first = FALSE;
4100 }
4101
4102 /*
4103 * Get the second variable.
4104 */
4105 *arg = skipwhite(*arg + 2);
4106 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4107 return FAIL;
4108
4109 /*
4110 * Compute the result.
4111 */
4112 if (evaluate && !result)
4113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117 if (error)
4118 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 if (evaluate)
4121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 rettv->v_type = VAR_NUMBER;
4123 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 }
4126
4127 return OK;
4128}
4129
4130/*
4131 * Handle second level expression:
4132 * expr3 && expr3 && expr3 logical AND
4133 *
4134 * "arg" must point to the first non-white of the expression.
4135 * "arg" is advanced to the next non-white after the recognized expression.
4136 *
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 long result;
4147 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 /*
4151 * Get the first variable.
4152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 return FAIL;
4155
4156 /*
4157 * Repeat until there is no following "&&".
4158 */
4159 first = TRUE;
4160 result = TRUE;
4161 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4162 {
4163 if (evaluate && first)
4164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (error)
4169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 first = FALSE;
4171 }
4172
4173 /*
4174 * Get the second variable.
4175 */
4176 *arg = skipwhite(*arg + 2);
4177 if (eval4(arg, &var2, evaluate && result) == FAIL)
4178 return FAIL;
4179
4180 /*
4181 * Compute the result.
4182 */
4183 if (evaluate && result)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 if (evaluate)
4192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 rettv->v_type = VAR_NUMBER;
4194 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
4197
4198 return OK;
4199}
4200
4201/*
4202 * Handle third level expression:
4203 * var1 == var2
4204 * var1 =~ var2
4205 * var1 != var2
4206 * var1 !~ var2
4207 * var1 > var2
4208 * var1 >= var2
4209 * var1 < var2
4210 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004211 * var1 is var2
4212 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 *
4214 * "arg" must point to the first non-white of the expression.
4215 * "arg" is advanced to the next non-white after the recognized expression.
4216 *
4217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 int evaluate;
4224{
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u *p;
4227 int i;
4228 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004229 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 int len = 2;
4231 long n1, n2;
4232 char_u *s1, *s2;
4233 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4234 regmatch_T regmatch;
4235 int ic;
4236 char_u *save_cpo;
4237
4238 /*
4239 * Get the first variable.
4240 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 p = *arg;
4245 switch (p[0])
4246 {
4247 case '=': if (p[1] == '=')
4248 type = TYPE_EQUAL;
4249 else if (p[1] == '~')
4250 type = TYPE_MATCH;
4251 break;
4252 case '!': if (p[1] == '=')
4253 type = TYPE_NEQUAL;
4254 else if (p[1] == '~')
4255 type = TYPE_NOMATCH;
4256 break;
4257 case '>': if (p[1] != '=')
4258 {
4259 type = TYPE_GREATER;
4260 len = 1;
4261 }
4262 else
4263 type = TYPE_GEQUAL;
4264 break;
4265 case '<': if (p[1] != '=')
4266 {
4267 type = TYPE_SMALLER;
4268 len = 1;
4269 }
4270 else
4271 type = TYPE_SEQUAL;
4272 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004273 case 'i': if (p[1] == 's')
4274 {
4275 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4276 len = 5;
4277 if (!vim_isIDc(p[len]))
4278 {
4279 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4280 type_is = TRUE;
4281 }
4282 }
4283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004287 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 */
4289 if (type != TYPE_UNKNOWN)
4290 {
4291 /* extra question mark appended: ignore case */
4292 if (p[len] == '?')
4293 {
4294 ic = TRUE;
4295 ++len;
4296 }
4297 /* extra '#' appended: match case */
4298 else if (p[len] == '#')
4299 {
4300 ic = FALSE;
4301 ++len;
4302 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004303 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 else
4305 ic = p_ic;
4306
4307 /*
4308 * Get the second variable.
4309 */
4310 *arg = skipwhite(p + len);
4311 if (eval5(arg, &var2, evaluate) == FAIL)
4312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315 }
4316
4317 if (evaluate)
4318 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004319 if (type_is && rettv->v_type != var2.v_type)
4320 {
4321 /* For "is" a different type always means FALSE, for "notis"
4322 * it means TRUE. */
4323 n1 = (type == TYPE_NEQUAL);
4324 }
4325 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4326 {
4327 if (type_is)
4328 {
4329 n1 = (rettv->v_type == var2.v_type
4330 && rettv->vval.v_list == var2.vval.v_list);
4331 if (type == TYPE_NEQUAL)
4332 n1 = !n1;
4333 }
4334 else if (rettv->v_type != var2.v_type
4335 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4336 {
4337 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004338 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004340 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004341 clear_tv(rettv);
4342 clear_tv(&var2);
4343 return FAIL;
4344 }
4345 else
4346 {
4347 /* Compare two Lists for being equal or unequal. */
4348 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4349 if (type == TYPE_NEQUAL)
4350 n1 = !n1;
4351 }
4352 }
4353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004354 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4355 {
4356 if (type_is)
4357 {
4358 n1 = (rettv->v_type == var2.v_type
4359 && rettv->vval.v_dict == var2.vval.v_dict);
4360 if (type == TYPE_NEQUAL)
4361 n1 = !n1;
4362 }
4363 else if (rettv->v_type != var2.v_type
4364 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4365 {
4366 if (rettv->v_type != var2.v_type)
4367 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4368 else
4369 EMSG(_("E736: Invalid operation for Dictionary"));
4370 clear_tv(rettv);
4371 clear_tv(&var2);
4372 return FAIL;
4373 }
4374 else
4375 {
4376 /* Compare two Dictionaries for being equal or unequal. */
4377 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4378 if (type == TYPE_NEQUAL)
4379 n1 = !n1;
4380 }
4381 }
4382
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4384 {
4385 if (rettv->v_type != var2.v_type
4386 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4387 {
4388 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004389 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004391 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 clear_tv(rettv);
4393 clear_tv(&var2);
4394 return FAIL;
4395 }
4396 else
4397 {
4398 /* Compare two Funcrefs for being equal or unequal. */
4399 if (rettv->vval.v_string == NULL
4400 || var2.vval.v_string == NULL)
4401 n1 = FALSE;
4402 else
4403 n1 = STRCMP(rettv->vval.v_string,
4404 var2.vval.v_string) == 0;
4405 if (type == TYPE_NEQUAL)
4406 n1 = !n1;
4407 }
4408 }
4409
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004410#ifdef FEAT_FLOAT
4411 /*
4412 * If one of the two variables is a float, compare as a float.
4413 * When using "=~" or "!~", always compare as string.
4414 */
4415 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4416 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4417 {
4418 float_T f1, f2;
4419
4420 if (rettv->v_type == VAR_FLOAT)
4421 f1 = rettv->vval.v_float;
4422 else
4423 f1 = get_tv_number(rettv);
4424 if (var2.v_type == VAR_FLOAT)
4425 f2 = var2.vval.v_float;
4426 else
4427 f2 = get_tv_number(&var2);
4428 n1 = FALSE;
4429 switch (type)
4430 {
4431 case TYPE_EQUAL: n1 = (f1 == f2); break;
4432 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4433 case TYPE_GREATER: n1 = (f1 > f2); break;
4434 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4435 case TYPE_SMALLER: n1 = (f1 < f2); break;
4436 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4437 case TYPE_UNKNOWN:
4438 case TYPE_MATCH:
4439 case TYPE_NOMATCH: break; /* avoid gcc warning */
4440 }
4441 }
4442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 /*
4445 * If one of the two variables is a number, compare as a number.
4446 * When using "=~" or "!~", always compare as string.
4447 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 n1 = get_tv_number(rettv);
4452 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 switch (type)
4454 {
4455 case TYPE_EQUAL: n1 = (n1 == n2); break;
4456 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4457 case TYPE_GREATER: n1 = (n1 > n2); break;
4458 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4459 case TYPE_SMALLER: n1 = (n1 < n2); break;
4460 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4461 case TYPE_UNKNOWN:
4462 case TYPE_MATCH:
4463 case TYPE_NOMATCH: break; /* avoid gcc warning */
4464 }
4465 }
4466 else
4467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 s1 = get_tv_string_buf(rettv, buf1);
4469 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4471 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4472 else
4473 i = 0;
4474 n1 = FALSE;
4475 switch (type)
4476 {
4477 case TYPE_EQUAL: n1 = (i == 0); break;
4478 case TYPE_NEQUAL: n1 = (i != 0); break;
4479 case TYPE_GREATER: n1 = (i > 0); break;
4480 case TYPE_GEQUAL: n1 = (i >= 0); break;
4481 case TYPE_SMALLER: n1 = (i < 0); break;
4482 case TYPE_SEQUAL: n1 = (i <= 0); break;
4483
4484 case TYPE_MATCH:
4485 case TYPE_NOMATCH:
4486 /* avoid 'l' flag in 'cpoptions' */
4487 save_cpo = p_cpo;
4488 p_cpo = (char_u *)"";
4489 regmatch.regprog = vim_regcomp(s2,
4490 RE_MAGIC + RE_STRING);
4491 regmatch.rm_ic = ic;
4492 if (regmatch.regprog != NULL)
4493 {
4494 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4495 vim_free(regmatch.regprog);
4496 if (type == TYPE_NOMATCH)
4497 n1 = !n1;
4498 }
4499 p_cpo = save_cpo;
4500 break;
4501
4502 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4503 }
4504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 clear_tv(rettv);
4506 clear_tv(&var2);
4507 rettv->v_type = VAR_NUMBER;
4508 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 }
4511
4512 return OK;
4513}
4514
4515/*
4516 * Handle fourth level expression:
4517 * + number addition
4518 * - number subtraction
4519 * . string concatenation
4520 *
4521 * "arg" must point to the first non-white of the expression.
4522 * "arg" is advanced to the next non-white after the recognized expression.
4523 *
4524 * Return OK or FAIL.
4525 */
4526 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 int evaluate;
4531{
Bram Moolenaar33570922005-01-25 22:26:29 +00004532 typval_T var2;
4533 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 int op;
4535 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536#ifdef FEAT_FLOAT
4537 float_T f1 = 0, f2 = 0;
4538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 char_u *s1, *s2;
4540 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4541 char_u *p;
4542
4543 /*
4544 * Get the first variable.
4545 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004546 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 return FAIL;
4548
4549 /*
4550 * Repeat computing, until no '+', '-' or '.' is following.
4551 */
4552 for (;;)
4553 {
4554 op = **arg;
4555 if (op != '+' && op != '-' && op != '.')
4556 break;
4557
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004558 if ((op != '+' || rettv->v_type != VAR_LIST)
4559#ifdef FEAT_FLOAT
4560 && (op == '.' || rettv->v_type != VAR_FLOAT)
4561#endif
4562 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 {
4564 /* For "list + ...", an illegal use of the first operand as
4565 * a number cannot be determined before evaluating the 2nd
4566 * operand: if this is also a list, all is ok.
4567 * For "something . ...", "something - ..." or "non-list + ...",
4568 * we know that the first operand needs to be a string or number
4569 * without evaluating the 2nd operand. So check before to avoid
4570 * side effects after an error. */
4571 if (evaluate && get_tv_string_chk(rettv) == NULL)
4572 {
4573 clear_tv(rettv);
4574 return FAIL;
4575 }
4576 }
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /*
4579 * Get the second variable.
4580 */
4581 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004582 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 return FAIL;
4586 }
4587
4588 if (evaluate)
4589 {
4590 /*
4591 * Compute the result.
4592 */
4593 if (op == '.')
4594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4596 s2 = get_tv_string_buf_chk(&var2, buf2);
4597 if (s2 == NULL) /* type error ? */
4598 {
4599 clear_tv(rettv);
4600 clear_tv(&var2);
4601 return FAIL;
4602 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004603 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 clear_tv(rettv);
4605 rettv->v_type = VAR_STRING;
4606 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004608 else if (op == '+' && rettv->v_type == VAR_LIST
4609 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004610 {
4611 /* concatenate Lists */
4612 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4613 &var3) == FAIL)
4614 {
4615 clear_tv(rettv);
4616 clear_tv(&var2);
4617 return FAIL;
4618 }
4619 clear_tv(rettv);
4620 *rettv = var3;
4621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 else
4623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 int error = FALSE;
4625
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004626#ifdef FEAT_FLOAT
4627 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629 f1 = rettv->vval.v_float;
4630 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 else
4633#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635 n1 = get_tv_number_chk(rettv, &error);
4636 if (error)
4637 {
4638 /* This can only happen for "list + non-list". For
4639 * "non-list + ..." or "something - ...", we returned
4640 * before evaluating the 2nd operand. */
4641 clear_tv(rettv);
4642 return FAIL;
4643 }
4644#ifdef FEAT_FLOAT
4645 if (var2.v_type == VAR_FLOAT)
4646 f1 = n1;
4647#endif
4648 }
4649#ifdef FEAT_FLOAT
4650 if (var2.v_type == VAR_FLOAT)
4651 {
4652 f2 = var2.vval.v_float;
4653 n2 = 0;
4654 }
4655 else
4656#endif
4657 {
4658 n2 = get_tv_number_chk(&var2, &error);
4659 if (error)
4660 {
4661 clear_tv(rettv);
4662 clear_tv(&var2);
4663 return FAIL;
4664 }
4665#ifdef FEAT_FLOAT
4666 if (rettv->v_type == VAR_FLOAT)
4667 f2 = n2;
4668#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004671
4672#ifdef FEAT_FLOAT
4673 /* If there is a float on either side the result is a float. */
4674 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4675 {
4676 if (op == '+')
4677 f1 = f1 + f2;
4678 else
4679 f1 = f1 - f2;
4680 rettv->v_type = VAR_FLOAT;
4681 rettv->vval.v_float = f1;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684#endif
4685 {
4686 if (op == '+')
4687 n1 = n1 + n2;
4688 else
4689 n1 = n1 - n2;
4690 rettv->v_type = VAR_NUMBER;
4691 rettv->vval.v_number = n1;
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 }
4697 return OK;
4698}
4699
4700/*
4701 * Handle fifth level expression:
4702 * * number multiplication
4703 * / number division
4704 * % number modulo
4705 *
4706 * "arg" must point to the first non-white of the expression.
4707 * "arg" is advanced to the next non-white after the recognized expression.
4708 *
4709 * Return OK or FAIL.
4710 */
4711 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004716 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int op;
4720 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721#ifdef FEAT_FLOAT
4722 int use_float = FALSE;
4723 float_T f1 = 0, f2;
4724#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Get the first variable.
4729 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004730 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 return FAIL;
4732
4733 /*
4734 * Repeat computing, until no '*', '/' or '%' is following.
4735 */
4736 for (;;)
4737 {
4738 op = **arg;
4739 if (op != '*' && op != '/' && op != '%')
4740 break;
4741
4742 if (evaluate)
4743 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744#ifdef FEAT_FLOAT
4745 if (rettv->v_type == VAR_FLOAT)
4746 {
4747 f1 = rettv->vval.v_float;
4748 use_float = TRUE;
4749 n1 = 0;
4750 }
4751 else
4752#endif
4753 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 if (error)
4756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758 else
4759 n1 = 0;
4760
4761 /*
4762 * Get the second variable.
4763 */
4764 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004765 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 return FAIL;
4767
4768 if (evaluate)
4769 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 if (var2.v_type == VAR_FLOAT)
4772 {
4773 if (!use_float)
4774 {
4775 f1 = n1;
4776 use_float = TRUE;
4777 }
4778 f2 = var2.vval.v_float;
4779 n2 = 0;
4780 }
4781 else
4782#endif
4783 {
4784 n2 = get_tv_number_chk(&var2, &error);
4785 clear_tv(&var2);
4786 if (error)
4787 return FAIL;
4788#ifdef FEAT_FLOAT
4789 if (use_float)
4790 f2 = n2;
4791#endif
4792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 /*
4795 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798#ifdef FEAT_FLOAT
4799 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 if (op == '*')
4802 f1 = f1 * f2;
4803 else if (op == '/')
4804 {
4805 /* We rely on the floating point library to handle divide
4806 * by zero to result in "inf" and not a crash. */
4807 f1 = f1 / f2;
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004810 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004811 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812 return FAIL;
4813 }
4814 rettv->v_type = VAR_FLOAT;
4815 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820 if (op == '*')
4821 n1 = n1 * n2;
4822 else if (op == '/')
4823 {
4824 if (n2 == 0) /* give an error message? */
4825 {
4826 if (n1 == 0)
4827 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4828 else if (n1 < 0)
4829 n1 = -0x7fffffffL;
4830 else
4831 n1 = 0x7fffffffL;
4832 }
4833 else
4834 n1 = n1 / n2;
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837 {
4838 if (n2 == 0) /* give an error message? */
4839 n1 = 0;
4840 else
4841 n1 = n1 % n2;
4842 }
4843 rettv->v_type = VAR_NUMBER;
4844 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848
4849 return OK;
4850}
4851
4852/*
4853 * Handle sixth level expression:
4854 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004855 * "string" string constant
4856 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * &option-name option value
4858 * @r register contents
4859 * identifier variable value
4860 * function() function call
4861 * $VAR environment variable
4862 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004863 * [expr, expr] List
4864 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 *
4866 * Also handle:
4867 * ! in front logical NOT
4868 * - in front unary minus
4869 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004870 * trailing [] subscript in String or List
4871 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 *
4873 * "arg" must point to the first non-white of the expression.
4874 * "arg" is advanced to the next non-white after the recognized expression.
4875 *
4876 * Return OK or FAIL.
4877 */
4878 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004879eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004883 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 long n;
4886 int len;
4887 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 char_u *start_leader, *end_leader;
4889 int ret = OK;
4890 char_u *alias;
4891
4892 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
4898 /*
4899 * Skip '!' and '-' characters. They are handled later.
4900 */
4901 start_leader = *arg;
4902 while (**arg == '!' || **arg == '-' || **arg == '+')
4903 *arg = skipwhite(*arg + 1);
4904 end_leader = *arg;
4905
4906 switch (**arg)
4907 {
4908 /*
4909 * Number constant.
4910 */
4911 case '0':
4912 case '1':
4913 case '2':
4914 case '3':
4915 case '4':
4916 case '5':
4917 case '6':
4918 case '7':
4919 case '8':
4920 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 {
4922#ifdef FEAT_FLOAT
4923 char_u *p = skipdigits(*arg + 1);
4924 int get_float = FALSE;
4925
4926 /* We accept a float when the format matches
4927 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004928 * strict to avoid backwards compatibility problems.
4929 * Don't look for a float after the "." operator, so that
4930 * ":let vers = 1.2.3" doesn't fail. */
4931 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004933 get_float = TRUE;
4934 p = skipdigits(p + 2);
4935 if (*p == 'e' || *p == 'E')
4936 {
4937 ++p;
4938 if (*p == '-' || *p == '+')
4939 ++p;
4940 if (!vim_isdigit(*p))
4941 get_float = FALSE;
4942 else
4943 p = skipdigits(p + 1);
4944 }
4945 if (ASCII_ISALPHA(*p) || *p == '.')
4946 get_float = FALSE;
4947 }
4948 if (get_float)
4949 {
4950 float_T f;
4951
4952 *arg += string2float(*arg, &f);
4953 if (evaluate)
4954 {
4955 rettv->v_type = VAR_FLOAT;
4956 rettv->vval.v_float = f;
4957 }
4958 }
4959 else
4960#endif
4961 {
4962 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4963 *arg += len;
4964 if (evaluate)
4965 {
4966 rettv->v_type = VAR_NUMBER;
4967 rettv->vval.v_number = n;
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
4970 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 /*
4974 * String constant: "string".
4975 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978
4979 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 break;
4984
4985 /*
4986 * List: [expr, expr]
4987 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 break;
4990
4991 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 * Dictionary: {key: val, key: val}
4993 */
4994 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4995 break;
4996
4997 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004998 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005000 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 break;
5002
5003 /*
5004 * Environment variable: $VAR.
5005 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005006 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008
5009 /*
5010 * Register contents: @r.
5011 */
5012 case '@': ++*arg;
5013 if (evaluate)
5014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005016 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 if (**arg != NUL)
5019 ++*arg;
5020 break;
5021
5022 /*
5023 * nested expression: (expression).
5024 */
5025 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005026 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (**arg == ')')
5028 ++*arg;
5029 else if (ret == OK)
5030 {
5031 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005032 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 ret = FAIL;
5034 }
5035 break;
5036
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 break;
5039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040
5041 if (ret == NOTDONE)
5042 {
5043 /*
5044 * Must be a variable or function name.
5045 * Can also be a curly-braces kind of name: {expr}.
5046 */
5047 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005048 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 if (alias != NULL)
5050 s = alias;
5051
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005052 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 ret = FAIL;
5054 else
5055 {
5056 if (**arg == '(') /* recursive! */
5057 {
5058 /* If "s" is the name of a variable of type VAR_FUNC
5059 * use its contents. */
5060 s = deref_func_name(s, &len);
5061
5062 /* Invoke the function. */
5063 ret = get_func_tv(s, len, rettv, arg,
5064 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005065 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 /* Stop the expression evaluation when immediately
5067 * aborting on error, or when an interrupt occurred or
5068 * an exception was thrown but not caught. */
5069 if (aborting())
5070 {
5071 if (ret == OK)
5072 clear_tv(rettv);
5073 ret = FAIL;
5074 }
5075 }
5076 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005077 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005078 else
5079 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 }
5081
5082 if (alias != NULL)
5083 vim_free(alias);
5084 }
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 *arg = skipwhite(*arg);
5087
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005088 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5089 * expr(expr). */
5090 if (ret == OK)
5091 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
5093 /*
5094 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5095 */
5096 if (ret == OK && evaluate && end_leader > start_leader)
5097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005098 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 int val = 0;
5100#ifdef FEAT_FLOAT
5101 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005102
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 if (rettv->v_type == VAR_FLOAT)
5104 f = rettv->vval.v_float;
5105 else
5106#endif
5107 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005110 clear_tv(rettv);
5111 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005113 else
5114 {
5115 while (end_leader > start_leader)
5116 {
5117 --end_leader;
5118 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 {
5120#ifdef FEAT_FLOAT
5121 if (rettv->v_type == VAR_FLOAT)
5122 f = !f;
5123 else
5124#endif
5125 val = !val;
5126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005128 {
5129#ifdef FEAT_FLOAT
5130 if (rettv->v_type == VAR_FLOAT)
5131 f = -f;
5132 else
5133#endif
5134 val = -val;
5135 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005136 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137#ifdef FEAT_FLOAT
5138 if (rettv->v_type == VAR_FLOAT)
5139 {
5140 clear_tv(rettv);
5141 rettv->vval.v_float = f;
5142 }
5143 else
5144#endif
5145 {
5146 clear_tv(rettv);
5147 rettv->v_type = VAR_NUMBER;
5148 rettv->vval.v_number = val;
5149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152
5153 return ret;
5154}
5155
5156/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005157 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5158 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5160 */
5161 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005162eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005166 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167{
5168 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005169 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 long len = -1;
5172 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005176 if (rettv->v_type == VAR_FUNC
5177#ifdef FEAT_FLOAT
5178 || rettv->v_type == VAR_FLOAT
5179#endif
5180 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005182 if (verbose)
5183 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 return FAIL;
5185 }
5186
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 /*
5190 * dict.name
5191 */
5192 key = *arg + 1;
5193 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5194 ;
5195 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 }
5199 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 /*
5202 * something[idx]
5203 *
5204 * Get the (first) variable from inside the [].
5205 */
5206 *arg = skipwhite(*arg + 1);
5207 if (**arg == ':')
5208 empty1 = TRUE;
5209 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5210 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5212 {
5213 /* not a number or string */
5214 clear_tv(&var1);
5215 return FAIL;
5216 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217
5218 /*
5219 * Get the second variable from inside the [:].
5220 */
5221 if (**arg == ':')
5222 {
5223 range = TRUE;
5224 *arg = skipwhite(*arg + 1);
5225 if (**arg == ']')
5226 empty2 = TRUE;
5227 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 if (!empty1)
5230 clear_tv(&var1);
5231 return FAIL;
5232 }
5233 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5234 {
5235 /* not a number or string */
5236 if (!empty1)
5237 clear_tv(&var1);
5238 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 return FAIL;
5240 }
5241 }
5242
5243 /* Check for the ']'. */
5244 if (**arg != ']')
5245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005246 if (verbose)
5247 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 clear_tv(&var1);
5249 if (range)
5250 clear_tv(&var2);
5251 return FAIL;
5252 }
5253 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 }
5255
5256 if (evaluate)
5257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 n1 = 0;
5259 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 n1 = get_tv_number(&var1);
5262 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 }
5264 if (range)
5265 {
5266 if (empty2)
5267 n2 = -1;
5268 else
5269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 n2 = get_tv_number(&var2);
5271 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 }
5273 }
5274
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005275 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
5277 case VAR_NUMBER:
5278 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 len = (long)STRLEN(s);
5281 if (range)
5282 {
5283 /* The resulting variable is a substring. If the indexes
5284 * are out of range the result is empty. */
5285 if (n1 < 0)
5286 {
5287 n1 = len + n1;
5288 if (n1 < 0)
5289 n1 = 0;
5290 }
5291 if (n2 < 0)
5292 n2 = len + n2;
5293 else if (n2 >= len)
5294 n2 = len;
5295 if (n1 >= len || n2 < 0 || n1 > n2)
5296 s = NULL;
5297 else
5298 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5299 }
5300 else
5301 {
5302 /* The resulting variable is a string of a single
5303 * character. If the index is too big or negative the
5304 * result is empty. */
5305 if (n1 >= len || n1 < 0)
5306 s = NULL;
5307 else
5308 s = vim_strnsave(s + n1, 1);
5309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005310 clear_tv(rettv);
5311 rettv->v_type = VAR_STRING;
5312 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 break;
5314
5315 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005316 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 if (n1 < 0)
5318 n1 = len + n1;
5319 if (!empty1 && (n1 < 0 || n1 >= len))
5320 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005321 /* For a range we allow invalid values and return an empty
5322 * list. A list index out of range is an error. */
5323 if (!range)
5324 {
5325 if (verbose)
5326 EMSGN(_(e_listidx), n1);
5327 return FAIL;
5328 }
5329 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 }
5331 if (range)
5332 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005333 list_T *l;
5334 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335
5336 if (n2 < 0)
5337 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005338 else if (n2 >= len)
5339 n2 = len - 1;
5340 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005341 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 l = list_alloc();
5343 if (l == NULL)
5344 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005345 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 n1 <= n2; ++n1)
5347 {
5348 if (list_append_tv(l, &item->li_tv) == FAIL)
5349 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005350 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 return FAIL;
5352 }
5353 item = item->li_next;
5354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355 clear_tv(rettv);
5356 rettv->v_type = VAR_LIST;
5357 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005358 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 }
5360 else
5361 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005362 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 clear_tv(rettv);
5364 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367
5368 case VAR_DICT:
5369 if (range)
5370 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 if (verbose)
5372 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 if (len == -1)
5374 clear_tv(&var1);
5375 return FAIL;
5376 }
5377 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379
5380 if (len == -1)
5381 {
5382 key = get_tv_string(&var1);
5383 if (*key == NUL)
5384 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005385 if (verbose)
5386 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 clear_tv(&var1);
5388 return FAIL;
5389 }
5390 }
5391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005392 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005394 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005395 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 if (len == -1)
5397 clear_tv(&var1);
5398 if (item == NULL)
5399 return FAIL;
5400
5401 copy_tv(&item->di_tv, &var1);
5402 clear_tv(rettv);
5403 *rettv = var1;
5404 }
5405 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 }
5407 }
5408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 return OK;
5410}
5411
5412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 * Get an option value.
5414 * "arg" points to the '&' or '+' before the option name.
5415 * "arg" is advanced to character after the option name.
5416 * Return OK or FAIL.
5417 */
5418 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 int evaluate;
5423{
5424 char_u *option_end;
5425 long numval;
5426 char_u *stringval;
5427 int opt_type;
5428 int c;
5429 int working = (**arg == '+'); /* has("+option") */
5430 int ret = OK;
5431 int opt_flags;
5432
5433 /*
5434 * Isolate the option name and find its value.
5435 */
5436 option_end = find_option_end(arg, &opt_flags);
5437 if (option_end == NULL)
5438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 EMSG2(_("E112: Option name missing: %s"), *arg);
5441 return FAIL;
5442 }
5443
5444 if (!evaluate)
5445 {
5446 *arg = option_end;
5447 return OK;
5448 }
5449
5450 c = *option_end;
5451 *option_end = NUL;
5452 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 if (opt_type == -3) /* invalid name */
5456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 EMSG2(_("E113: Unknown option: %s"), *arg);
5459 ret = FAIL;
5460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 if (opt_type == -2) /* hidden string option */
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 rettv->v_type = VAR_STRING;
5466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else if (opt_type == -1) /* hidden number option */
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 rettv->v_type = VAR_NUMBER;
5471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 else if (opt_type == 1) /* number option */
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 rettv->v_type = VAR_NUMBER;
5476 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 else /* string option */
5479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 rettv->v_type = VAR_STRING;
5481 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 }
5484 else if (working && (opt_type == -2 || opt_type == -1))
5485 ret = FAIL;
5486
5487 *option_end = c; /* put back for error messages */
5488 *arg = option_end;
5489
5490 return ret;
5491}
5492
5493/*
5494 * Allocate a variable for a string constant.
5495 * Return OK or FAIL.
5496 */
5497 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 int evaluate;
5502{
5503 char_u *p;
5504 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 int extra = 0;
5506
5507 /*
5508 * Find the end of the string, skipping backslashed characters.
5509 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005510 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
5512 if (*p == '\\' && p[1] != NUL)
5513 {
5514 ++p;
5515 /* A "\<x>" form occupies at least 4 characters, and produces up
5516 * to 6 characters: reserve space for 2 extra */
5517 if (*p == '<')
5518 extra += 2;
5519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
5521
5522 if (*p != '"')
5523 {
5524 EMSG2(_("E114: Missing quote: %s"), *arg);
5525 return FAIL;
5526 }
5527
5528 /* If only parsing, set *arg and return here */
5529 if (!evaluate)
5530 {
5531 *arg = p + 1;
5532 return OK;
5533 }
5534
5535 /*
5536 * Copy the string into allocated memory, handling backslashed
5537 * characters.
5538 */
5539 name = alloc((unsigned)(p - *arg + extra));
5540 if (name == NULL)
5541 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 rettv->v_type = VAR_STRING;
5543 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar8c711452005-01-14 21:53:12 +00005545 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
5547 if (*p == '\\')
5548 {
5549 switch (*++p)
5550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 case 'b': *name++ = BS; ++p; break;
5552 case 'e': *name++ = ESC; ++p; break;
5553 case 'f': *name++ = FF; ++p; break;
5554 case 'n': *name++ = NL; ++p; break;
5555 case 'r': *name++ = CAR; ++p; break;
5556 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 case 'X': /* hex: "\x1", "\x12" */
5559 case 'x':
5560 case 'u': /* Unicode: "\u0023" */
5561 case 'U':
5562 if (vim_isxdigit(p[1]))
5563 {
5564 int n, nr;
5565 int c = toupper(*p);
5566
5567 if (c == 'X')
5568 n = 2;
5569 else
5570 n = 4;
5571 nr = 0;
5572 while (--n >= 0 && vim_isxdigit(p[1]))
5573 {
5574 ++p;
5575 nr = (nr << 4) + hex2nr(*p);
5576 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578#ifdef FEAT_MBYTE
5579 /* For "\u" store the number according to
5580 * 'encoding'. */
5581 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 else
5584#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 break;
5588
5589 /* octal: "\1", "\12", "\123" */
5590 case '0':
5591 case '1':
5592 case '2':
5593 case '3':
5594 case '4':
5595 case '5':
5596 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 case '7': *name = *p++ - '0';
5598 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 *name = (*name << 3) + *p++ - '0';
5601 if (*p >= '0' && *p <= '7')
5602 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 break;
5606
5607 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (extra != 0)
5610 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break;
5613 }
5614 /* FALLTHROUGH */
5615
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 break;
5618 }
5619 }
5620 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 *arg = p + 1;
5626
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 return OK;
5628}
5629
5630/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 * Return OK or FAIL.
5633 */
5634 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 int evaluate;
5639{
5640 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005642 int reduce = 0;
5643
5644 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005646 */
5647 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005652 break;
5653 ++reduce;
5654 ++p;
5655 }
5656 }
5657
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 return FAIL;
5662 }
5663
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 if (!evaluate)
5666 {
5667 *arg = p + 1;
5668 return OK;
5669 }
5670
5671 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005673 */
5674 str = alloc((unsigned)((p - *arg) - reduce));
5675 if (str == NULL)
5676 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 rettv->v_type = VAR_STRING;
5678 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005679
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005685 break;
5686 ++p;
5687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005691 *arg = p + 1;
5692
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 return OK;
5694}
5695
5696/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 * Allocate a variable for a List and fill it from "*arg".
5698 * Return OK or FAIL.
5699 */
5700 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 int evaluate;
5705{
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 list_T *l = NULL;
5707 typval_T tv;
5708 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709
5710 if (evaluate)
5711 {
5712 l = list_alloc();
5713 if (l == NULL)
5714 return FAIL;
5715 }
5716
5717 *arg = skipwhite(*arg + 1);
5718 while (**arg != ']' && **arg != NUL)
5719 {
5720 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5721 goto failret;
5722 if (evaluate)
5723 {
5724 item = listitem_alloc();
5725 if (item != NULL)
5726 {
5727 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005728 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729 list_append(l, item);
5730 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005731 else
5732 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733 }
5734
5735 if (**arg == ']')
5736 break;
5737 if (**arg != ',')
5738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 goto failret;
5741 }
5742 *arg = skipwhite(*arg + 1);
5743 }
5744
5745 if (**arg != ']')
5746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748failret:
5749 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005750 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 return FAIL;
5752 }
5753
5754 *arg = skipwhite(*arg + 1);
5755 if (evaluate)
5756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005757 rettv->v_type = VAR_LIST;
5758 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 ++l->lv_refcount;
5760 }
5761
5762 return OK;
5763}
5764
5765/*
5766 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005767 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005769 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770list_alloc()
5771{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005772 list_T *l;
5773
5774 l = (list_T *)alloc_clear(sizeof(list_T));
5775 if (l != NULL)
5776 {
5777 /* Prepend the list to the list of lists for garbage collection. */
5778 if (first_list != NULL)
5779 first_list->lv_used_prev = l;
5780 l->lv_used_prev = NULL;
5781 l->lv_used_next = first_list;
5782 first_list = l;
5783 }
5784 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785}
5786
5787/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005788 * Allocate an empty list for a return value.
5789 * Returns OK or FAIL.
5790 */
5791 static int
5792rettv_list_alloc(rettv)
5793 typval_T *rettv;
5794{
5795 list_T *l = list_alloc();
5796
5797 if (l == NULL)
5798 return FAIL;
5799
5800 rettv->vval.v_list = l;
5801 rettv->v_type = VAR_LIST;
5802 ++l->lv_refcount;
5803 return OK;
5804}
5805
5806/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 * Unreference a list: decrement the reference count and free it when it
5808 * becomes zero.
5809 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005810 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005814 if (l != NULL && --l->lv_refcount <= 0)
5815 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816}
5817
5818/*
5819 * Free a list, including all items it points to.
5820 * Ignores the reference count.
5821 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005822 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005823list_free(l, recurse)
5824 list_T *l;
5825 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826{
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005829 /* Remove the list from the list of lists for garbage collection. */
5830 if (l->lv_used_prev == NULL)
5831 first_list = l->lv_used_next;
5832 else
5833 l->lv_used_prev->lv_used_next = l->lv_used_next;
5834 if (l->lv_used_next != NULL)
5835 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5836
Bram Moolenaard9fba312005-06-26 22:34:35 +00005837 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005839 /* Remove the item before deleting it. */
5840 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005841 if (recurse || (item->li_tv.v_type != VAR_LIST
5842 && item->li_tv.v_type != VAR_DICT))
5843 clear_tv(&item->li_tv);
5844 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 }
5846 vim_free(l);
5847}
5848
5849/*
5850 * Allocate a list item.
5851 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005852 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853listitem_alloc()
5854{
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856}
5857
5858/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005859 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 */
5861 static void
5862listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005865 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 vim_free(item);
5867}
5868
5869/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005870 * Remove a list item from a List and free it. Also clears the value.
5871 */
5872 static void
5873listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *l;
5875 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005876{
5877 list_remove(l, item, item);
5878 listitem_free(item);
5879}
5880
5881/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 * Get the number of items in a list.
5883 */
5884 static long
5885list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 if (l == NULL)
5889 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005890 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891}
5892
5893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005894 * Return TRUE when two lists have exactly the same values.
5895 */
5896 static int
5897list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *l1;
5899 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005900 int ic; /* ignore case for strings */
5901{
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005904 if (l1 == NULL || l2 == NULL)
5905 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005906 if (l1 == l2)
5907 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005908 if (list_len(l1) != list_len(l2))
5909 return FALSE;
5910
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005911 for (item1 = l1->lv_first, item2 = l2->lv_first;
5912 item1 != NULL && item2 != NULL;
5913 item1 = item1->li_next, item2 = item2->li_next)
5914 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5915 return FALSE;
5916 return item1 == NULL && item2 == NULL;
5917}
5918
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005919#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5920 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005921/*
5922 * Return the dictitem that an entry in a hashtable points to.
5923 */
5924 dictitem_T *
5925dict_lookup(hi)
5926 hashitem_T *hi;
5927{
5928 return HI2DI(hi);
5929}
5930#endif
5931
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005933 * Return TRUE when two dictionaries have exactly the same key/values.
5934 */
5935 static int
5936dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 dict_T *d1;
5938 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005939 int ic; /* ignore case for strings */
5940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 hashitem_T *hi;
5942 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005943 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005944
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005945 if (d1 == NULL || d2 == NULL)
5946 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005947 if (d1 == d2)
5948 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005949 if (dict_len(d1) != dict_len(d2))
5950 return FALSE;
5951
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005952 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005953 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005955 if (!HASHITEM_EMPTY(hi))
5956 {
5957 item2 = dict_find(d2, hi->hi_key, -1);
5958 if (item2 == NULL)
5959 return FALSE;
5960 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5961 return FALSE;
5962 --todo;
5963 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005964 }
5965 return TRUE;
5966}
5967
5968/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005970 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005971 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972 */
5973 static int
5974tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 typval_T *tv1;
5976 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005977 int ic; /* ignore case */
5978{
5979 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005980 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005981 static int recursive = 0; /* cach recursive loops */
5982 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005983
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005984 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005985 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005986 /* Catch lists and dicts that have an endless loop by limiting
5987 * recursiveness to 1000. We guess they are equal then. */
5988 if (recursive >= 1000)
5989 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005990
5991 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005992 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005993 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005994 ++recursive;
5995 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5996 --recursive;
5997 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005998
5999 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006000 ++recursive;
6001 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6002 --recursive;
6003 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006004
6005 case VAR_FUNC:
6006 return (tv1->vval.v_string != NULL
6007 && tv2->vval.v_string != NULL
6008 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6009
6010 case VAR_NUMBER:
6011 return tv1->vval.v_number == tv2->vval.v_number;
6012
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006013#ifdef FEAT_FLOAT
6014 case VAR_FLOAT:
6015 return tv1->vval.v_float == tv2->vval.v_float;
6016#endif
6017
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006018 case VAR_STRING:
6019 s1 = get_tv_string_buf(tv1, buf1);
6020 s2 = get_tv_string_buf(tv2, buf2);
6021 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006022 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006023
6024 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 return TRUE;
6026}
6027
6028/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 * Locate item with index "n" in list "l" and return it.
6030 * A negative index is counted from the end; -1 is the last item.
6031 * Returns NULL when "n" is out of range.
6032 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 long n;
6037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 long idx;
6040
6041 if (l == NULL)
6042 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006043
6044 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006046 n = l->lv_len + n;
6047
6048 /* Check for index out of range. */
6049 if (n < 0 || n >= l->lv_len)
6050 return NULL;
6051
6052 /* When there is a cached index may start search from there. */
6053 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006055 if (n < l->lv_idx / 2)
6056 {
6057 /* closest to the start of the list */
6058 item = l->lv_first;
6059 idx = 0;
6060 }
6061 else if (n > (l->lv_idx + l->lv_len) / 2)
6062 {
6063 /* closest to the end of the list */
6064 item = l->lv_last;
6065 idx = l->lv_len - 1;
6066 }
6067 else
6068 {
6069 /* closest to the cached index */
6070 item = l->lv_idx_item;
6071 idx = l->lv_idx;
6072 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 }
6074 else
6075 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006076 if (n < l->lv_len / 2)
6077 {
6078 /* closest to the start of the list */
6079 item = l->lv_first;
6080 idx = 0;
6081 }
6082 else
6083 {
6084 /* closest to the end of the list */
6085 item = l->lv_last;
6086 idx = l->lv_len - 1;
6087 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006089
6090 while (n > idx)
6091 {
6092 /* search forward */
6093 item = item->li_next;
6094 ++idx;
6095 }
6096 while (n < idx)
6097 {
6098 /* search backward */
6099 item = item->li_prev;
6100 --idx;
6101 }
6102
6103 /* cache the used index */
6104 l->lv_idx = idx;
6105 l->lv_idx_item = item;
6106
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 return item;
6108}
6109
6110/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006111 * Get list item "l[idx]" as a number.
6112 */
6113 static long
6114list_find_nr(l, idx, errorp)
6115 list_T *l;
6116 long idx;
6117 int *errorp; /* set to TRUE when something wrong */
6118{
6119 listitem_T *li;
6120
6121 li = list_find(l, idx);
6122 if (li == NULL)
6123 {
6124 if (errorp != NULL)
6125 *errorp = TRUE;
6126 return -1L;
6127 }
6128 return get_tv_number_chk(&li->li_tv, errorp);
6129}
6130
6131/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006132 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6133 */
6134 char_u *
6135list_find_str(l, idx)
6136 list_T *l;
6137 long idx;
6138{
6139 listitem_T *li;
6140
6141 li = list_find(l, idx - 1);
6142 if (li == NULL)
6143 {
6144 EMSGN(_(e_listidx), idx);
6145 return NULL;
6146 }
6147 return get_tv_string(&li->li_tv);
6148}
6149
6150/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006151 * Locate "item" list "l" and return its index.
6152 * Returns -1 when "item" is not in the list.
6153 */
6154 static long
6155list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 list_T *l;
6157 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006158{
6159 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006161
6162 if (l == NULL)
6163 return -1;
6164 idx = 0;
6165 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6166 ++idx;
6167 if (li == NULL)
6168 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006169 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Append item "item" to the end of list "l".
6174 */
6175 static void
6176list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 list_T *l;
6178 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179{
6180 if (l->lv_last == NULL)
6181 {
6182 /* empty list */
6183 l->lv_first = item;
6184 l->lv_last = item;
6185 item->li_prev = NULL;
6186 }
6187 else
6188 {
6189 l->lv_last->li_next = item;
6190 item->li_prev = l->lv_last;
6191 l->lv_last = item;
6192 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006193 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194 item->li_next = NULL;
6195}
6196
6197/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006201 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 list_T *l;
6204 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006206 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006207
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006210 copy_tv(tv, &li->li_tv);
6211 list_append(l, li);
6212 return OK;
6213}
6214
6215/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006216 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006217 * Return FAIL when out of memory.
6218 */
6219 int
6220list_append_dict(list, dict)
6221 list_T *list;
6222 dict_T *dict;
6223{
6224 listitem_T *li = listitem_alloc();
6225
6226 if (li == NULL)
6227 return FAIL;
6228 li->li_tv.v_type = VAR_DICT;
6229 li->li_tv.v_lock = 0;
6230 li->li_tv.vval.v_dict = dict;
6231 list_append(list, li);
6232 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233 return OK;
6234}
6235
6236/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006237 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006238 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006239 * Returns FAIL when out of memory.
6240 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006241 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006242list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006243 list_T *l;
6244 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006245 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006246{
6247 listitem_T *li = listitem_alloc();
6248
6249 if (li == NULL)
6250 return FAIL;
6251 list_append(l, li);
6252 li->li_tv.v_type = VAR_STRING;
6253 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006254 if (str == NULL)
6255 li->li_tv.vval.v_string = NULL;
6256 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006257 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006258 return FAIL;
6259 return OK;
6260}
6261
6262/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006263 * Append "n" to list "l".
6264 * Returns FAIL when out of memory.
6265 */
6266 static int
6267list_append_number(l, n)
6268 list_T *l;
6269 varnumber_T n;
6270{
6271 listitem_T *li;
6272
6273 li = listitem_alloc();
6274 if (li == NULL)
6275 return FAIL;
6276 li->li_tv.v_type = VAR_NUMBER;
6277 li->li_tv.v_lock = 0;
6278 li->li_tv.vval.v_number = n;
6279 list_append(l, li);
6280 return OK;
6281}
6282
6283/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285 * If "item" is NULL append at the end.
6286 * Return FAIL when out of memory.
6287 */
6288 static int
6289list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 list_T *l;
6291 typval_T *tv;
6292 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006293{
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006295
6296 if (ni == NULL)
6297 return FAIL;
6298 copy_tv(tv, &ni->li_tv);
6299 if (item == NULL)
6300 /* Append new item at end of list. */
6301 list_append(l, ni);
6302 else
6303 {
6304 /* Insert new item before existing item. */
6305 ni->li_prev = item->li_prev;
6306 ni->li_next = item;
6307 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 ++l->lv_idx;
6311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006315 l->lv_idx_item = NULL;
6316 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006317 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006318 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006319 }
6320 return OK;
6321}
6322
6323/*
6324 * Extend "l1" with "l2".
6325 * If "bef" is NULL append at the end, otherwise insert before this item.
6326 * Returns FAIL when out of memory.
6327 */
6328 static int
6329list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006330 list_T *l1;
6331 list_T *l2;
6332 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333{
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006335 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006337 /* We also quit the loop when we have inserted the original item count of
6338 * the list, avoid a hang when we extend a list with itself. */
6339 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006340 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6341 return FAIL;
6342 return OK;
6343}
6344
6345/*
6346 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6347 * Return FAIL when out of memory.
6348 */
6349 static int
6350list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 list_T *l1;
6352 list_T *l2;
6353 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006354{
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006356
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006357 if (l1 == NULL || l2 == NULL)
6358 return FAIL;
6359
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006361 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362 if (l == NULL)
6363 return FAIL;
6364 tv->v_type = VAR_LIST;
6365 tv->vval.v_list = l;
6366
6367 /* append all items from the second list */
6368 return list_extend(l, l2, NULL);
6369}
6370
6371/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006374 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 * Returns NULL when out of memory.
6376 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006381 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382{
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 list_T *copy;
6384 listitem_T *item;
6385 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386
6387 if (orig == NULL)
6388 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389
6390 copy = list_alloc();
6391 if (copy != NULL)
6392 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006393 if (copyID != 0)
6394 {
6395 /* Do this before adding the items, because one of the items may
6396 * refer back to this list. */
6397 orig->lv_copyID = copyID;
6398 orig->lv_copylist = copy;
6399 }
6400 for (item = orig->lv_first; item != NULL && !got_int;
6401 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 {
6403 ni = listitem_alloc();
6404 if (ni == NULL)
6405 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006406 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006407 {
6408 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6409 {
6410 vim_free(ni);
6411 break;
6412 }
6413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006415 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 list_append(copy, ni);
6417 }
6418 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006419 if (item != NULL)
6420 {
6421 list_unref(copy);
6422 copy = NULL;
6423 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 }
6425
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return copy;
6427}
6428
6429/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006431 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006434list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 list_T *l;
6436 listitem_T *item;
6437 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438{
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 /* notify watchers */
6442 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006444 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445 list_fix_watch(l, ip);
6446 if (ip == item2)
6447 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449
6450 if (item2->li_next == NULL)
6451 l->lv_last = item->li_prev;
6452 else
6453 item2->li_next->li_prev = item->li_prev;
6454 if (item->li_prev == NULL)
6455 l->lv_first = item2->li_next;
6456 else
6457 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459}
6460
6461/*
6462 * Return an allocated string with the string representation of a list.
6463 * May return NULL.
6464 */
6465 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006466list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006468 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469{
6470 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471
6472 if (tv->vval.v_list == NULL)
6473 return NULL;
6474 ga_init2(&ga, (int)sizeof(char), 80);
6475 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006476 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006477 {
6478 vim_free(ga.ga_data);
6479 return NULL;
6480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 ga_append(&ga, ']');
6482 ga_append(&ga, NUL);
6483 return (char_u *)ga.ga_data;
6484}
6485
6486/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006487 * Join list "l" into a string in "*gap", using separator "sep".
6488 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006495 char_u *sep;
6496 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006497 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498{
6499 int first = TRUE;
6500 char_u *tofree;
6501 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006503 char_u *s;
6504
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006506 {
6507 if (first)
6508 first = FALSE;
6509 else
6510 ga_concat(gap, sep);
6511
6512 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006513 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006514 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006515 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006516 if (s != NULL)
6517 ga_concat(gap, s);
6518 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 if (s == NULL)
6520 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006521 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006522 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006523 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006524}
6525
6526/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006527 * Garbage collection for lists and dictionaries.
6528 *
6529 * We use reference counts to be able to free most items right away when they
6530 * are no longer used. But for composite items it's possible that it becomes
6531 * unused while the reference count is > 0: When there is a recursive
6532 * reference. Example:
6533 * :let l = [1, 2, 3]
6534 * :let d = {9: l}
6535 * :let l[1] = d
6536 *
6537 * Since this is quite unusual we handle this with garbage collection: every
6538 * once in a while find out which lists and dicts are not referenced from any
6539 * variable.
6540 *
6541 * Here is a good reference text about garbage collection (refers to Python
6542 * but it applies to all reference-counting mechanisms):
6543 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006544 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006545
6546/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006547 * Do garbage collection for lists and dicts.
6548 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006549 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006550 int
6551garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006552{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006553 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006554 buf_T *buf;
6555 win_T *wp;
6556 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006557 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006558 int did_free;
6559 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006560#ifdef FEAT_WINDOWS
6561 tabpage_T *tp;
6562#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006563
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006564 /* Only do this once. */
6565 want_garbage_collect = FALSE;
6566 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006567 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006568
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006569 /* We advance by two because we add one for items referenced through
6570 * previous_funccal. */
6571 current_copyID += COPYID_INC;
6572 copyID = current_copyID;
6573
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 /*
6575 * 1. Go through all accessible variables and mark all lists and dicts
6576 * with copyID.
6577 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006578
6579 /* Don't free variables in the previous_funccal list unless they are only
6580 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006581 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006582 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6583 {
6584 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6585 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6586 }
6587
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006588 /* script-local variables */
6589 for (i = 1; i <= ga_scripts.ga_len; ++i)
6590 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6591
6592 /* buffer-local variables */
6593 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6594 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6595
6596 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006597 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006598 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6599
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006600#ifdef FEAT_WINDOWS
6601 /* tabpage-local variables */
6602 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6603 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6604#endif
6605
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006606 /* global variables */
6607 set_ref_in_ht(&globvarht, copyID);
6608
6609 /* function-local variables */
6610 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6611 {
6612 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6613 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6614 }
6615
Bram Moolenaard812df62008-11-09 12:46:09 +00006616 /* v: vars */
6617 set_ref_in_ht(&vimvarht, copyID);
6618
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006619 /*
6620 * 2. Free lists and dictionaries that are not referenced.
6621 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006622 did_free = free_unref_items(copyID);
6623
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006624 /*
6625 * 3. Check if any funccal can be freed now.
6626 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006627 for (pfc = &previous_funccal; *pfc != NULL; )
6628 {
6629 if (can_free_funccal(*pfc, copyID))
6630 {
6631 fc = *pfc;
6632 *pfc = fc->caller;
6633 free_funccal(fc, TRUE);
6634 did_free = TRUE;
6635 did_free_funccal = TRUE;
6636 }
6637 else
6638 pfc = &(*pfc)->caller;
6639 }
6640 if (did_free_funccal)
6641 /* When a funccal was freed some more items might be garbage
6642 * collected, so run again. */
6643 (void)garbage_collect();
6644
6645 return did_free;
6646}
6647
6648/*
6649 * Free lists and dictionaries that are no longer referenced.
6650 */
6651 static int
6652free_unref_items(copyID)
6653 int copyID;
6654{
6655 dict_T *dd;
6656 list_T *ll;
6657 int did_free = FALSE;
6658
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006660 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006661 */
6662 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006663 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006664 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006665 /* Free the Dictionary and ordinary items it contains, but don't
6666 * recurse into Lists and Dictionaries, they will be in the list
6667 * of dicts or list of lists. */
6668 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006669 did_free = TRUE;
6670
6671 /* restart, next dict may also have been freed */
6672 dd = first_dict;
6673 }
6674 else
6675 dd = dd->dv_used_next;
6676
6677 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006678 * Go through the list of lists and free items without the copyID.
6679 * But don't free a list that has a watcher (used in a for loop), these
6680 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006681 */
6682 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006683 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6684 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006685 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006686 /* Free the List and ordinary items it contains, but don't recurse
6687 * into Lists and Dictionaries, they will be in the list of dicts
6688 * or list of lists. */
6689 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006690 did_free = TRUE;
6691
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006692 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006693 ll = first_list;
6694 }
6695 else
6696 ll = ll->lv_used_next;
6697
6698 return did_free;
6699}
6700
6701/*
6702 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6703 */
6704 static void
6705set_ref_in_ht(ht, copyID)
6706 hashtab_T *ht;
6707 int copyID;
6708{
6709 int todo;
6710 hashitem_T *hi;
6711
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006712 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006713 for (hi = ht->ht_array; todo > 0; ++hi)
6714 if (!HASHITEM_EMPTY(hi))
6715 {
6716 --todo;
6717 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6718 }
6719}
6720
6721/*
6722 * Mark all lists and dicts referenced through list "l" with "copyID".
6723 */
6724 static void
6725set_ref_in_list(l, copyID)
6726 list_T *l;
6727 int copyID;
6728{
6729 listitem_T *li;
6730
6731 for (li = l->lv_first; li != NULL; li = li->li_next)
6732 set_ref_in_item(&li->li_tv, copyID);
6733}
6734
6735/*
6736 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6737 */
6738 static void
6739set_ref_in_item(tv, copyID)
6740 typval_T *tv;
6741 int copyID;
6742{
6743 dict_T *dd;
6744 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745
6746 switch (tv->v_type)
6747 {
6748 case VAR_DICT:
6749 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006750 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006751 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752 /* Didn't see this dict yet. */
6753 dd->dv_copyID = copyID;
6754 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006757
6758 case VAR_LIST:
6759 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006760 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762 /* Didn't see this list yet. */
6763 ll->lv_copyID = copyID;
6764 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769}
6770
6771/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006772 * Allocate an empty header for a dictionary.
6773 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006774 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775dict_alloc()
6776{
Bram Moolenaar33570922005-01-25 22:26:29 +00006777 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006778
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006780 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006781 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006782 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783 if (first_dict != NULL)
6784 first_dict->dv_used_prev = d;
6785 d->dv_used_next = first_dict;
6786 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006787 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788
Bram Moolenaar33570922005-01-25 22:26:29 +00006789 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006790 d->dv_lock = 0;
6791 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006792 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006793 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006794 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795}
6796
6797/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006798 * Allocate an empty dict for a return value.
6799 * Returns OK or FAIL.
6800 */
6801 static int
6802rettv_dict_alloc(rettv)
6803 typval_T *rettv;
6804{
6805 dict_T *d = dict_alloc();
6806
6807 if (d == NULL)
6808 return FAIL;
6809
6810 rettv->vval.v_dict = d;
6811 rettv->v_type = VAR_DICT;
6812 ++d->dv_refcount;
6813 return OK;
6814}
6815
6816
6817/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818 * Unreference a Dictionary: decrement the reference count and free it when it
6819 * becomes zero.
6820 */
6821 static void
6822dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006823 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006825 if (d != NULL && --d->dv_refcount <= 0)
6826 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006827}
6828
6829/*
6830 * Free a Dictionary, including all items it contains.
6831 * Ignores the reference count.
6832 */
6833 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006834dict_free(d, recurse)
6835 dict_T *d;
6836 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006838 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006841
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 /* Remove the dict from the list of dicts for garbage collection. */
6843 if (d->dv_used_prev == NULL)
6844 first_dict = d->dv_used_next;
6845 else
6846 d->dv_used_prev->dv_used_next = d->dv_used_next;
6847 if (d->dv_used_next != NULL)
6848 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6849
6850 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006851 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006852 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006853 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006855 if (!HASHITEM_EMPTY(hi))
6856 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006857 /* Remove the item before deleting it, just in case there is
6858 * something recursive causing trouble. */
6859 di = HI2DI(hi);
6860 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006861 if (recurse || (di->di_tv.v_type != VAR_LIST
6862 && di->di_tv.v_type != VAR_DICT))
6863 clear_tv(&di->di_tv);
6864 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006865 --todo;
6866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006867 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006869 vim_free(d);
6870}
6871
6872/*
6873 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006874 * The "key" is copied to the new item.
6875 * Note that the value of the item "di_tv" still needs to be initialized!
6876 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006877 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006878 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006879dictitem_alloc(key)
6880 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006881{
Bram Moolenaar33570922005-01-25 22:26:29 +00006882 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006884 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006886 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006887 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006888 di->di_flags = 0;
6889 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006890 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891}
6892
6893/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006894 * Make a copy of a Dictionary item.
6895 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899{
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006902 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6903 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006904 if (di != NULL)
6905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006906 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006907 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006908 copy_tv(&org->di_tv, &di->di_tv);
6909 }
6910 return di;
6911}
6912
6913/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006914 * Remove item "item" from Dictionary "dict" and free it.
6915 */
6916 static void
6917dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 dict_T *dict;
6919 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006920{
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922
Bram Moolenaar33570922005-01-25 22:26:29 +00006923 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006924 if (HASHITEM_EMPTY(hi))
6925 EMSG2(_(e_intern2), "dictitem_remove()");
6926 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006927 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006928 dictitem_free(item);
6929}
6930
6931/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932 * Free a dict item. Also clears the value.
6933 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006934 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006937{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006938 clear_tv(&item->di_tv);
6939 vim_free(item);
6940}
6941
6942/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006943 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6944 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006945 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006946 * Returns NULL when out of memory.
6947 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006949dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006952 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953{
Bram Moolenaar33570922005-01-25 22:26:29 +00006954 dict_T *copy;
6955 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006956 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006958
6959 if (orig == NULL)
6960 return NULL;
6961
6962 copy = dict_alloc();
6963 if (copy != NULL)
6964 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006965 if (copyID != 0)
6966 {
6967 orig->dv_copyID = copyID;
6968 orig->dv_copydict = copy;
6969 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006970 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006971 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006973 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 --todo;
6976
6977 di = dictitem_alloc(hi->hi_key);
6978 if (di == NULL)
6979 break;
6980 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006981 {
6982 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6983 copyID) == FAIL)
6984 {
6985 vim_free(di);
6986 break;
6987 }
6988 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006989 else
6990 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6991 if (dict_add(copy, di) == FAIL)
6992 {
6993 dictitem_free(di);
6994 break;
6995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006998
Bram Moolenaare9a41262005-01-15 22:18:47 +00006999 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007000 if (todo > 0)
7001 {
7002 dict_unref(copy);
7003 copy = NULL;
7004 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007005 }
7006
7007 return copy;
7008}
7009
7010/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007012 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007014 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007016 dict_T *d;
7017 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007018{
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020}
7021
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007023 * Add a number or string entry to dictionary "d".
7024 * When "str" is NULL use number "nr", otherwise use "str".
7025 * Returns FAIL when out of memory and when key already exists.
7026 */
7027 int
7028dict_add_nr_str(d, key, nr, str)
7029 dict_T *d;
7030 char *key;
7031 long nr;
7032 char_u *str;
7033{
7034 dictitem_T *item;
7035
7036 item = dictitem_alloc((char_u *)key);
7037 if (item == NULL)
7038 return FAIL;
7039 item->di_tv.v_lock = 0;
7040 if (str == NULL)
7041 {
7042 item->di_tv.v_type = VAR_NUMBER;
7043 item->di_tv.vval.v_number = nr;
7044 }
7045 else
7046 {
7047 item->di_tv.v_type = VAR_STRING;
7048 item->di_tv.vval.v_string = vim_strsave(str);
7049 }
7050 if (dict_add(d, item) == FAIL)
7051 {
7052 dictitem_free(item);
7053 return FAIL;
7054 }
7055 return OK;
7056}
7057
7058/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007059 * Add a list entry to dictionary "d".
7060 * Returns FAIL when out of memory and when key already exists.
7061 */
7062 int
7063dict_add_list(d, key, list)
7064 dict_T *d;
7065 char *key;
7066 list_T *list;
7067{
7068 dictitem_T *item;
7069
7070 item = dictitem_alloc((char_u *)key);
7071 if (item == NULL)
7072 return FAIL;
7073 item->di_tv.v_lock = 0;
7074 item->di_tv.v_type = VAR_LIST;
7075 item->di_tv.vval.v_list = list;
7076 if (dict_add(d, item) == FAIL)
7077 {
7078 dictitem_free(item);
7079 return FAIL;
7080 }
7081 return OK;
7082}
7083
7084/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007085 * Get the number of items in a Dictionary.
7086 */
7087 static long
7088dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007089 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091 if (d == NULL)
7092 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007093 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094}
7095
7096/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097 * Find item "key[len]" in Dictionary "d".
7098 * If "len" is negative use strlen(key).
7099 * Returns NULL when not found.
7100 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007101 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 char_u *key;
7105 int len;
7106{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107#define AKEYLEN 200
7108 char_u buf[AKEYLEN];
7109 char_u *akey;
7110 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 if (len < 0)
7114 akey = key;
7115 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 tofree = akey = vim_strnsave(key, len);
7118 if (akey == NULL)
7119 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007120 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 else
7122 {
7123 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007124 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 akey = buf;
7126 }
7127
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 vim_free(tofree);
7130 if (HASHITEM_EMPTY(hi))
7131 return NULL;
7132 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133}
7134
7135/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007136 * Get a string item from a dictionary.
7137 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007138 * Returns NULL if the entry doesn't exist or out of memory.
7139 */
7140 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007141get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007142 dict_T *d;
7143 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007144 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007145{
7146 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007147 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007148
7149 di = dict_find(d, key, -1);
7150 if (di == NULL)
7151 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007152 s = get_tv_string(&di->di_tv);
7153 if (save && s != NULL)
7154 s = vim_strsave(s);
7155 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007156}
7157
7158/*
7159 * Get a number item from a dictionary.
7160 * Returns 0 if the entry doesn't exist or out of memory.
7161 */
7162 long
7163get_dict_number(d, key)
7164 dict_T *d;
7165 char_u *key;
7166{
7167 dictitem_T *di;
7168
7169 di = dict_find(d, key, -1);
7170 if (di == NULL)
7171 return 0;
7172 return get_tv_number(&di->di_tv);
7173}
7174
7175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 * Return an allocated string with the string representation of a Dictionary.
7177 * May return NULL.
7178 */
7179 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007180dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007182 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183{
7184 garray_T ga;
7185 int first = TRUE;
7186 char_u *tofree;
7187 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194 return NULL;
7195 ga_init2(&ga, (int)sizeof(char), 80);
7196 ga_append(&ga, '{');
7197
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007198 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 --todo;
7204
7205 if (first)
7206 first = FALSE;
7207 else
7208 ga_concat(&ga, (char_u *)", ");
7209
7210 tofree = string_quote(hi->hi_key, FALSE);
7211 if (tofree != NULL)
7212 {
7213 ga_concat(&ga, tofree);
7214 vim_free(tofree);
7215 }
7216 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007217 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 if (s != NULL)
7219 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 if (s == NULL)
7222 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007225 if (todo > 0)
7226 {
7227 vim_free(ga.ga_data);
7228 return NULL;
7229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230
7231 ga_append(&ga, '}');
7232 ga_append(&ga, NUL);
7233 return (char_u *)ga.ga_data;
7234}
7235
7236/*
7237 * Allocate a variable for a Dictionary and fill it from "*arg".
7238 * Return OK or FAIL. Returns NOTDONE for {expr}.
7239 */
7240 static int
7241get_dict_tv(arg, rettv, evaluate)
7242 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 int evaluate;
7245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 dict_T *d = NULL;
7247 typval_T tvkey;
7248 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007249 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253
7254 /*
7255 * First check if it's not a curly-braces thing: {expr}.
7256 * Must do this without evaluating, otherwise a function may be called
7257 * twice. Unfortunately this means we need to call eval1() twice for the
7258 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 if (*start != '}')
7262 {
7263 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7264 return FAIL;
7265 if (*start == '}')
7266 return NOTDONE;
7267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268
7269 if (evaluate)
7270 {
7271 d = dict_alloc();
7272 if (d == NULL)
7273 return FAIL;
7274 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 tvkey.v_type = VAR_UNKNOWN;
7276 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277
7278 *arg = skipwhite(*arg + 1);
7279 while (**arg != '}' && **arg != NUL)
7280 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 goto failret;
7283 if (**arg != ':')
7284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007285 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 goto failret;
7288 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007289 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007291 key = get_tv_string_buf_chk(&tvkey, buf);
7292 if (key == NULL || *key == NUL)
7293 {
7294 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7295 if (key != NULL)
7296 EMSG(_(e_emptykey));
7297 clear_tv(&tvkey);
7298 goto failret;
7299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301
7302 *arg = skipwhite(*arg + 1);
7303 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7304 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007305 if (evaluate)
7306 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 goto failret;
7308 }
7309 if (evaluate)
7310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 if (item != NULL)
7313 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007314 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 clear_tv(&tv);
7317 goto failret;
7318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 item = dictitem_alloc(key);
7320 clear_tv(&tvkey);
7321 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if (dict_add(d, item) == FAIL)
7326 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 }
7328 }
7329
7330 if (**arg == '}')
7331 break;
7332 if (**arg != ',')
7333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007334 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 goto failret;
7336 }
7337 *arg = skipwhite(*arg + 1);
7338 }
7339
7340 if (**arg != '}')
7341 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007342 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343failret:
7344 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007345 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 return FAIL;
7347 }
7348
7349 *arg = skipwhite(*arg + 1);
7350 if (evaluate)
7351 {
7352 rettv->v_type = VAR_DICT;
7353 rettv->vval.v_dict = d;
7354 ++d->dv_refcount;
7355 }
7356
7357 return OK;
7358}
7359
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007361 * Return a string with the string representation of a variable.
7362 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007363 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007364 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007365 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007366 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007367 */
7368 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007369echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007371 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007372 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007373 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007374{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 static int recurse = 0;
7376 char_u *r = NULL;
7377
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007380 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 *tofree = NULL;
7382 return NULL;
7383 }
7384 ++recurse;
7385
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 switch (tv->v_type)
7387 {
7388 case VAR_FUNC:
7389 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 r = tv->vval.v_string;
7391 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007394 if (tv->vval.v_list == NULL)
7395 {
7396 *tofree = NULL;
7397 r = NULL;
7398 }
7399 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7400 {
7401 *tofree = NULL;
7402 r = (char_u *)"[...]";
7403 }
7404 else
7405 {
7406 tv->vval.v_list->lv_copyID = copyID;
7407 *tofree = list2string(tv, copyID);
7408 r = *tofree;
7409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413 if (tv->vval.v_dict == NULL)
7414 {
7415 *tofree = NULL;
7416 r = NULL;
7417 }
7418 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7419 {
7420 *tofree = NULL;
7421 r = (char_u *)"{...}";
7422 }
7423 else
7424 {
7425 tv->vval.v_dict->dv_copyID = copyID;
7426 *tofree = dict2string(tv, copyID);
7427 r = *tofree;
7428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 case VAR_STRING:
7432 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 *tofree = NULL;
7434 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007436
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007437#ifdef FEAT_FLOAT
7438 case VAR_FLOAT:
7439 *tofree = NULL;
7440 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7441 r = numbuf;
7442 break;
7443#endif
7444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007446 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449
7450 --recurse;
7451 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007452}
7453
7454/*
7455 * Return a string with the string representation of a variable.
7456 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7457 * "numbuf" is used for a number.
7458 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007459 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007460 */
7461 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007462tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007463 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007464 char_u **tofree;
7465 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007466 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007467{
7468 switch (tv->v_type)
7469 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007470 case VAR_FUNC:
7471 *tofree = string_quote(tv->vval.v_string, TRUE);
7472 return *tofree;
7473 case VAR_STRING:
7474 *tofree = string_quote(tv->vval.v_string, FALSE);
7475 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007476#ifdef FEAT_FLOAT
7477 case VAR_FLOAT:
7478 *tofree = NULL;
7479 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7480 return numbuf;
7481#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007483 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007486 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007487 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007488 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007489 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007490}
7491
7492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 * Return string "str" in ' quotes, doubling ' characters.
7494 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007496 */
7497 static char_u *
7498string_quote(str, function)
7499 char_u *str;
7500 int function;
7501{
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007503 char_u *p, *r, *s;
7504
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 len = (function ? 13 : 3);
7506 if (str != NULL)
7507 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007508 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 for (p = str; *p != NUL; mb_ptr_adv(p))
7510 if (*p == '\'')
7511 ++len;
7512 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007513 s = r = alloc(len);
7514 if (r != NULL)
7515 {
7516 if (function)
7517 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007519 r += 10;
7520 }
7521 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 if (str != NULL)
7524 for (p = str; *p != NUL; )
7525 {
7526 if (*p == '\'')
7527 *r++ = '\'';
7528 MB_COPY_CHAR(p, r);
7529 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007531 if (function)
7532 *r++ = ')';
7533 *r++ = NUL;
7534 }
7535 return s;
7536}
7537
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007538#ifdef FEAT_FLOAT
7539/*
7540 * Convert the string "text" to a floating point number.
7541 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7542 * this always uses a decimal point.
7543 * Returns the length of the text that was consumed.
7544 */
7545 static int
7546string2float(text, value)
7547 char_u *text;
7548 float_T *value; /* result stored here */
7549{
7550 char *s = (char *)text;
7551 float_T f;
7552
7553 f = strtod(s, &s);
7554 *value = f;
7555 return (int)((char_u *)s - text);
7556}
7557#endif
7558
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 * Get the value of an environment variable.
7561 * "arg" is pointing to the '$'. It is advanced to after the name.
7562 * If the environment variable was not set, silently assume it is empty.
7563 * Always return OK.
7564 */
7565 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007566get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 int evaluate;
7570{
7571 char_u *string = NULL;
7572 int len;
7573 int cc;
7574 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007575 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
7577 ++*arg;
7578 name = *arg;
7579 len = get_env_len(arg);
7580 if (evaluate)
7581 {
7582 if (len != 0)
7583 {
7584 cc = name[len];
7585 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007586 /* first try vim_getenv(), fast for normal environment vars */
7587 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007589 {
7590 if (!mustfree)
7591 string = vim_strsave(string);
7592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 else
7594 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007595 if (mustfree)
7596 vim_free(string);
7597
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 /* next try expanding things like $VIM and ${HOME} */
7599 string = expand_env_save(name - 1);
7600 if (string != NULL && *string == '$')
7601 {
7602 vim_free(string);
7603 string = NULL;
7604 }
7605 }
7606 name[len] = cc;
7607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 rettv->v_type = VAR_STRING;
7609 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
7611
7612 return OK;
7613}
7614
7615/*
7616 * Array with names and number of arguments of all internal functions
7617 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7618 */
7619static struct fst
7620{
7621 char *f_name; /* function name */
7622 char f_min_argc; /* minimal number of arguments */
7623 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007625 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626} functions[] =
7627{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007628#ifdef FEAT_FLOAT
7629 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007630 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007631#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007632 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {"append", 2, 2, f_append},
7634 {"argc", 0, 0, f_argc},
7635 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007636 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007637#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007638 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007639 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007640 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007643 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {"bufexists", 1, 1, f_bufexists},
7645 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7646 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7647 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7648 {"buflisted", 1, 1, f_buflisted},
7649 {"bufloaded", 1, 1, f_bufloaded},
7650 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007651 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"bufwinnr", 1, 1, f_bufwinnr},
7653 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007654 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007656#ifdef FEAT_FLOAT
7657 {"ceil", 1, 1, f_ceil},
7658#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007659 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {"char2nr", 1, 1, f_char2nr},
7661 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007662 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007664#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007665 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007666 {"complete_add", 1, 1, f_complete_add},
7667 {"complete_check", 0, 0, f_complete_check},
7668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671#ifdef FEAT_FLOAT
7672 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007673 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007674#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007677 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007678 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"delete", 1, 1, f_delete},
7680 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007681 {"diff_filler", 1, 1, f_diff_filler},
7682 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007683 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007685 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {"eventhandler", 0, 0, f_eventhandler},
7687 {"executable", 1, 1, f_executable},
7688 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007689#ifdef FEAT_FLOAT
7690 {"exp", 1, 1, f_exp},
7691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007693 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007694 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7696 {"filereadable", 1, 1, f_filereadable},
7697 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007699 {"finddir", 1, 3, f_finddir},
7700 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007701#ifdef FEAT_FLOAT
7702 {"float2nr", 1, 1, f_float2nr},
7703 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007704 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007705#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007706 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"fnamemodify", 2, 2, f_fnamemodify},
7708 {"foldclosed", 1, 1, f_foldclosed},
7709 {"foldclosedend", 1, 1, f_foldclosedend},
7710 {"foldlevel", 1, 1, f_foldlevel},
7711 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007712 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007714 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007715 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007716 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007717 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {"getbufvar", 2, 2, f_getbufvar},
7719 {"getchar", 0, 1, f_getchar},
7720 {"getcharmod", 0, 0, f_getcharmod},
7721 {"getcmdline", 0, 0, f_getcmdline},
7722 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007723 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007725 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007726 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {"getfsize", 1, 1, f_getfsize},
7728 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007729 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007730 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007731 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007732 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007733 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007734 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007735 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007736 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007738 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007739 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {"getwinposx", 0, 0, f_getwinposx},
7741 {"getwinposy", 0, 0, f_getwinposy},
7742 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007743 {"glob", 1, 2, f_glob},
7744 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007746 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007747 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007748 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7750 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7751 {"histadd", 2, 2, f_histadd},
7752 {"histdel", 1, 2, f_histdel},
7753 {"histget", 1, 2, f_histget},
7754 {"histnr", 1, 1, f_histnr},
7755 {"hlID", 1, 1, f_hlID},
7756 {"hlexists", 1, 1, f_hlexists},
7757 {"hostname", 0, 0, f_hostname},
7758 {"iconv", 3, 3, f_iconv},
7759 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007760 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007761 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007763 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {"inputrestore", 0, 0, f_inputrestore},
7765 {"inputsave", 0, 0, f_inputsave},
7766 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007769 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {"libcall", 3, 3, f_libcall},
7776 {"libcallnr", 3, 3, f_libcallnr},
7777 {"line", 1, 1, f_line},
7778 {"line2byte", 1, 1, f_line2byte},
7779 {"lispindent", 1, 1, f_lispindent},
7780 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007781#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007782 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783 {"log10", 1, 1, f_log10},
7784#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007785 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007786 {"maparg", 1, 3, f_maparg},
7787 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007788 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007789 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007790 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007791 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007792 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007793 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007794 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007795 {"max", 1, 1, f_max},
7796 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007797#ifdef vim_mkdir
7798 {"mkdir", 1, 3, f_mkdir},
7799#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007800 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007801#ifdef FEAT_MZSCHEME
7802 {"mzeval", 1, 1, f_mzeval},
7803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {"nextnonblank", 1, 1, f_nextnonblank},
7805 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007806 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007807#ifdef FEAT_FLOAT
7808 {"pow", 2, 2, f_pow},
7809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007811 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007812 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007813 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007814 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007815 {"reltime", 0, 2, f_reltime},
7816 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"remote_expr", 2, 3, f_remote_expr},
7818 {"remote_foreground", 1, 1, f_remote_foreground},
7819 {"remote_peek", 1, 2, f_remote_peek},
7820 {"remote_read", 1, 1, f_remote_read},
7821 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007822 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007824 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007826 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#ifdef FEAT_FLOAT
7828 {"round", 1, 1, f_round},
7829#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007830 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007831 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007832 {"searchpair", 3, 7, f_searchpair},
7833 {"searchpairpos", 3, 7, f_searchpairpos},
7834 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {"server2client", 2, 2, f_server2client},
7836 {"serverlist", 0, 0, f_serverlist},
7837 {"setbufvar", 3, 3, f_setbufvar},
7838 {"setcmdpos", 1, 1, f_setcmdpos},
7839 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007840 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007841 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007842 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007843 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007845 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007846 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007848 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850#ifdef FEAT_FLOAT
7851 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007852 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007853#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007854 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007855 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007856 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007857 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007858 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#ifdef FEAT_FLOAT
7860 {"sqrt", 1, 1, f_sqrt},
7861 {"str2float", 1, 1, f_str2float},
7862#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007863 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007864 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007865 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866#ifdef HAVE_STRFTIME
7867 {"strftime", 1, 2, f_strftime},
7868#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"strlen", 1, 1, f_strlen},
7872 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007873 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007875 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"submatch", 1, 1, f_submatch},
7877 {"substitute", 4, 4, f_substitute},
7878 {"synID", 3, 3, f_synID},
7879 {"synIDattr", 2, 3, f_synIDattr},
7880 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007881 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007882 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007883 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007884 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007885 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007886 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007887 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007888 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007889#ifdef FEAT_FLOAT
7890 {"tan", 1, 1, f_tan},
7891 {"tanh", 1, 1, f_tanh},
7892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007894 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"tolower", 1, 1, f_tolower},
7896 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007897 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898#ifdef FEAT_FLOAT
7899 {"trunc", 1, 1, f_trunc},
7900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007902 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007903 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"virtcol", 1, 1, f_virtcol},
7906 {"visualmode", 0, 1, f_visualmode},
7907 {"winbufnr", 1, 1, f_winbufnr},
7908 {"wincol", 0, 0, f_wincol},
7909 {"winheight", 1, 1, f_winheight},
7910 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007911 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007913 {"winrestview", 1, 1, f_winrestview},
7914 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007916 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917};
7918
7919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7920
7921/*
7922 * Function given to ExpandGeneric() to obtain the list of internal
7923 * or user defined function names.
7924 */
7925 char_u *
7926get_function_name(xp, idx)
7927 expand_T *xp;
7928 int idx;
7929{
7930 static int intidx = -1;
7931 char_u *name;
7932
7933 if (idx == 0)
7934 intidx = -1;
7935 if (intidx < 0)
7936 {
7937 name = get_user_func_name(xp, idx);
7938 if (name != NULL)
7939 return name;
7940 }
7941 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7942 {
7943 STRCPY(IObuff, functions[intidx].f_name);
7944 STRCAT(IObuff, "(");
7945 if (functions[intidx].f_max_argc == 0)
7946 STRCAT(IObuff, ")");
7947 return IObuff;
7948 }
7949
7950 return NULL;
7951}
7952
7953/*
7954 * Function given to ExpandGeneric() to obtain the list of internal or
7955 * user defined variable or function names.
7956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 char_u *
7958get_expr_name(xp, idx)
7959 expand_T *xp;
7960 int idx;
7961{
7962 static int intidx = -1;
7963 char_u *name;
7964
7965 if (idx == 0)
7966 intidx = -1;
7967 if (intidx < 0)
7968 {
7969 name = get_function_name(xp, idx);
7970 if (name != NULL)
7971 return name;
7972 }
7973 return get_user_var_name(xp, ++intidx);
7974}
7975
7976#endif /* FEAT_CMDL_COMPL */
7977
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007978#if defined(EBCDIC) || defined(PROTO)
7979/*
7980 * Compare struct fst by function name.
7981 */
7982 static int
7983compare_func_name(s1, s2)
7984 const void *s1;
7985 const void *s2;
7986{
7987 struct fst *p1 = (struct fst *)s1;
7988 struct fst *p2 = (struct fst *)s2;
7989
7990 return STRCMP(p1->f_name, p2->f_name);
7991}
7992
7993/*
7994 * Sort the function table by function name.
7995 * The sorting of the table above is ASCII dependant.
7996 * On machines using EBCDIC we have to sort it.
7997 */
7998 static void
7999sortFunctions()
8000{
8001 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8002
8003 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8004}
8005#endif
8006
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008/*
8009 * Find internal function in table above.
8010 * Return index, or -1 if not found
8011 */
8012 static int
8013find_internal_func(name)
8014 char_u *name; /* name of the function */
8015{
8016 int first = 0;
8017 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8018 int cmp;
8019 int x;
8020
8021 /*
8022 * Find the function name in the table. Binary search.
8023 */
8024 while (first <= last)
8025 {
8026 x = first + ((unsigned)(last - first) >> 1);
8027 cmp = STRCMP(name, functions[x].f_name);
8028 if (cmp < 0)
8029 last = x - 1;
8030 else if (cmp > 0)
8031 first = x + 1;
8032 else
8033 return x;
8034 }
8035 return -1;
8036}
8037
8038/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008039 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8040 * name it contains, otherwise return "name".
8041 */
8042 static char_u *
8043deref_func_name(name, lenp)
8044 char_u *name;
8045 int *lenp;
8046{
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048 int cc;
8049
8050 cc = name[*lenp];
8051 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008052 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008053 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008055 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008057 {
8058 *lenp = 0;
8059 return (char_u *)""; /* just in case */
8060 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008061 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008063 }
8064
8065 return name;
8066}
8067
8068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 * Allocate a variable for the result of a function.
8070 * Return OK or FAIL.
8071 */
8072 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008073get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8074 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 char_u *name; /* name of the function */
8076 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 char_u **arg; /* argument, pointing to the '(' */
8079 linenr_T firstline; /* first line of range */
8080 linenr_T lastline; /* last line of range */
8081 int *doesrange; /* return: function handled range */
8082 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 char_u *argp;
8086 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008087 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 int argcount = 0; /* number of arguments found */
8089
8090 /*
8091 * Get the arguments.
8092 */
8093 argp = *arg;
8094 while (argcount < MAX_FUNC_ARGS)
8095 {
8096 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8097 if (*argp == ')' || *argp == ',' || *argp == NUL)
8098 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8100 {
8101 ret = FAIL;
8102 break;
8103 }
8104 ++argcount;
8105 if (*argp != ',')
8106 break;
8107 }
8108 if (*argp == ')')
8109 ++argp;
8110 else
8111 ret = FAIL;
8112
8113 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 {
8118 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008119 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008120 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008121 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123
8124 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008125 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126
8127 *arg = skipwhite(argp);
8128 return ret;
8129}
8130
8131
8132/*
8133 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008134 * Return OK when the function can't be called, FAIL otherwise.
8135 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 */
8137 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008138call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008140 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008144 typval_T *argvars; /* vars for arguments, must have "argcount"
8145 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 linenr_T firstline; /* first line of range */
8147 linenr_T lastline; /* last line of range */
8148 int *doesrange; /* return: function handled range */
8149 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153#define ERROR_UNKNOWN 0
8154#define ERROR_TOOMANY 1
8155#define ERROR_TOOFEW 2
8156#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008157#define ERROR_DICT 4
8158#define ERROR_NONE 5
8159#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 int error = ERROR_NONE;
8161 int i;
8162 int llen;
8163 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164#define FLEN_FIXED 40
8165 char_u fname_buf[FLEN_FIXED + 1];
8166 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008167 char_u *name;
8168
8169 /* Make a copy of the name, if it comes from a funcref variable it could
8170 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008171 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008172 if (name == NULL)
8173 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174
8175 /*
8176 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8177 * Change <SNR>123_name() to K_SNR 123_name().
8178 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 llen = eval_fname_script(name);
8181 if (llen > 0)
8182 {
8183 fname_buf[0] = K_SPECIAL;
8184 fname_buf[1] = KS_EXTRA;
8185 fname_buf[2] = (int)KE_SNR;
8186 i = 3;
8187 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8188 {
8189 if (current_SID <= 0)
8190 error = ERROR_SCRIPT;
8191 else
8192 {
8193 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8194 i = (int)STRLEN(fname_buf);
8195 }
8196 }
8197 if (i + STRLEN(name + llen) < FLEN_FIXED)
8198 {
8199 STRCPY(fname_buf + i, name + llen);
8200 fname = fname_buf;
8201 }
8202 else
8203 {
8204 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8205 if (fname == NULL)
8206 error = ERROR_OTHER;
8207 else
8208 {
8209 mch_memmove(fname, fname_buf, (size_t)i);
8210 STRCPY(fname + i, name + llen);
8211 }
8212 }
8213 }
8214 else
8215 fname = name;
8216
8217 *doesrange = FALSE;
8218
8219
8220 /* execute the function if no errors detected and executing */
8221 if (evaluate && error == ERROR_NONE)
8222 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008223 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8224 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 error = ERROR_UNKNOWN;
8226
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008227 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
8229 /*
8230 * User defined function.
8231 */
8232 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008235 /* Trigger FuncUndefined event, may load the function. */
8236 if (fp == NULL
8237 && apply_autocmds(EVENT_FUNCUNDEFINED,
8238 fname, fname, TRUE, NULL)
8239 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008241 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 fp = find_func(fname);
8243 }
8244#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008245 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008246 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008247 {
8248 /* loaded a package, search for the function again */
8249 fp = find_func(fname);
8250 }
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 if (fp != NULL)
8253 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008254 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008256 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008258 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008260 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008261 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 else
8263 {
8264 /*
8265 * Call the user function.
8266 * Save and restore search patterns, script variables and
8267 * redo buffer.
8268 */
8269 save_search_patterns();
8270 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008271 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008273 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008274 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8275 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8276 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008277 /* Function was unreferenced while being used, free it
8278 * now. */
8279 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 restoreRedobuff();
8281 restore_search_patterns();
8282 error = ERROR_NONE;
8283 }
8284 }
8285 }
8286 else
8287 {
8288 /*
8289 * Find the function name in the table, call its implementation.
8290 */
8291 i = find_internal_func(fname);
8292 if (i >= 0)
8293 {
8294 if (argcount < functions[i].f_min_argc)
8295 error = ERROR_TOOFEW;
8296 else if (argcount > functions[i].f_max_argc)
8297 error = ERROR_TOOMANY;
8298 else
8299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 error = ERROR_NONE;
8303 }
8304 }
8305 }
8306 /*
8307 * The function call (or "FuncUndefined" autocommand sequence) might
8308 * have been aborted by an error, an interrupt, or an explicitly thrown
8309 * exception that has not been caught so far. This situation can be
8310 * tested for by calling aborting(). For an error in an internal
8311 * function or for the "E132" error in call_user_func(), however, the
8312 * throw point at which the "force_abort" flag (temporarily reset by
8313 * emsg()) is normally updated has not been reached yet. We need to
8314 * update that flag first to make aborting() reliable.
8315 */
8316 update_force_abort();
8317 }
8318 if (error == ERROR_NONE)
8319 ret = OK;
8320
8321 /*
8322 * Report an error unless the argument evaluation or function call has been
8323 * cancelled due to an aborting error, an interrupt, or an exception.
8324 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 if (!aborting())
8326 {
8327 switch (error)
8328 {
8329 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008330 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008331 break;
8332 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008333 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008334 break;
8335 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008336 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008337 name);
8338 break;
8339 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008340 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008341 name);
8342 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008344 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 name);
8346 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 }
8348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (fname != name && fname != fname_buf)
8351 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008352 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 return ret;
8355}
8356
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008357/*
8358 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008359 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008360 */
8361 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008362emsg_funcname(ermsg, name)
8363 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008364 char_u *name;
8365{
8366 char_u *p;
8367
8368 if (*name == K_SPECIAL)
8369 p = concat_str((char_u *)"<SNR>", name + 3);
8370 else
8371 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008372 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008373 if (p != name)
8374 vim_free(p);
8375}
8376
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008377/*
8378 * Return TRUE for a non-zero Number and a non-empty String.
8379 */
8380 static int
8381non_zero_arg(argvars)
8382 typval_T *argvars;
8383{
8384 return ((argvars[0].v_type == VAR_NUMBER
8385 && argvars[0].vval.v_number != 0)
8386 || (argvars[0].v_type == VAR_STRING
8387 && argvars[0].vval.v_string != NULL
8388 && *argvars[0].vval.v_string != NUL));
8389}
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*********************************************
8392 * Implementation of the built-in functions
8393 */
8394
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008396static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8397
8398/*
8399 * Get the float value of "argvars[0]" into "f".
8400 * Returns FAIL when the argument is not a Number or Float.
8401 */
8402 static int
8403get_float_arg(argvars, f)
8404 typval_T *argvars;
8405 float_T *f;
8406{
8407 if (argvars[0].v_type == VAR_FLOAT)
8408 {
8409 *f = argvars[0].vval.v_float;
8410 return OK;
8411 }
8412 if (argvars[0].v_type == VAR_NUMBER)
8413 {
8414 *f = (float_T)argvars[0].vval.v_number;
8415 return OK;
8416 }
8417 EMSG(_("E808: Number or Float required"));
8418 return FAIL;
8419}
8420
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008421/*
8422 * "abs(expr)" function
8423 */
8424 static void
8425f_abs(argvars, rettv)
8426 typval_T *argvars;
8427 typval_T *rettv;
8428{
8429 if (argvars[0].v_type == VAR_FLOAT)
8430 {
8431 rettv->v_type = VAR_FLOAT;
8432 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8433 }
8434 else
8435 {
8436 varnumber_T n;
8437 int error = FALSE;
8438
8439 n = get_tv_number_chk(&argvars[0], &error);
8440 if (error)
8441 rettv->vval.v_number = -1;
8442 else if (n > 0)
8443 rettv->vval.v_number = n;
8444 else
8445 rettv->vval.v_number = -n;
8446 }
8447}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008448
8449/*
8450 * "acos()" function
8451 */
8452 static void
8453f_acos(argvars, rettv)
8454 typval_T *argvars;
8455 typval_T *rettv;
8456{
8457 float_T f;
8458
8459 rettv->v_type = VAR_FLOAT;
8460 if (get_float_arg(argvars, &f) == OK)
8461 rettv->vval.v_float = acos(f);
8462 else
8463 rettv->vval.v_float = 0.0;
8464}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008465#endif
8466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008468 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 */
8470 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008471f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008472 typval_T *argvars;
8473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008477 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008478 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008480 if ((l = argvars[0].vval.v_list) != NULL
8481 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8482 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 }
8485 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486 EMSG(_(e_listreq));
8487}
8488
8489/*
8490 * "append(lnum, string/list)" function
8491 */
8492 static void
8493f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 typval_T *argvars;
8495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008496{
8497 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008498 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 list_T *l = NULL;
8500 listitem_T *li = NULL;
8501 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008502 long added = 0;
8503
Bram Moolenaar0d660222005-01-07 21:51:51 +00008504 lnum = get_tv_lnum(argvars);
8505 if (lnum >= 0
8506 && lnum <= curbuf->b_ml.ml_line_count
8507 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008509 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008511 l = argvars[1].vval.v_list;
8512 if (l == NULL)
8513 return;
8514 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008516 for (;;)
8517 {
8518 if (l == NULL)
8519 tv = &argvars[1]; /* append a string */
8520 else if (li == NULL)
8521 break; /* end of list */
8522 else
8523 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008524 line = get_tv_string_chk(tv);
8525 if (line == NULL) /* type error */
8526 {
8527 rettv->vval.v_number = 1; /* Failed */
8528 break;
8529 }
8530 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008531 ++added;
8532 if (l == NULL)
8533 break;
8534 li = li->li_next;
8535 }
8536
8537 appended_lines_mark(lnum, added);
8538 if (curwin->w_cursor.lnum > lnum)
8539 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008541 else
8542 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543}
8544
8545/*
8546 * "argc()" function
8547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008549f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008550 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554}
8555
8556/*
8557 * "argidx()" function
8558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565}
8566
8567/*
8568 * "argv(nr)" function
8569 */
8570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 int idx;
8576
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008577 if (argvars[0].v_type != VAR_UNKNOWN)
8578 {
8579 idx = get_tv_number_chk(&argvars[0], NULL);
8580 if (idx >= 0 && idx < ARGCOUNT)
8581 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8582 else
8583 rettv->vval.v_string = NULL;
8584 rettv->v_type = VAR_STRING;
8585 }
8586 else if (rettv_list_alloc(rettv) == OK)
8587 for (idx = 0; idx < ARGCOUNT; ++idx)
8588 list_append_string(rettv->vval.v_list,
8589 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590}
8591
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008593/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008594 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008595 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008596 static void
8597f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008598 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008599 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008600{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008601 float_T f;
8602
8603 rettv->v_type = VAR_FLOAT;
8604 if (get_float_arg(argvars, &f) == OK)
8605 rettv->vval.v_float = asin(f);
8606 else
8607 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008608}
8609
8610/*
8611 * "atan()" function
8612 */
8613 static void
8614f_atan(argvars, rettv)
8615 typval_T *argvars;
8616 typval_T *rettv;
8617{
8618 float_T f;
8619
8620 rettv->v_type = VAR_FLOAT;
8621 if (get_float_arg(argvars, &f) == OK)
8622 rettv->vval.v_float = atan(f);
8623 else
8624 rettv->vval.v_float = 0.0;
8625}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008626
8627/*
8628 * "atan2()" function
8629 */
8630 static void
8631f_atan2(argvars, rettv)
8632 typval_T *argvars;
8633 typval_T *rettv;
8634{
8635 float_T fx, fy;
8636
8637 rettv->v_type = VAR_FLOAT;
8638 if (get_float_arg(argvars, &fx) == OK
8639 && get_float_arg(&argvars[1], &fy) == OK)
8640 rettv->vval.v_float = atan2(fx, fy);
8641 else
8642 rettv->vval.v_float = 0.0;
8643}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008644#endif
8645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646/*
8647 * "browse(save, title, initdir, default)" function
8648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008651 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
8654#ifdef FEAT_BROWSE
8655 int save;
8656 char_u *title;
8657 char_u *initdir;
8658 char_u *defname;
8659 char_u buf[NUMBUFLEN];
8660 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008661 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 save = get_tv_number_chk(&argvars[0], &error);
8664 title = get_tv_string_chk(&argvars[1]);
8665 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8666 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 if (error || title == NULL || initdir == NULL || defname == NULL)
8669 rettv->vval.v_string = NULL;
8670 else
8671 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008672 do_browse(save ? BROWSE_SAVE : 0,
8673 title, defname, NULL, initdir, NULL, curbuf);
8674#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008676#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008678}
8679
8680/*
8681 * "browsedir(title, initdir)" function
8682 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008685 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008687{
8688#ifdef FEAT_BROWSE
8689 char_u *title;
8690 char_u *initdir;
8691 char_u buf[NUMBUFLEN];
8692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008693 title = get_tv_string_chk(&argvars[0]);
8694 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008696 if (title == NULL || initdir == NULL)
8697 rettv->vval.v_string = NULL;
8698 else
8699 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008700 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705}
8706
Bram Moolenaar33570922005-01-25 22:26:29 +00008707static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709/*
8710 * Find a buffer by number or exact name.
8711 */
8712 static buf_T *
8713find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008718 if (avar->v_type == VAR_NUMBER)
8719 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008720 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008722 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008723 if (buf == NULL)
8724 {
8725 /* No full path name match, try a match with a URL or a "nofile"
8726 * buffer, these don't use the full path. */
8727 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8728 if (buf->b_fname != NULL
8729 && (path_with_url(buf->b_fname)
8730#ifdef FEAT_QUICKFIX
8731 || bt_nofile(buf)
8732#endif
8733 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008734 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008735 break;
8736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 }
8738 return buf;
8739}
8740
8741/*
8742 * "bufexists(expr)" function
8743 */
8744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750}
8751
8752/*
8753 * "buflisted(expr)" function
8754 */
8755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 typval_T *argvars;
8758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 buf_T *buf;
8761
8762 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764}
8765
8766/*
8767 * "bufloaded(expr)" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 buf_T *buf;
8775
8776 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
Bram Moolenaar33570922005-01-25 22:26:29 +00008780static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782/*
8783 * Get buffer by number or pattern.
8784 */
8785 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 int save_magic;
8791 char_u *save_cpo;
8792 buf_T *buf;
8793
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 if (tv->v_type == VAR_NUMBER)
8795 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008796 if (tv->v_type != VAR_STRING)
8797 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 if (name == NULL || *name == NUL)
8799 return curbuf;
8800 if (name[0] == '$' && name[1] == NUL)
8801 return lastbuf;
8802
8803 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8804 save_magic = p_magic;
8805 p_magic = TRUE;
8806 save_cpo = p_cpo;
8807 p_cpo = (char_u *)"";
8808
8809 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8810 TRUE, FALSE));
8811
8812 p_magic = save_magic;
8813 p_cpo = save_cpo;
8814
8815 /* If not found, try expanding the name, like done for bufexists(). */
8816 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
8819 return buf;
8820}
8821
8822/*
8823 * "bufname(expr)" function
8824 */
8825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008827 typval_T *argvars;
8828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 buf_T *buf;
8831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008832 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 buf = get_buf_tv(&argvars[0]);
8835 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 --emsg_off;
8841}
8842
8843/*
8844 * "bufnr(expr)" function
8845 */
8846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 typval_T *argvars;
8849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008852 int error = FALSE;
8853 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008855 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008858 --emsg_off;
8859
8860 /* If the buffer isn't found and the second argument is not zero create a
8861 * new buffer. */
8862 if (buf == NULL
8863 && argvars[1].v_type != VAR_UNKNOWN
8864 && get_tv_number_chk(&argvars[1], &error) != 0
8865 && !error
8866 && (name = get_tv_string_chk(&argvars[0])) != NULL
8867 && !error)
8868 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874}
8875
8876/*
8877 * "bufwinnr(nr)" function
8878 */
8879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008881 typval_T *argvars;
8882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884#ifdef FEAT_WINDOWS
8885 win_T *wp;
8886 int winnr = 0;
8887#endif
8888 buf_T *buf;
8889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893#ifdef FEAT_WINDOWS
8894 for (wp = firstwin; wp; wp = wp->w_next)
8895 {
8896 ++winnr;
8897 if (wp->w_buffer == buf)
8898 break;
8899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903#endif
8904 --emsg_off;
8905}
8906
8907/*
8908 * "byte2line(byte)" function
8909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008912 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
8915#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917#else
8918 long boff = 0;
8919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 (linenr_T)0, &boff);
8926#endif
8927}
8928
8929/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008930 * "byteidx()" function
8931 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *argvars;
8935 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008936{
8937#ifdef FEAT_MBYTE
8938 char_u *t;
8939#endif
8940 char_u *str;
8941 long idx;
8942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 str = get_tv_string_chk(&argvars[0]);
8944 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008947 return;
8948
8949#ifdef FEAT_MBYTE
8950 t = str;
8951 for ( ; idx > 0; idx--)
8952 {
8953 if (*t == NUL) /* EOL reached */
8954 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008955 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008956 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008957 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008958#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008959 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008961#endif
8962}
8963
8964/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008965 * "call(func, arglist)" function
8966 */
8967 static void
8968f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *argvars;
8970 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008971{
8972 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008973 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008974 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008976 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008978
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008979 if (argvars[1].v_type != VAR_LIST)
8980 {
8981 EMSG(_(e_listreq));
8982 return;
8983 }
8984 if (argvars[1].vval.v_list == NULL)
8985 return;
8986
8987 if (argvars[0].v_type == VAR_FUNC)
8988 func = argvars[0].vval.v_string;
8989 else
8990 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 if (*func == NUL)
8992 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008993
Bram Moolenaare9a41262005-01-15 22:18:47 +00008994 if (argvars[2].v_type != VAR_UNKNOWN)
8995 {
8996 if (argvars[2].v_type != VAR_DICT)
8997 {
8998 EMSG(_(e_dictreq));
8999 return;
9000 }
9001 selfdict = argvars[2].vval.v_dict;
9002 }
9003
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9005 item = item->li_next)
9006 {
9007 if (argc == MAX_FUNC_ARGS)
9008 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009009 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010 break;
9011 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009012 /* Make a copy of each argument. This is needed to be able to set
9013 * v_lock to VAR_FIXED in the copy without changing the original list.
9014 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009015 copy_tv(&item->li_tv, &argv[argc++]);
9016 }
9017
9018 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009019 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009020 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9021 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009022
9023 /* Free the arguments. */
9024 while (argc > 0)
9025 clear_tv(&argv[--argc]);
9026}
9027
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009028#ifdef FEAT_FLOAT
9029/*
9030 * "ceil({float})" function
9031 */
9032 static void
9033f_ceil(argvars, rettv)
9034 typval_T *argvars;
9035 typval_T *rettv;
9036{
9037 float_T f;
9038
9039 rettv->v_type = VAR_FLOAT;
9040 if (get_float_arg(argvars, &f) == OK)
9041 rettv->vval.v_float = ceil(f);
9042 else
9043 rettv->vval.v_float = 0.0;
9044}
9045#endif
9046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009047/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009048 * "changenr()" function
9049 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009050 static void
9051f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009052 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009053 typval_T *rettv;
9054{
9055 rettv->vval.v_number = curbuf->b_u_seq_cur;
9056}
9057
9058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 * "char2nr(string)" function
9060 */
9061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 typval_T *argvars;
9064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066#ifdef FEAT_MBYTE
9067 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009068 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 else
9070#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
9075 * "cindent(lnum)" function
9076 */
9077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *argvars;
9080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082#ifdef FEAT_CINDENT
9083 pos_T pos;
9084 linenr_T lnum;
9085
9086 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9089 {
9090 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 curwin->w_cursor = pos;
9093 }
9094 else
9095#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
9099/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009100 * "clearmatches()" function
9101 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009102 static void
9103f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009104 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009105 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009106{
9107#ifdef FEAT_SEARCH_EXTRA
9108 clear_matches(curwin);
9109#endif
9110}
9111
9112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 * "col(string)" function
9114 */
9115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *argvars;
9118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 colnr_T col = 0;
9121 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009122 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009124 fp = var2fpos(&argvars[0], FALSE, &fnum);
9125 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 {
9127 if (fp->col == MAXCOL)
9128 {
9129 /* '> can be MAXCOL, get the length of the line then */
9130 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009131 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 else
9133 col = MAXCOL;
9134 }
9135 else
9136 {
9137 col = fp->col + 1;
9138#ifdef FEAT_VIRTUALEDIT
9139 /* col(".") when the cursor is on the NUL at the end of the line
9140 * because of "coladd" can be seen as an extra column. */
9141 if (virtual_active() && fp == &curwin->w_cursor)
9142 {
9143 char_u *p = ml_get_cursor();
9144
9145 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9146 curwin->w_virtcol - curwin->w_cursor.coladd))
9147 {
9148# ifdef FEAT_MBYTE
9149 int l;
9150
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009151 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 col += l;
9153# else
9154 if (*p != NUL && p[1] == NUL)
9155 ++col;
9156# endif
9157 }
9158 }
9159#endif
9160 }
9161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
9164
Bram Moolenaar572cb562005-08-05 21:35:02 +00009165#if defined(FEAT_INS_EXPAND)
9166/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009167 * "complete()" function
9168 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009169 static void
9170f_complete(argvars, rettv)
9171 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009172 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009173{
9174 int startcol;
9175
9176 if ((State & INSERT) == 0)
9177 {
9178 EMSG(_("E785: complete() can only be used in Insert mode"));
9179 return;
9180 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009181
9182 /* Check for undo allowed here, because if something was already inserted
9183 * the line was already saved for undo and this check isn't done. */
9184 if (!undo_allowed())
9185 return;
9186
Bram Moolenaarade00832006-03-10 21:46:58 +00009187 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9188 {
9189 EMSG(_(e_invarg));
9190 return;
9191 }
9192
9193 startcol = get_tv_number_chk(&argvars[0], NULL);
9194 if (startcol <= 0)
9195 return;
9196
9197 set_completion(startcol - 1, argvars[1].vval.v_list);
9198}
9199
9200/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009201 * "complete_add()" function
9202 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009203 static void
9204f_complete_add(argvars, rettv)
9205 typval_T *argvars;
9206 typval_T *rettv;
9207{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009208 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009209}
9210
9211/*
9212 * "complete_check()" function
9213 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009214 static void
9215f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009216 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009217 typval_T *rettv;
9218{
9219 int saved = RedrawingDisabled;
9220
9221 RedrawingDisabled = 0;
9222 ins_compl_check_keys(0);
9223 rettv->vval.v_number = compl_interrupted;
9224 RedrawingDisabled = saved;
9225}
9226#endif
9227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228/*
9229 * "confirm(message, buttons[, default [, type]])" function
9230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009233 typval_T *argvars UNUSED;
9234 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9237 char_u *message;
9238 char_u *buttons = NULL;
9239 char_u buf[NUMBUFLEN];
9240 char_u buf2[NUMBUFLEN];
9241 int def = 1;
9242 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 char_u *typestr;
9244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 message = get_tv_string_chk(&argvars[0]);
9247 if (message == NULL)
9248 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009249 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9252 if (buttons == NULL)
9253 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009257 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009259 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9260 if (typestr == NULL)
9261 error = TRUE;
9262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 switch (TOUPPER_ASC(*typestr))
9265 {
9266 case 'E': type = VIM_ERROR; break;
9267 case 'Q': type = VIM_QUESTION; break;
9268 case 'I': type = VIM_INFO; break;
9269 case 'W': type = VIM_WARNING; break;
9270 case 'G': type = VIM_GENERIC; break;
9271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 }
9273 }
9274 }
9275 }
9276
9277 if (buttons == NULL || *buttons == NUL)
9278 buttons = (char_u *)_("&Ok");
9279
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009280 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#endif
9284}
9285
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009286/*
9287 * "copy()" function
9288 */
9289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 typval_T *argvars;
9292 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009293{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009294 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009297#ifdef FEAT_FLOAT
9298/*
9299 * "cos()" function
9300 */
9301 static void
9302f_cos(argvars, rettv)
9303 typval_T *argvars;
9304 typval_T *rettv;
9305{
9306 float_T f;
9307
9308 rettv->v_type = VAR_FLOAT;
9309 if (get_float_arg(argvars, &f) == OK)
9310 rettv->vval.v_float = cos(f);
9311 else
9312 rettv->vval.v_float = 0.0;
9313}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009314
9315/*
9316 * "cosh()" function
9317 */
9318 static void
9319f_cosh(argvars, rettv)
9320 typval_T *argvars;
9321 typval_T *rettv;
9322{
9323 float_T f;
9324
9325 rettv->v_type = VAR_FLOAT;
9326 if (get_float_arg(argvars, &f) == OK)
9327 rettv->vval.v_float = cosh(f);
9328 else
9329 rettv->vval.v_float = 0.0;
9330}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009331#endif
9332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009334 * "count()" function
9335 */
9336 static void
9337f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009338 typval_T *argvars;
9339 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009340{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009341 long n = 0;
9342 int ic = FALSE;
9343
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009345 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 listitem_T *li;
9347 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009348 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009349
Bram Moolenaare9a41262005-01-15 22:18:47 +00009350 if ((l = argvars[0].vval.v_list) != NULL)
9351 {
9352 li = l->lv_first;
9353 if (argvars[2].v_type != VAR_UNKNOWN)
9354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 int error = FALSE;
9356
9357 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009358 if (argvars[3].v_type != VAR_UNKNOWN)
9359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 idx = get_tv_number_chk(&argvars[3], &error);
9361 if (!error)
9362 {
9363 li = list_find(l, idx);
9364 if (li == NULL)
9365 EMSGN(_(e_listidx), idx);
9366 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368 if (error)
9369 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009370 }
9371
9372 for ( ; li != NULL; li = li->li_next)
9373 if (tv_equal(&li->li_tv, &argvars[1], ic))
9374 ++n;
9375 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009377 else if (argvars[0].v_type == VAR_DICT)
9378 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009379 int todo;
9380 dict_T *d;
9381 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009383 if ((d = argvars[0].vval.v_dict) != NULL)
9384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009385 int error = FALSE;
9386
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 if (argvars[2].v_type != VAR_UNKNOWN)
9388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390 if (argvars[3].v_type != VAR_UNKNOWN)
9391 EMSG(_(e_invarg));
9392 }
9393
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009394 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009396 {
9397 if (!HASHITEM_EMPTY(hi))
9398 {
9399 --todo;
9400 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9401 ++n;
9402 }
9403 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009404 }
9405 }
9406 else
9407 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009408 rettv->vval.v_number = n;
9409}
9410
9411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9413 *
9414 * Checks the existence of a cscope connection.
9415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009418 typval_T *argvars UNUSED;
9419 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421#ifdef FEAT_CSCOPE
9422 int num = 0;
9423 char_u *dbpath = NULL;
9424 char_u *prepend = NULL;
9425 char_u buf[NUMBUFLEN];
9426
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427 if (argvars[0].v_type != VAR_UNKNOWN
9428 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 num = (int)get_tv_number(&argvars[0]);
9431 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
9435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#endif
9438}
9439
9440/*
9441 * "cursor(lnum, col)" function
9442 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009443 * Moves the cursor to the specified line and column.
9444 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009452#ifdef FEAT_VIRTUALEDIT
9453 long coladd = 0;
9454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009456 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009457 if (argvars[1].v_type == VAR_UNKNOWN)
9458 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009459 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009460
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009461 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009462 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009463 line = pos.lnum;
9464 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009465#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009466 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009467#endif
9468 }
9469 else
9470 {
9471 line = get_tv_lnum(argvars);
9472 col = get_tv_number_chk(&argvars[1], NULL);
9473#ifdef FEAT_VIRTUALEDIT
9474 if (argvars[2].v_type != VAR_UNKNOWN)
9475 coladd = get_tv_number_chk(&argvars[2], NULL);
9476#endif
9477 }
9478 if (line < 0 || col < 0
9479#ifdef FEAT_VIRTUALEDIT
9480 || coladd < 0
9481#endif
9482 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009483 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (line > 0)
9485 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 if (col > 0)
9487 curwin->w_cursor.col = col - 1;
9488#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009489 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490#endif
9491
9492 /* Make sure the cursor is in a valid position. */
9493 check_cursor();
9494#ifdef FEAT_MBYTE
9495 /* Correct cursor for multi-byte character. */
9496 if (has_mbyte)
9497 mb_adjust_cursor();
9498#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009499
9500 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009501 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503
9504/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 */
9507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 typval_T *argvars;
9510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009512 int noref = 0;
9513
9514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009516 if (noref < 0 || noref > 1)
9517 EMSG(_(e_invarg));
9518 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009519 {
9520 current_copyID += COPYID_INC;
9521 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524
9525/*
9526 * "delete()" function
9527 */
9528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009530 typval_T *argvars;
9531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537}
9538
9539/*
9540 * "did_filetype()" function
9541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009544 typval_T *argvars UNUSED;
9545 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#endif
9550}
9551
9552/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009553 * "diff_filler()" function
9554 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009557 typval_T *argvars UNUSED;
9558 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009559{
9560#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009562#endif
9563}
9564
9565/*
9566 * "diff_hlID()" function
9567 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009570 typval_T *argvars UNUSED;
9571 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009572{
9573#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009575 static linenr_T prev_lnum = 0;
9576 static int changedtick = 0;
9577 static int fnum = 0;
9578 static int change_start = 0;
9579 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009580 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009581 int filler_lines;
9582 int col;
9583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 if (lnum < 0) /* ignore type error in {lnum} arg */
9585 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009586 if (lnum != prev_lnum
9587 || changedtick != curbuf->b_changedtick
9588 || fnum != curbuf->b_fnum)
9589 {
9590 /* New line, buffer, change: need to get the values. */
9591 filler_lines = diff_check(curwin, lnum);
9592 if (filler_lines < 0)
9593 {
9594 if (filler_lines == -1)
9595 {
9596 change_start = MAXCOL;
9597 change_end = -1;
9598 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9599 hlID = HLF_ADD; /* added line */
9600 else
9601 hlID = HLF_CHD; /* changed line */
9602 }
9603 else
9604 hlID = HLF_ADD; /* added line */
9605 }
9606 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009607 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009608 prev_lnum = lnum;
9609 changedtick = curbuf->b_changedtick;
9610 fnum = curbuf->b_fnum;
9611 }
9612
9613 if (hlID == HLF_CHD || hlID == HLF_TXD)
9614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009616 if (col >= change_start && col <= change_end)
9617 hlID = HLF_TXD; /* changed text */
9618 else
9619 hlID = HLF_CHD; /* changed line */
9620 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009621 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009622#endif
9623}
9624
9625/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009626 * "empty({expr})" function
9627 */
9628 static void
9629f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009632{
9633 int n;
9634
9635 switch (argvars[0].v_type)
9636 {
9637 case VAR_STRING:
9638 case VAR_FUNC:
9639 n = argvars[0].vval.v_string == NULL
9640 || *argvars[0].vval.v_string == NUL;
9641 break;
9642 case VAR_NUMBER:
9643 n = argvars[0].vval.v_number == 0;
9644 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009645#ifdef FEAT_FLOAT
9646 case VAR_FLOAT:
9647 n = argvars[0].vval.v_float == 0.0;
9648 break;
9649#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009650 case VAR_LIST:
9651 n = argvars[0].vval.v_list == NULL
9652 || argvars[0].vval.v_list->lv_first == NULL;
9653 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 case VAR_DICT:
9655 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009658 default:
9659 EMSG2(_(e_intern2), "f_empty()");
9660 n = 0;
9661 }
9662
9663 rettv->vval.v_number = n;
9664}
9665
9666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 * "escape({string}, {chars})" function
9668 */
9669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *argvars;
9672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
9674 char_u buf[NUMBUFLEN];
9675
Bram Moolenaar758711c2005-02-02 23:11:38 +00009676 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9677 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679}
9680
9681/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009682 * "eval()" function
9683 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009684 static void
9685f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 typval_T *argvars;
9687 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009688{
9689 char_u *s;
9690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009691 s = get_tv_string_chk(&argvars[0]);
9692 if (s != NULL)
9693 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9696 {
9697 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009698 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009700 else if (*s != NUL)
9701 EMSG(_(e_trailing));
9702}
9703
9704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 * "eventhandler()" function
9706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009709 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713}
9714
9715/*
9716 * "executable()" function
9717 */
9718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009720 typval_T *argvars;
9721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724}
9725
9726/*
9727 * "exists()" function
9728 */
9729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 typval_T *argvars;
9732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
9734 char_u *p;
9735 char_u *name;
9736 int n = FALSE;
9737 int len = 0;
9738
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009739 no_autoload = TRUE;
9740
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (*p == '$') /* environment variable */
9743 {
9744 /* first try "normal" environment variables (fast) */
9745 if (mch_getenv(p + 1) != NULL)
9746 n = TRUE;
9747 else
9748 {
9749 /* try expanding things like $VIM and ${HOME} */
9750 p = expand_env_save(p);
9751 if (p != NULL && *p != '$')
9752 n = TRUE;
9753 vim_free(p);
9754 }
9755 }
9756 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009759 if (*skipwhite(p) != NUL)
9760 n = FALSE; /* trailing garbage */
9761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 else if (*p == '*') /* internal or user defined function */
9763 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009764 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 }
9766 else if (*p == ':')
9767 {
9768 n = cmd_exists(p + 1);
9769 }
9770 else if (*p == '#')
9771 {
9772#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009773 if (p[1] == '#')
9774 n = autocmd_supported(p + 2);
9775 else
9776 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777#endif
9778 }
9779 else /* internal variable */
9780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009781 char_u *tofree;
9782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009784 /* get_name_len() takes care of expanding curly braces */
9785 name = p;
9786 len = get_name_len(&p, &tofree, TRUE, FALSE);
9787 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009789 if (tofree != NULL)
9790 name = tofree;
9791 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9792 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009794 /* handle d.key, l[idx], f(expr) */
9795 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9796 if (n)
9797 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 }
9799 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009800 if (*p != NUL)
9801 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009803 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 }
9805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009807
9808 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809}
9810
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009811#ifdef FEAT_FLOAT
9812/*
9813 * "exp()" function
9814 */
9815 static void
9816f_exp(argvars, rettv)
9817 typval_T *argvars;
9818 typval_T *rettv;
9819{
9820 float_T f;
9821
9822 rettv->v_type = VAR_FLOAT;
9823 if (get_float_arg(argvars, &f) == OK)
9824 rettv->vval.v_float = exp(f);
9825 else
9826 rettv->vval.v_float = 0.0;
9827}
9828#endif
9829
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830/*
9831 * "expand()" function
9832 */
9833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009835 typval_T *argvars;
9836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837{
9838 char_u *s;
9839 int len;
9840 char_u *errormsg;
9841 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9842 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845 rettv->v_type = VAR_STRING;
9846 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 if (*s == '%' || *s == '#' || *s == '<')
9848 {
9849 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009850 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 --emsg_off;
9852 }
9853 else
9854 {
9855 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009856 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 if (argvars[1].v_type != VAR_UNKNOWN
9858 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009860 if (!error)
9861 {
9862 ExpandInit(&xpc);
9863 xpc.xp_context = EXPAND_FILES;
9864 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009865 }
9866 else
9867 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 }
9869}
9870
9871/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009873 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009874 */
9875 static void
9876f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 typval_T *argvars;
9878 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009880 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009881 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009882 list_T *l1, *l2;
9883 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009886
Bram Moolenaare9a41262005-01-15 22:18:47 +00009887 l1 = argvars[0].vval.v_list;
9888 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009889 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9890 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009891 {
9892 if (argvars[2].v_type != VAR_UNKNOWN)
9893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 before = get_tv_number_chk(&argvars[2], &error);
9895 if (error)
9896 return; /* type error; errmsg already given */
9897
Bram Moolenaar758711c2005-02-02 23:11:38 +00009898 if (before == l1->lv_len)
9899 item = NULL;
9900 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009901 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009902 item = list_find(l1, before);
9903 if (item == NULL)
9904 {
9905 EMSGN(_(e_listidx), before);
9906 return;
9907 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 }
9909 }
9910 else
9911 item = NULL;
9912 list_extend(l1, l2, item);
9913
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 copy_tv(&argvars[0], rettv);
9915 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009917 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 dict_T *d1, *d2;
9920 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 char_u *action;
9922 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009923 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009924 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925
9926 d1 = argvars[0].vval.v_dict;
9927 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009928 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9929 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 {
9931 /* Check the third argument. */
9932 if (argvars[2].v_type != VAR_UNKNOWN)
9933 {
9934 static char *(av[]) = {"keep", "force", "error"};
9935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 action = get_tv_string_chk(&argvars[2]);
9937 if (action == NULL)
9938 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 for (i = 0; i < 3; ++i)
9940 if (STRCMP(action, av[i]) == 0)
9941 break;
9942 if (i == 3)
9943 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009944 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945 return;
9946 }
9947 }
9948 else
9949 action = (char_u *)"force";
9950
9951 /* Go over all entries in the second dict and add them to the
9952 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009953 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009955 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009956 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009958 --todo;
9959 di1 = dict_find(d1, hi2->hi_key, -1);
9960 if (di1 == NULL)
9961 {
9962 di1 = dictitem_copy(HI2DI(hi2));
9963 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9964 dictitem_free(di1);
9965 }
9966 else if (*action == 'e')
9967 {
9968 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9969 break;
9970 }
9971 else if (*action == 'f')
9972 {
9973 clear_tv(&di1->di_tv);
9974 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 }
9977 }
9978
Bram Moolenaare9a41262005-01-15 22:18:47 +00009979 copy_tv(&argvars[0], rettv);
9980 }
9981 }
9982 else
9983 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009984}
9985
9986/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009987 * "feedkeys()" function
9988 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009989 static void
9990f_feedkeys(argvars, rettv)
9991 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009992 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009993{
9994 int remap = TRUE;
9995 char_u *keys, *flags;
9996 char_u nbuf[NUMBUFLEN];
9997 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009998 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009999
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010000 /* This is not allowed in the sandbox. If the commands would still be
10001 * executed in the sandbox it would be OK, but it probably happens later,
10002 * when "sandbox" is no longer set. */
10003 if (check_secure())
10004 return;
10005
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010006 keys = get_tv_string(&argvars[0]);
10007 if (*keys != NUL)
10008 {
10009 if (argvars[1].v_type != VAR_UNKNOWN)
10010 {
10011 flags = get_tv_string_buf(&argvars[1], nbuf);
10012 for ( ; *flags != NUL; ++flags)
10013 {
10014 switch (*flags)
10015 {
10016 case 'n': remap = FALSE; break;
10017 case 'm': remap = TRUE; break;
10018 case 't': typed = TRUE; break;
10019 }
10020 }
10021 }
10022
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010023 /* Need to escape K_SPECIAL and CSI before putting the string in the
10024 * typeahead buffer. */
10025 keys_esc = vim_strsave_escape_csi(keys);
10026 if (keys_esc != NULL)
10027 {
10028 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010029 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010030 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010031 if (vgetc_busy)
10032 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010033 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010034 }
10035}
10036
10037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 * "filereadable()" function
10039 */
10040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 typval_T *argvars;
10043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010045 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 char_u *p;
10047 int n;
10048
Bram Moolenaarc236c162008-07-13 17:41:49 +000010049#ifndef O_NONBLOCK
10050# define O_NONBLOCK 0
10051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010053 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10054 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
10056 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010057 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 }
10059 else
10060 n = FALSE;
10061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063}
10064
10065/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010066 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 * rights to write into.
10068 */
10069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 typval_T *argvars;
10072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010074 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010077static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010078
10079 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010080findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010081 typval_T *argvars;
10082 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010083 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010084{
10085#ifdef FEAT_SEARCHPATH
10086 char_u *fname;
10087 char_u *fresult = NULL;
10088 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10089 char_u *p;
10090 char_u pathbuf[NUMBUFLEN];
10091 int count = 1;
10092 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010093 int error = FALSE;
10094#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010095
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010096 rettv->vval.v_string = NULL;
10097 rettv->v_type = VAR_STRING;
10098
10099#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010101
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010102 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010104 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10105 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010106 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 else
10108 {
10109 if (*p != NUL)
10110 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010112 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010113 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010115 }
10116
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010117 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10118 error = TRUE;
10119
10120 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 do
10123 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010124 if (rettv->v_type == VAR_STRING)
10125 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010126 fresult = find_file_in_path_option(first ? fname : NULL,
10127 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010128 0, first, path,
10129 find_what,
10130 curbuf->b_ffname,
10131 find_what == FINDFILE_DIR
10132 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010134
10135 if (fresult != NULL && rettv->v_type == VAR_LIST)
10136 list_append_string(rettv->vval.v_list, fresult, -1);
10137
10138 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010140
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010141 if (rettv->v_type == VAR_STRING)
10142 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010143#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010144}
10145
Bram Moolenaar33570922005-01-25 22:26:29 +000010146static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10147static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010148
10149/*
10150 * Implementation of map() and filter().
10151 */
10152 static void
10153filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 typval_T *argvars;
10155 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010156 int map;
10157{
10158 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 listitem_T *li, *nli;
10161 list_T *l = NULL;
10162 dictitem_T *di;
10163 hashtab_T *ht;
10164 hashitem_T *hi;
10165 dict_T *d = NULL;
10166 typval_T save_val;
10167 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010169 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010170 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010171 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010172 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010173
Bram Moolenaare9a41262005-01-15 22:18:47 +000010174 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010175 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010176 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010177 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 return;
10179 }
10180 else if (argvars[0].v_type == VAR_DICT)
10181 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010182 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010183 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010184 return;
10185 }
10186 else
10187 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010188 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 return;
10190 }
10191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 expr = get_tv_string_buf_chk(&argvars[1], buf);
10193 /* On type errors, the preceding call has already displayed an error
10194 * message. Avoid a misleading error message for an empty string that
10195 * was not passed as argument. */
10196 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 prepare_vimvar(VV_VAL, &save_val);
10199 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010200
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010201 /* We reset "did_emsg" to be able to detect whether an error
10202 * occurred during evaluation of the expression. */
10203 save_did_emsg = did_emsg;
10204 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010205
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010206 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010209 vimvars[VV_KEY].vv_type = VAR_STRING;
10210
10211 ht = &d->dv_hashtab;
10212 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010213 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 if (!HASHITEM_EMPTY(hi))
10217 {
10218 --todo;
10219 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010220 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 break;
10222 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010223 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010224 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 break;
10226 if (!map && rem)
10227 dictitem_remove(d, di);
10228 clear_tv(&vimvars[VV_KEY].vv_tv);
10229 }
10230 }
10231 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 }
10233 else
10234 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010235 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 for (li = l->lv_first; li != NULL; li = nli)
10238 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010239 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010240 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010242 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010243 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010244 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010245 break;
10246 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010248 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010250 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010252 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010254
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010255 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257
10258 copy_tv(&argvars[0], rettv);
10259}
10260
10261 static int
10262filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010263 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 char_u *expr;
10265 int map;
10266 int *remp;
10267{
Bram Moolenaar33570922005-01-25 22:26:29 +000010268 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010269 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010270 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271
Bram Moolenaar33570922005-01-25 22:26:29 +000010272 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 s = expr;
10274 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010275 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 if (*s != NUL) /* check for trailing chars after expr */
10277 {
10278 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010279 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 }
10281 if (map)
10282 {
10283 /* map(): replace the list item value */
10284 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010285 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 *tv = rettv;
10287 }
10288 else
10289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 int error = FALSE;
10291
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 /* On type error, nothing has been removed; return FAIL to stop the
10296 * loop. The error message was given by get_tv_number_chk(). */
10297 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010298 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010299 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010300 retval = OK;
10301theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010302 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010303 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010304}
10305
10306/*
10307 * "filter()" function
10308 */
10309 static void
10310f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010311 typval_T *argvars;
10312 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010313{
10314 filter_map(argvars, rettv, FALSE);
10315}
10316
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010317/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010318 * "finddir({fname}[, {path}[, {count}]])" function
10319 */
10320 static void
10321f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010322 typval_T *argvars;
10323 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010324{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010325 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010326}
10327
10328/*
10329 * "findfile({fname}[, {path}[, {count}]])" function
10330 */
10331 static void
10332f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010333 typval_T *argvars;
10334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010335{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010336 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010337}
10338
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010339#ifdef FEAT_FLOAT
10340/*
10341 * "float2nr({float})" function
10342 */
10343 static void
10344f_float2nr(argvars, rettv)
10345 typval_T *argvars;
10346 typval_T *rettv;
10347{
10348 float_T f;
10349
10350 if (get_float_arg(argvars, &f) == OK)
10351 {
10352 if (f < -0x7fffffff)
10353 rettv->vval.v_number = -0x7fffffff;
10354 else if (f > 0x7fffffff)
10355 rettv->vval.v_number = 0x7fffffff;
10356 else
10357 rettv->vval.v_number = (varnumber_T)f;
10358 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010359}
10360
10361/*
10362 * "floor({float})" function
10363 */
10364 static void
10365f_floor(argvars, rettv)
10366 typval_T *argvars;
10367 typval_T *rettv;
10368{
10369 float_T f;
10370
10371 rettv->v_type = VAR_FLOAT;
10372 if (get_float_arg(argvars, &f) == OK)
10373 rettv->vval.v_float = floor(f);
10374 else
10375 rettv->vval.v_float = 0.0;
10376}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010377
10378/*
10379 * "fmod()" function
10380 */
10381 static void
10382f_fmod(argvars, rettv)
10383 typval_T *argvars;
10384 typval_T *rettv;
10385{
10386 float_T fx, fy;
10387
10388 rettv->v_type = VAR_FLOAT;
10389 if (get_float_arg(argvars, &fx) == OK
10390 && get_float_arg(&argvars[1], &fy) == OK)
10391 rettv->vval.v_float = fmod(fx, fy);
10392 else
10393 rettv->vval.v_float = 0.0;
10394}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010395#endif
10396
Bram Moolenaar0d660222005-01-07 21:51:51 +000010397/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010398 * "fnameescape({string})" function
10399 */
10400 static void
10401f_fnameescape(argvars, rettv)
10402 typval_T *argvars;
10403 typval_T *rettv;
10404{
10405 rettv->vval.v_string = vim_strsave_fnameescape(
10406 get_tv_string(&argvars[0]), FALSE);
10407 rettv->v_type = VAR_STRING;
10408}
10409
10410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 * "fnamemodify({fname}, {mods})" function
10412 */
10413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 typval_T *argvars;
10416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417{
10418 char_u *fname;
10419 char_u *mods;
10420 int usedlen = 0;
10421 int len;
10422 char_u *fbuf = NULL;
10423 char_u buf[NUMBUFLEN];
10424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 fname = get_tv_string_chk(&argvars[0]);
10426 mods = get_tv_string_buf_chk(&argvars[1], buf);
10427 if (fname == NULL || mods == NULL)
10428 fname = NULL;
10429 else
10430 {
10431 len = (int)STRLEN(fname);
10432 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 vim_free(fbuf);
10441}
10442
Bram Moolenaar33570922005-01-25 22:26:29 +000010443static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444
10445/*
10446 * "foldclosed()" function
10447 */
10448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010450 typval_T *argvars;
10451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 int end;
10453{
10454#ifdef FEAT_FOLDING
10455 linenr_T lnum;
10456 linenr_T first, last;
10457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10460 {
10461 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10462 {
10463 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 return;
10468 }
10469 }
10470#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010471 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472}
10473
10474/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010475 * "foldclosed()" function
10476 */
10477 static void
10478f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010479 typval_T *argvars;
10480 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010481{
10482 foldclosed_both(argvars, rettv, FALSE);
10483}
10484
10485/*
10486 * "foldclosedend()" function
10487 */
10488 static void
10489f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010490 typval_T *argvars;
10491 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010492{
10493 foldclosed_both(argvars, rettv, TRUE);
10494}
10495
10496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 * "foldlevel()" function
10498 */
10499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504#ifdef FEAT_FOLDING
10505 linenr_T lnum;
10506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010507 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511}
10512
10513/*
10514 * "foldtext()" function
10515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010518 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521#ifdef FEAT_FOLDING
10522 linenr_T lnum;
10523 char_u *s;
10524 char_u *r;
10525 int len;
10526 char *txt;
10527#endif
10528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 rettv->v_type = VAR_STRING;
10530 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10533 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10534 <= curbuf->b_ml.ml_line_count
10535 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 {
10537 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010538 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10539 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 {
10541 if (!linewhite(lnum))
10542 break;
10543 ++lnum;
10544 }
10545
10546 /* Find interesting text in this line. */
10547 s = skipwhite(ml_get(lnum));
10548 /* skip C comment-start */
10549 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010552 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010554 {
10555 s = skipwhite(ml_get(lnum + 1));
10556 if (*s == '*')
10557 s = skipwhite(s + 1);
10558 }
10559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 txt = _("+-%s%3ld lines: ");
10561 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 + 20 /* for %3ld */
10564 + STRLEN(s))); /* concatenated */
10565 if (r != NULL)
10566 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10568 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10569 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 len = (int)STRLEN(r);
10571 STRCAT(r, s);
10572 /* remove 'foldmarker' and 'commentstring' */
10573 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 }
10576 }
10577#endif
10578}
10579
10580/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010581 * "foldtextresult(lnum)" function
10582 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010585 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010587{
10588#ifdef FEAT_FOLDING
10589 linenr_T lnum;
10590 char_u *text;
10591 char_u buf[51];
10592 foldinfo_T foldinfo;
10593 int fold_count;
10594#endif
10595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 rettv->v_type = VAR_STRING;
10597 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010598#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 /* treat illegal types and illegal string values for {lnum} the same */
10601 if (lnum < 0)
10602 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010603 fold_count = foldedCount(curwin, lnum, &foldinfo);
10604 if (fold_count > 0)
10605 {
10606 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10607 &foldinfo, buf);
10608 if (text == buf)
10609 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010611 }
10612#endif
10613}
10614
10615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 * "foreground()" function
10617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010620 typval_T *argvars UNUSED;
10621 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623#ifdef FEAT_GUI
10624 if (gui.in_use)
10625 gui_mch_set_foreground();
10626#else
10627# ifdef WIN32
10628 win32_set_foreground();
10629# endif
10630#endif
10631}
10632
10633/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010634 * "function()" function
10635 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010638 typval_T *argvars;
10639 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010640{
10641 char_u *s;
10642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010644 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010645 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010646 /* Don't check an autoload name for existence here. */
10647 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010648 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010649 else
10650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 rettv->vval.v_string = vim_strsave(s);
10652 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010653 }
10654}
10655
10656/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010657 * "garbagecollect()" function
10658 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010659 static void
10660f_garbagecollect(argvars, rettv)
10661 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010662 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010663{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010664 /* This is postponed until we are back at the toplevel, because we may be
10665 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10666 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010667
10668 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10669 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010670}
10671
10672/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010673 * "get()" function
10674 */
10675 static void
10676f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 typval_T *argvars;
10678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010679{
Bram Moolenaar33570922005-01-25 22:26:29 +000010680 listitem_T *li;
10681 list_T *l;
10682 dictitem_T *di;
10683 dict_T *d;
10684 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010685
Bram Moolenaare9a41262005-01-15 22:18:47 +000010686 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010687 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 int error = FALSE;
10691
10692 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10693 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010694 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010697 else if (argvars[0].v_type == VAR_DICT)
10698 {
10699 if ((d = argvars[0].vval.v_dict) != NULL)
10700 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010701 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010702 if (di != NULL)
10703 tv = &di->di_tv;
10704 }
10705 }
10706 else
10707 EMSG2(_(e_listdictarg), "get()");
10708
10709 if (tv == NULL)
10710 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010712 copy_tv(&argvars[2], rettv);
10713 }
10714 else
10715 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716}
10717
Bram Moolenaar342337a2005-07-21 21:11:17 +000010718static 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 +000010719
10720/*
10721 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010722 * Return a range (from start to end) of lines in rettv from the specified
10723 * buffer.
10724 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010725 */
10726 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010727get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010728 buf_T *buf;
10729 linenr_T start;
10730 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010731 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010732 typval_T *rettv;
10733{
10734 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010735
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010736 if (retlist && rettv_list_alloc(rettv) == FAIL)
10737 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010738
10739 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10740 return;
10741
10742 if (!retlist)
10743 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010744 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10745 p = ml_get_buf(buf, start, FALSE);
10746 else
10747 p = (char_u *)"";
10748
10749 rettv->v_type = VAR_STRING;
10750 rettv->vval.v_string = vim_strsave(p);
10751 }
10752 else
10753 {
10754 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010755 return;
10756
10757 if (start < 1)
10758 start = 1;
10759 if (end > buf->b_ml.ml_line_count)
10760 end = buf->b_ml.ml_line_count;
10761 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010762 if (list_append_string(rettv->vval.v_list,
10763 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010764 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010765 }
10766}
10767
10768/*
10769 * "getbufline()" function
10770 */
10771 static void
10772f_getbufline(argvars, rettv)
10773 typval_T *argvars;
10774 typval_T *rettv;
10775{
10776 linenr_T lnum;
10777 linenr_T end;
10778 buf_T *buf;
10779
10780 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10781 ++emsg_off;
10782 buf = get_buf_tv(&argvars[0]);
10783 --emsg_off;
10784
Bram Moolenaar661b1822005-07-28 22:36:45 +000010785 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010786 if (argvars[2].v_type == VAR_UNKNOWN)
10787 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010788 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010789 end = get_tv_lnum_buf(&argvars[2], buf);
10790
Bram Moolenaar342337a2005-07-21 21:11:17 +000010791 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010792}
10793
Bram Moolenaar0d660222005-01-07 21:51:51 +000010794/*
10795 * "getbufvar()" function
10796 */
10797 static void
10798f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 typval_T *argvars;
10800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801{
10802 buf_T *buf;
10803 buf_T *save_curbuf;
10804 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010805 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10808 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010809 ++emsg_off;
10810 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811
10812 rettv->v_type = VAR_STRING;
10813 rettv->vval.v_string = NULL;
10814
10815 if (buf != NULL && varname != NULL)
10816 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010817 /* set curbuf to be our buf, temporarily */
10818 save_curbuf = curbuf;
10819 curbuf = buf;
10820
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010823 else
10824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010825 if (*varname == NUL)
10826 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10827 * scope prefix before the NUL byte is required by
10828 * find_var_in_ht(). */
10829 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010831 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010834 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010835
10836 /* restore previous notion of curbuf */
10837 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010838 }
10839
10840 --emsg_off;
10841}
10842
10843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 * "getchar()" function
10845 */
10846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010848 typval_T *argvars;
10849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850{
10851 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010852 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010854 /* Position the cursor. Needed after a message that ends in a space. */
10855 windgoto(msg_row, msg_col);
10856
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 ++no_mapping;
10858 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010859 for (;;)
10860 {
10861 if (argvars[0].v_type == VAR_UNKNOWN)
10862 /* getchar(): blocking wait. */
10863 n = safe_vgetc();
10864 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10865 /* getchar(1): only check if char avail */
10866 n = vpeekc();
10867 else if (error || vpeekc() == NUL)
10868 /* illegal argument or getchar(0) and no char avail: return zero */
10869 n = 0;
10870 else
10871 /* getchar(0) and char avail: return char */
10872 n = safe_vgetc();
10873 if (n == K_IGNORE)
10874 continue;
10875 break;
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 --no_mapping;
10878 --allow_keys;
10879
Bram Moolenaar219b8702006-11-01 14:32:36 +000010880 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10881 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10882 vimvars[VV_MOUSE_COL].vv_nr = 0;
10883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 if (IS_SPECIAL(n) || mod_mask != 0)
10886 {
10887 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10888 int i = 0;
10889
10890 /* Turn a special key into three bytes, plus modifier. */
10891 if (mod_mask != 0)
10892 {
10893 temp[i++] = K_SPECIAL;
10894 temp[i++] = KS_MODIFIER;
10895 temp[i++] = mod_mask;
10896 }
10897 if (IS_SPECIAL(n))
10898 {
10899 temp[i++] = K_SPECIAL;
10900 temp[i++] = K_SECOND(n);
10901 temp[i++] = K_THIRD(n);
10902 }
10903#ifdef FEAT_MBYTE
10904 else if (has_mbyte)
10905 i += (*mb_char2bytes)(n, temp + i);
10906#endif
10907 else
10908 temp[i++] = n;
10909 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 rettv->v_type = VAR_STRING;
10911 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010912
10913#ifdef FEAT_MOUSE
10914 if (n == K_LEFTMOUSE
10915 || n == K_LEFTMOUSE_NM
10916 || n == K_LEFTDRAG
10917 || n == K_LEFTRELEASE
10918 || n == K_LEFTRELEASE_NM
10919 || n == K_MIDDLEMOUSE
10920 || n == K_MIDDLEDRAG
10921 || n == K_MIDDLERELEASE
10922 || n == K_RIGHTMOUSE
10923 || n == K_RIGHTDRAG
10924 || n == K_RIGHTRELEASE
10925 || n == K_X1MOUSE
10926 || n == K_X1DRAG
10927 || n == K_X1RELEASE
10928 || n == K_X2MOUSE
10929 || n == K_X2DRAG
10930 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010931 || n == K_MOUSELEFT
10932 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010933 || n == K_MOUSEDOWN
10934 || n == K_MOUSEUP)
10935 {
10936 int row = mouse_row;
10937 int col = mouse_col;
10938 win_T *win;
10939 linenr_T lnum;
10940# ifdef FEAT_WINDOWS
10941 win_T *wp;
10942# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010943 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010944
10945 if (row >= 0 && col >= 0)
10946 {
10947 /* Find the window at the mouse coordinates and compute the
10948 * text position. */
10949 win = mouse_find_win(&row, &col);
10950 (void)mouse_comp_pos(win, &row, &col, &lnum);
10951# ifdef FEAT_WINDOWS
10952 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010953 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010954# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010955 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010956 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10957 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10958 }
10959 }
10960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 }
10962}
10963
10964/*
10965 * "getcharmod()" function
10966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010969 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973}
10974
10975/*
10976 * "getcmdline()" function
10977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983 rettv->v_type = VAR_STRING;
10984 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
10988 * "getcmdpos()" function
10989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010992 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996}
10997
10998/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010999 * "getcmdtype()" function
11000 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011001 static void
11002f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011003 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011004 typval_T *rettv;
11005{
11006 rettv->v_type = VAR_STRING;
11007 rettv->vval.v_string = alloc(2);
11008 if (rettv->vval.v_string != NULL)
11009 {
11010 rettv->vval.v_string[0] = get_cmdline_type();
11011 rettv->vval.v_string[1] = NUL;
11012 }
11013}
11014
11015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 * "getcwd()" function
11017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011020 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022{
11023 char_u cwd[MAXPATHL];
11024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 else
11029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011032 if (rettv->vval.v_string != NULL)
11033 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034#endif
11035 }
11036}
11037
11038/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011039 * "getfontname()" function
11040 */
11041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->v_type = VAR_STRING;
11047 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011048#ifdef FEAT_GUI
11049 if (gui.in_use)
11050 {
11051 GuiFont font;
11052 char_u *name = NULL;
11053
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011054 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011055 {
11056 /* Get the "Normal" font. Either the name saved by
11057 * hl_set_font_name() or from the font ID. */
11058 font = gui.norm_font;
11059 name = hl_get_font_name();
11060 }
11061 else
11062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011064 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11065 return;
11066 font = gui_mch_get_font(name, FALSE);
11067 if (font == NOFONT)
11068 return; /* Invalid font name, return empty string. */
11069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011071 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011072 gui_mch_free_font(font);
11073 }
11074#endif
11075}
11076
11077/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011078 * "getfperm({fname})" function
11079 */
11080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 typval_T *argvars;
11083 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011084{
11085 char_u *fname;
11086 struct stat st;
11087 char_u *perm = NULL;
11088 char_u flags[] = "rwx";
11089 int i;
11090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011094 if (mch_stat((char *)fname, &st) >= 0)
11095 {
11096 perm = vim_strsave((char_u *)"---------");
11097 if (perm != NULL)
11098 {
11099 for (i = 0; i < 9; i++)
11100 {
11101 if (st.st_mode & (1 << (8 - i)))
11102 perm[i] = flags[i % 3];
11103 }
11104 }
11105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011107}
11108
11109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 * "getfsize({fname})" function
11111 */
11112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 typval_T *argvars;
11115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116{
11117 char_u *fname;
11118 struct stat st;
11119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123
11124 if (mch_stat((char *)fname, &st) >= 0)
11125 {
11126 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011131
11132 /* non-perfect check for overflow */
11133 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11134 rettv->vval.v_number = -2;
11135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 }
11137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140
11141/*
11142 * "getftime({fname})" function
11143 */
11144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 typval_T *argvars;
11147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148{
11149 char_u *fname;
11150 struct stat st;
11151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
11154 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159
11160/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011161 * "getftype({fname})" function
11162 */
11163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *argvars;
11166 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011167{
11168 char_u *fname;
11169 struct stat st;
11170 char_u *type = NULL;
11171 char *t;
11172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011176 if (mch_lstat((char *)fname, &st) >= 0)
11177 {
11178#ifdef S_ISREG
11179 if (S_ISREG(st.st_mode))
11180 t = "file";
11181 else if (S_ISDIR(st.st_mode))
11182 t = "dir";
11183# ifdef S_ISLNK
11184 else if (S_ISLNK(st.st_mode))
11185 t = "link";
11186# endif
11187# ifdef S_ISBLK
11188 else if (S_ISBLK(st.st_mode))
11189 t = "bdev";
11190# endif
11191# ifdef S_ISCHR
11192 else if (S_ISCHR(st.st_mode))
11193 t = "cdev";
11194# endif
11195# ifdef S_ISFIFO
11196 else if (S_ISFIFO(st.st_mode))
11197 t = "fifo";
11198# endif
11199# ifdef S_ISSOCK
11200 else if (S_ISSOCK(st.st_mode))
11201 t = "fifo";
11202# endif
11203 else
11204 t = "other";
11205#else
11206# ifdef S_IFMT
11207 switch (st.st_mode & S_IFMT)
11208 {
11209 case S_IFREG: t = "file"; break;
11210 case S_IFDIR: t = "dir"; break;
11211# ifdef S_IFLNK
11212 case S_IFLNK: t = "link"; break;
11213# endif
11214# ifdef S_IFBLK
11215 case S_IFBLK: t = "bdev"; break;
11216# endif
11217# ifdef S_IFCHR
11218 case S_IFCHR: t = "cdev"; break;
11219# endif
11220# ifdef S_IFIFO
11221 case S_IFIFO: t = "fifo"; break;
11222# endif
11223# ifdef S_IFSOCK
11224 case S_IFSOCK: t = "socket"; break;
11225# endif
11226 default: t = "other";
11227 }
11228# else
11229 if (mch_isdir(fname))
11230 t = "dir";
11231 else
11232 t = "file";
11233# endif
11234#endif
11235 type = vim_strsave((char_u *)t);
11236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011238}
11239
11240/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011241 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242 */
11243 static void
11244f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011245 typval_T *argvars;
11246 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247{
11248 linenr_T lnum;
11249 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011250 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011251
11252 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011253 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011254 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011255 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011256 retlist = FALSE;
11257 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011258 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011259 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011260 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011261 retlist = TRUE;
11262 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011263
Bram Moolenaar342337a2005-07-21 21:11:17 +000011264 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011265}
11266
11267/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011268 * "getmatches()" function
11269 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011270 static void
11271f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011272 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011273 typval_T *rettv;
11274{
11275#ifdef FEAT_SEARCH_EXTRA
11276 dict_T *dict;
11277 matchitem_T *cur = curwin->w_match_head;
11278
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011279 if (rettv_list_alloc(rettv) == OK)
11280 {
11281 while (cur != NULL)
11282 {
11283 dict = dict_alloc();
11284 if (dict == NULL)
11285 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011286 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11287 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11288 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11289 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11290 list_append_dict(rettv->vval.v_list, dict);
11291 cur = cur->next;
11292 }
11293 }
11294#endif
11295}
11296
11297/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011298 * "getpid()" function
11299 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011300 static void
11301f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011302 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011303 typval_T *rettv;
11304{
11305 rettv->vval.v_number = mch_get_pid();
11306}
11307
11308/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011309 * "getpos(string)" function
11310 */
11311 static void
11312f_getpos(argvars, rettv)
11313 typval_T *argvars;
11314 typval_T *rettv;
11315{
11316 pos_T *fp;
11317 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011318 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011319
11320 if (rettv_list_alloc(rettv) == OK)
11321 {
11322 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011323 fp = var2fpos(&argvars[0], TRUE, &fnum);
11324 if (fnum != -1)
11325 list_append_number(l, (varnumber_T)fnum);
11326 else
11327 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011328 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11329 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011330 list_append_number(l, (fp != NULL)
11331 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011332 : (varnumber_T)0);
11333 list_append_number(l,
11334#ifdef FEAT_VIRTUALEDIT
11335 (fp != NULL) ? (varnumber_T)fp->coladd :
11336#endif
11337 (varnumber_T)0);
11338 }
11339 else
11340 rettv->vval.v_number = FALSE;
11341}
11342
11343/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011344 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011345 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011346 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011347f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011348 typval_T *argvars UNUSED;
11349 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011350{
11351#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011352 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011353#endif
11354
Bram Moolenaar2641f772005-03-25 21:58:17 +000011355#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011356 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011357 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011358 wp = NULL;
11359 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11360 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011361 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011362 if (wp == NULL)
11363 return;
11364 }
11365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011366 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011367 }
11368#endif
11369}
11370
11371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 * "getreg()" function
11373 */
11374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378{
11379 char_u *strregname;
11380 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011381 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011384 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 strregname = get_tv_string_chk(&argvars[0]);
11387 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011388 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011392 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 regname = (strregname == NULL ? '"' : *strregname);
11394 if (regname == 0)
11395 regname = '"';
11396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 rettv->vval.v_string = error ? NULL :
11399 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400}
11401
11402/*
11403 * "getregtype()" function
11404 */
11405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011407 typval_T *argvars;
11408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409{
11410 char_u *strregname;
11411 int regname;
11412 char_u buf[NUMBUFLEN + 2];
11413 long reglen = 0;
11414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011415 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 {
11417 strregname = get_tv_string_chk(&argvars[0]);
11418 if (strregname == NULL) /* type error; errmsg already given */
11419 {
11420 rettv->v_type = VAR_STRING;
11421 rettv->vval.v_string = NULL;
11422 return;
11423 }
11424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 else
11426 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011427 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
11429 regname = (strregname == NULL ? '"' : *strregname);
11430 if (regname == 0)
11431 regname = '"';
11432
11433 buf[0] = NUL;
11434 buf[1] = NUL;
11435 switch (get_reg_type(regname, &reglen))
11436 {
11437 case MLINE: buf[0] = 'V'; break;
11438 case MCHAR: buf[0] = 'v'; break;
11439#ifdef FEAT_VISUAL
11440 case MBLOCK:
11441 buf[0] = Ctrl_V;
11442 sprintf((char *)buf + 1, "%ld", reglen + 1);
11443 break;
11444#endif
11445 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_STRING;
11447 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448}
11449
11450/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011451 * "gettabvar()" function
11452 */
11453 static void
11454f_gettabvar(argvars, rettv)
11455 typval_T *argvars;
11456 typval_T *rettv;
11457{
11458 tabpage_T *tp;
11459 dictitem_T *v;
11460 char_u *varname;
11461
11462 rettv->v_type = VAR_STRING;
11463 rettv->vval.v_string = NULL;
11464
11465 varname = get_tv_string_chk(&argvars[1]);
11466 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11467 if (tp != NULL && varname != NULL)
11468 {
11469 /* look up the variable */
11470 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11471 if (v != NULL)
11472 copy_tv(&v->di_tv, rettv);
11473 }
11474}
11475
11476/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011477 * "gettabwinvar()" function
11478 */
11479 static void
11480f_gettabwinvar(argvars, rettv)
11481 typval_T *argvars;
11482 typval_T *rettv;
11483{
11484 getwinvar(argvars, rettv, 1);
11485}
11486
11487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 * "getwinposx()" function
11489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496#ifdef FEAT_GUI
11497 if (gui.in_use)
11498 {
11499 int x, y;
11500
11501 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 }
11504#endif
11505}
11506
11507/*
11508 * "getwinposy()" function
11509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011512 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516#ifdef FEAT_GUI
11517 if (gui.in_use)
11518 {
11519 int x, y;
11520
11521 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 }
11524#endif
11525}
11526
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011527/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011528 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011529 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011530 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011531find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011532 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011533 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011534{
11535#ifdef FEAT_WINDOWS
11536 win_T *wp;
11537#endif
11538 int nr;
11539
11540 nr = get_tv_number_chk(vp, NULL);
11541
11542#ifdef FEAT_WINDOWS
11543 if (nr < 0)
11544 return NULL;
11545 if (nr == 0)
11546 return curwin;
11547
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011548 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11549 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011550 if (--nr <= 0)
11551 break;
11552 return wp;
11553#else
11554 if (nr == 0 || nr == 1)
11555 return curwin;
11556 return NULL;
11557#endif
11558}
11559
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560/*
11561 * "getwinvar()" function
11562 */
11563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 typval_T *argvars;
11566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011568 getwinvar(argvars, rettv, 0);
11569}
11570
11571/*
11572 * getwinvar() and gettabwinvar()
11573 */
11574 static void
11575getwinvar(argvars, rettv, off)
11576 typval_T *argvars;
11577 typval_T *rettv;
11578 int off; /* 1 for gettabwinvar() */
11579{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 win_T *win, *oldcurwin;
11581 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011582 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011583 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011585#ifdef FEAT_WINDOWS
11586 if (off == 1)
11587 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11588 else
11589 tp = curtab;
11590#endif
11591 win = find_win_by_nr(&argvars[off], tp);
11592 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011593 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->v_type = VAR_STRING;
11596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597
11598 if (win != NULL && varname != NULL)
11599 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011600 /* Set curwin to be our win, temporarily. Also set curbuf, so
11601 * that we can get buffer-local options. */
11602 oldcurwin = curwin;
11603 curwin = win;
11604 curbuf = win->w_buffer;
11605
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 else
11609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 if (*varname == NUL)
11611 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11612 * scope prefix before the NUL byte is required by
11613 * find_var_in_ht(). */
11614 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011616 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011618 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011620
11621 /* restore previous notion of curwin */
11622 curwin = oldcurwin;
11623 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 }
11625
11626 --emsg_off;
11627}
11628
11629/*
11630 * "glob()" function
11631 */
11632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 typval_T *argvars;
11635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011637 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011639 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011641 /* When the optional second argument is non-zero, don't remove matches
11642 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11643 if (argvars[1].v_type != VAR_UNKNOWN
11644 && get_tv_number_chk(&argvars[1], &error))
11645 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011647 if (!error)
11648 {
11649 ExpandInit(&xpc);
11650 xpc.xp_context = EXPAND_FILES;
11651 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11652 NULL, flags, WILD_ALL);
11653 }
11654 else
11655 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656}
11657
11658/*
11659 * "globpath()" function
11660 */
11661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *argvars;
11664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011666 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011669 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011671 /* When the optional second argument is non-zero, don't remove matches
11672 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11673 if (argvars[2].v_type != VAR_UNKNOWN
11674 && get_tv_number_chk(&argvars[2], &error))
11675 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011677 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 rettv->vval.v_string = NULL;
11679 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011680 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11681 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
11684/*
11685 * "has()" function
11686 */
11687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011689 typval_T *argvars;
11690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691{
11692 int i;
11693 char_u *name;
11694 int n = FALSE;
11695 static char *(has_list[]) =
11696 {
11697#ifdef AMIGA
11698 "amiga",
11699# ifdef FEAT_ARP
11700 "arp",
11701# endif
11702#endif
11703#ifdef __BEOS__
11704 "beos",
11705#endif
11706#ifdef MSDOS
11707# ifdef DJGPP
11708 "dos32",
11709# else
11710 "dos16",
11711# endif
11712#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011713#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 "mac",
11715#endif
11716#if defined(MACOS_X_UNIX)
11717 "macunix",
11718#endif
11719#ifdef OS2
11720 "os2",
11721#endif
11722#ifdef __QNX__
11723 "qnx",
11724#endif
11725#ifdef RISCOS
11726 "riscos",
11727#endif
11728#ifdef UNIX
11729 "unix",
11730#endif
11731#ifdef VMS
11732 "vms",
11733#endif
11734#ifdef WIN16
11735 "win16",
11736#endif
11737#ifdef WIN32
11738 "win32",
11739#endif
11740#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11741 "win32unix",
11742#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011743#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 "win64",
11745#endif
11746#ifdef EBCDIC
11747 "ebcdic",
11748#endif
11749#ifndef CASE_INSENSITIVE_FILENAME
11750 "fname_case",
11751#endif
11752#ifdef FEAT_ARABIC
11753 "arabic",
11754#endif
11755#ifdef FEAT_AUTOCMD
11756 "autocmd",
11757#endif
11758#ifdef FEAT_BEVAL
11759 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011760# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11761 "balloon_multiline",
11762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763#endif
11764#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11765 "builtin_terms",
11766# ifdef ALL_BUILTIN_TCAPS
11767 "all_builtin_terms",
11768# endif
11769#endif
11770#ifdef FEAT_BYTEOFF
11771 "byte_offset",
11772#endif
11773#ifdef FEAT_CINDENT
11774 "cindent",
11775#endif
11776#ifdef FEAT_CLIENTSERVER
11777 "clientserver",
11778#endif
11779#ifdef FEAT_CLIPBOARD
11780 "clipboard",
11781#endif
11782#ifdef FEAT_CMDL_COMPL
11783 "cmdline_compl",
11784#endif
11785#ifdef FEAT_CMDHIST
11786 "cmdline_hist",
11787#endif
11788#ifdef FEAT_COMMENTS
11789 "comments",
11790#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011791#ifdef FEAT_CONCEAL
11792 "conceal",
11793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794#ifdef FEAT_CRYPT
11795 "cryptv",
11796#endif
11797#ifdef FEAT_CSCOPE
11798 "cscope",
11799#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011800#ifdef FEAT_CURSORBIND
11801 "cursorbind",
11802#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011803#ifdef CURSOR_SHAPE
11804 "cursorshape",
11805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806#ifdef DEBUG
11807 "debug",
11808#endif
11809#ifdef FEAT_CON_DIALOG
11810 "dialog_con",
11811#endif
11812#ifdef FEAT_GUI_DIALOG
11813 "dialog_gui",
11814#endif
11815#ifdef FEAT_DIFF
11816 "diff",
11817#endif
11818#ifdef FEAT_DIGRAPHS
11819 "digraphs",
11820#endif
11821#ifdef FEAT_DND
11822 "dnd",
11823#endif
11824#ifdef FEAT_EMACS_TAGS
11825 "emacs_tags",
11826#endif
11827 "eval", /* always present, of course! */
11828#ifdef FEAT_EX_EXTRA
11829 "ex_extra",
11830#endif
11831#ifdef FEAT_SEARCH_EXTRA
11832 "extra_search",
11833#endif
11834#ifdef FEAT_FKMAP
11835 "farsi",
11836#endif
11837#ifdef FEAT_SEARCHPATH
11838 "file_in_path",
11839#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011840#if defined(UNIX) && !defined(USE_SYSTEM)
11841 "filterpipe",
11842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843#ifdef FEAT_FIND_ID
11844 "find_in_path",
11845#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011846#ifdef FEAT_FLOAT
11847 "float",
11848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849#ifdef FEAT_FOLDING
11850 "folding",
11851#endif
11852#ifdef FEAT_FOOTER
11853 "footer",
11854#endif
11855#if !defined(USE_SYSTEM) && defined(UNIX)
11856 "fork",
11857#endif
11858#ifdef FEAT_GETTEXT
11859 "gettext",
11860#endif
11861#ifdef FEAT_GUI
11862 "gui",
11863#endif
11864#ifdef FEAT_GUI_ATHENA
11865# ifdef FEAT_GUI_NEXTAW
11866 "gui_neXtaw",
11867# else
11868 "gui_athena",
11869# endif
11870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871#ifdef FEAT_GUI_GTK
11872 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011875#ifdef FEAT_GUI_GNOME
11876 "gui_gnome",
11877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878#ifdef FEAT_GUI_MAC
11879 "gui_mac",
11880#endif
11881#ifdef FEAT_GUI_MOTIF
11882 "gui_motif",
11883#endif
11884#ifdef FEAT_GUI_PHOTON
11885 "gui_photon",
11886#endif
11887#ifdef FEAT_GUI_W16
11888 "gui_win16",
11889#endif
11890#ifdef FEAT_GUI_W32
11891 "gui_win32",
11892#endif
11893#ifdef FEAT_HANGULIN
11894 "hangul_input",
11895#endif
11896#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11897 "iconv",
11898#endif
11899#ifdef FEAT_INS_EXPAND
11900 "insert_expand",
11901#endif
11902#ifdef FEAT_JUMPLIST
11903 "jumplist",
11904#endif
11905#ifdef FEAT_KEYMAP
11906 "keymap",
11907#endif
11908#ifdef FEAT_LANGMAP
11909 "langmap",
11910#endif
11911#ifdef FEAT_LIBCALL
11912 "libcall",
11913#endif
11914#ifdef FEAT_LINEBREAK
11915 "linebreak",
11916#endif
11917#ifdef FEAT_LISP
11918 "lispindent",
11919#endif
11920#ifdef FEAT_LISTCMDS
11921 "listcmds",
11922#endif
11923#ifdef FEAT_LOCALMAP
11924 "localmap",
11925#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011926#ifdef FEAT_LUA
11927# ifndef DYNAMIC_LUA
11928 "lua",
11929# endif
11930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931#ifdef FEAT_MENU
11932 "menu",
11933#endif
11934#ifdef FEAT_SESSION
11935 "mksession",
11936#endif
11937#ifdef FEAT_MODIFY_FNAME
11938 "modify_fname",
11939#endif
11940#ifdef FEAT_MOUSE
11941 "mouse",
11942#endif
11943#ifdef FEAT_MOUSESHAPE
11944 "mouseshape",
11945#endif
11946#if defined(UNIX) || defined(VMS)
11947# ifdef FEAT_MOUSE_DEC
11948 "mouse_dec",
11949# endif
11950# ifdef FEAT_MOUSE_GPM
11951 "mouse_gpm",
11952# endif
11953# ifdef FEAT_MOUSE_JSB
11954 "mouse_jsbterm",
11955# endif
11956# ifdef FEAT_MOUSE_NET
11957 "mouse_netterm",
11958# endif
11959# ifdef FEAT_MOUSE_PTERM
11960 "mouse_pterm",
11961# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011962# ifdef FEAT_SYSMOUSE
11963 "mouse_sysmouse",
11964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965# ifdef FEAT_MOUSE_XTERM
11966 "mouse_xterm",
11967# endif
11968#endif
11969#ifdef FEAT_MBYTE
11970 "multi_byte",
11971#endif
11972#ifdef FEAT_MBYTE_IME
11973 "multi_byte_ime",
11974#endif
11975#ifdef FEAT_MULTI_LANG
11976 "multi_lang",
11977#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011978#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011979#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011980 "mzscheme",
11981#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983#ifdef FEAT_OLE
11984 "ole",
11985#endif
11986#ifdef FEAT_OSFILETYPE
11987 "osfiletype",
11988#endif
11989#ifdef FEAT_PATH_EXTRA
11990 "path_extra",
11991#endif
11992#ifdef FEAT_PERL
11993#ifndef DYNAMIC_PERL
11994 "perl",
11995#endif
11996#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011997#ifdef FEAT_PERSISTENT_UNDO
11998 "persistent_undo",
11999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000#ifdef FEAT_PYTHON
12001#ifndef DYNAMIC_PYTHON
12002 "python",
12003#endif
12004#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012005#ifdef FEAT_PYTHON3
12006#ifndef DYNAMIC_PYTHON3
12007 "python3",
12008#endif
12009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010#ifdef FEAT_POSTSCRIPT
12011 "postscript",
12012#endif
12013#ifdef FEAT_PRINTER
12014 "printer",
12015#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012016#ifdef FEAT_PROFILE
12017 "profile",
12018#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012019#ifdef FEAT_RELTIME
12020 "reltime",
12021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022#ifdef FEAT_QUICKFIX
12023 "quickfix",
12024#endif
12025#ifdef FEAT_RIGHTLEFT
12026 "rightleft",
12027#endif
12028#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12029 "ruby",
12030#endif
12031#ifdef FEAT_SCROLLBIND
12032 "scrollbind",
12033#endif
12034#ifdef FEAT_CMDL_INFO
12035 "showcmd",
12036 "cmdline_info",
12037#endif
12038#ifdef FEAT_SIGNS
12039 "signs",
12040#endif
12041#ifdef FEAT_SMARTINDENT
12042 "smartindent",
12043#endif
12044#ifdef FEAT_SNIFF
12045 "sniff",
12046#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012047#ifdef STARTUPTIME
12048 "startuptime",
12049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050#ifdef FEAT_STL_OPT
12051 "statusline",
12052#endif
12053#ifdef FEAT_SUN_WORKSHOP
12054 "sun_workshop",
12055#endif
12056#ifdef FEAT_NETBEANS_INTG
12057 "netbeans_intg",
12058#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012059#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012060 "spell",
12061#endif
12062#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 "syntax",
12064#endif
12065#if defined(USE_SYSTEM) || !defined(UNIX)
12066 "system",
12067#endif
12068#ifdef FEAT_TAG_BINS
12069 "tag_binary",
12070#endif
12071#ifdef FEAT_TAG_OLDSTATIC
12072 "tag_old_static",
12073#endif
12074#ifdef FEAT_TAG_ANYWHITE
12075 "tag_any_white",
12076#endif
12077#ifdef FEAT_TCL
12078# ifndef DYNAMIC_TCL
12079 "tcl",
12080# endif
12081#endif
12082#ifdef TERMINFO
12083 "terminfo",
12084#endif
12085#ifdef FEAT_TERMRESPONSE
12086 "termresponse",
12087#endif
12088#ifdef FEAT_TEXTOBJ
12089 "textobjects",
12090#endif
12091#ifdef HAVE_TGETENT
12092 "tgetent",
12093#endif
12094#ifdef FEAT_TITLE
12095 "title",
12096#endif
12097#ifdef FEAT_TOOLBAR
12098 "toolbar",
12099#endif
12100#ifdef FEAT_USR_CMDS
12101 "user-commands", /* was accidentally included in 5.4 */
12102 "user_commands",
12103#endif
12104#ifdef FEAT_VIMINFO
12105 "viminfo",
12106#endif
12107#ifdef FEAT_VERTSPLIT
12108 "vertsplit",
12109#endif
12110#ifdef FEAT_VIRTUALEDIT
12111 "virtualedit",
12112#endif
12113#ifdef FEAT_VISUAL
12114 "visual",
12115#endif
12116#ifdef FEAT_VISUALEXTRA
12117 "visualextra",
12118#endif
12119#ifdef FEAT_VREPLACE
12120 "vreplace",
12121#endif
12122#ifdef FEAT_WILDIGN
12123 "wildignore",
12124#endif
12125#ifdef FEAT_WILDMENU
12126 "wildmenu",
12127#endif
12128#ifdef FEAT_WINDOWS
12129 "windows",
12130#endif
12131#ifdef FEAT_WAK
12132 "winaltkeys",
12133#endif
12134#ifdef FEAT_WRITEBACKUP
12135 "writebackup",
12136#endif
12137#ifdef FEAT_XIM
12138 "xim",
12139#endif
12140#ifdef FEAT_XFONTSET
12141 "xfontset",
12142#endif
12143#ifdef USE_XSMP
12144 "xsmp",
12145#endif
12146#ifdef USE_XSMP_INTERACT
12147 "xsmp_interact",
12148#endif
12149#ifdef FEAT_XCLIPBOARD
12150 "xterm_clipboard",
12151#endif
12152#ifdef FEAT_XTERM_SAVE
12153 "xterm_save",
12154#endif
12155#if defined(UNIX) && defined(FEAT_X11)
12156 "X11",
12157#endif
12158 NULL
12159 };
12160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 for (i = 0; has_list[i] != NULL; ++i)
12163 if (STRICMP(name, has_list[i]) == 0)
12164 {
12165 n = TRUE;
12166 break;
12167 }
12168
12169 if (n == FALSE)
12170 {
12171 if (STRNICMP(name, "patch", 5) == 0)
12172 n = has_patch(atoi((char *)name + 5));
12173 else if (STRICMP(name, "vim_starting") == 0)
12174 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012175#ifdef FEAT_MBYTE
12176 else if (STRICMP(name, "multi_byte_encoding") == 0)
12177 n = has_mbyte;
12178#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012179#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12180 else if (STRICMP(name, "balloon_multiline") == 0)
12181 n = multiline_balloon_available();
12182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183#ifdef DYNAMIC_TCL
12184 else if (STRICMP(name, "tcl") == 0)
12185 n = tcl_enabled(FALSE);
12186#endif
12187#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12188 else if (STRICMP(name, "iconv") == 0)
12189 n = iconv_enabled(FALSE);
12190#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012191#ifdef DYNAMIC_LUA
12192 else if (STRICMP(name, "lua") == 0)
12193 n = lua_enabled(FALSE);
12194#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012195#ifdef DYNAMIC_MZSCHEME
12196 else if (STRICMP(name, "mzscheme") == 0)
12197 n = mzscheme_enabled(FALSE);
12198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199#ifdef DYNAMIC_RUBY
12200 else if (STRICMP(name, "ruby") == 0)
12201 n = ruby_enabled(FALSE);
12202#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012203#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef DYNAMIC_PYTHON
12205 else if (STRICMP(name, "python") == 0)
12206 n = python_enabled(FALSE);
12207#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012208#endif
12209#ifdef FEAT_PYTHON3
12210#ifdef DYNAMIC_PYTHON3
12211 else if (STRICMP(name, "python3") == 0)
12212 n = python3_enabled(FALSE);
12213#endif
12214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215#ifdef DYNAMIC_PERL
12216 else if (STRICMP(name, "perl") == 0)
12217 n = perl_enabled(FALSE);
12218#endif
12219#ifdef FEAT_GUI
12220 else if (STRICMP(name, "gui_running") == 0)
12221 n = (gui.in_use || gui.starting);
12222# ifdef FEAT_GUI_W32
12223 else if (STRICMP(name, "gui_win32s") == 0)
12224 n = gui_is_win32s();
12225# endif
12226# ifdef FEAT_BROWSE
12227 else if (STRICMP(name, "browse") == 0)
12228 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12229# endif
12230#endif
12231#ifdef FEAT_SYN_HL
12232 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012233 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#endif
12235#if defined(WIN3264)
12236 else if (STRICMP(name, "win95") == 0)
12237 n = mch_windows95();
12238#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012239#ifdef FEAT_NETBEANS_INTG
12240 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012241 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 }
12244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246}
12247
12248/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012249 * "has_key()" function
12250 */
12251 static void
12252f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 typval_T *argvars;
12254 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012255{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012256 if (argvars[0].v_type != VAR_DICT)
12257 {
12258 EMSG(_(e_dictreq));
12259 return;
12260 }
12261 if (argvars[0].vval.v_dict == NULL)
12262 return;
12263
12264 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012265 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012266}
12267
12268/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012269 * "haslocaldir()" function
12270 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012271 static void
12272f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012273 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012274 typval_T *rettv;
12275{
12276 rettv->vval.v_number = (curwin->w_localdir != NULL);
12277}
12278
12279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 * "hasmapto()" function
12281 */
12282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 typval_T *argvars;
12285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286{
12287 char_u *name;
12288 char_u *mode;
12289 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012290 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012293 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 mode = (char_u *)"nvo";
12295 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012298 if (argvars[2].v_type != VAR_UNKNOWN)
12299 abbr = get_tv_number(&argvars[2]);
12300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301
Bram Moolenaar2c932302006-03-18 21:42:09 +000012302 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012305 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306}
12307
12308/*
12309 * "histadd()" function
12310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012313 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315{
12316#ifdef FEAT_CMDHIST
12317 int histype;
12318 char_u *str;
12319 char_u buf[NUMBUFLEN];
12320#endif
12321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 if (check_restricted() || check_secure())
12324 return;
12325#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012326 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12327 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 if (histype >= 0)
12329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 if (*str != NUL)
12332 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012333 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 return;
12337 }
12338 }
12339#endif
12340}
12341
12342/*
12343 * "histdel()" function
12344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012347 typval_T *argvars UNUSED;
12348 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349{
12350#ifdef FEAT_CMDHIST
12351 int n;
12352 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012353 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012355 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12356 if (str == NULL)
12357 n = 0;
12358 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012360 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012361 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012363 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 else
12366 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 get_tv_string_buf(&argvars[1], buf));
12369 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370#endif
12371}
12372
12373/*
12374 * "histget()" function
12375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012378 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380{
12381#ifdef FEAT_CMDHIST
12382 int type;
12383 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12387 if (str == NULL)
12388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 {
12391 type = get_histtype(str);
12392 if (argvars[1].v_type == VAR_UNKNOWN)
12393 idx = get_history_idx(type);
12394 else
12395 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12396 /* -1 on type error */
12397 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403}
12404
12405/*
12406 * "histnr()" function
12407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412{
12413 int i;
12414
12415#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012416 char_u *history = get_tv_string_chk(&argvars[0]);
12417
12418 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 if (i >= HIST_CMD && i < HIST_COUNT)
12420 i = get_history_idx(i);
12421 else
12422#endif
12423 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425}
12426
12427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 * "highlightID(name)" function
12429 */
12430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436}
12437
12438/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012439 * "highlight_exists()" function
12440 */
12441 static void
12442f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012443 typval_T *argvars;
12444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445{
12446 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12447}
12448
12449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 * "hostname()" function
12451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012454 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456{
12457 char_u hostname[256];
12458
12459 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->v_type = VAR_STRING;
12461 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462}
12463
12464/*
12465 * iconv() function
12466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012469 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471{
12472#ifdef FEAT_MBYTE
12473 char_u buf1[NUMBUFLEN];
12474 char_u buf2[NUMBUFLEN];
12475 char_u *from, *to, *str;
12476 vimconv_T vimconv;
12477#endif
12478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->v_type = VAR_STRING;
12480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481
12482#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 str = get_tv_string(&argvars[0]);
12484 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12485 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 vimconv.vc_type = CONV_NONE;
12487 convert_setup(&vimconv, from, to);
12488
12489 /* If the encodings are equal, no conversion needed. */
12490 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494
12495 convert_setup(&vimconv, NULL, NULL);
12496 vim_free(from);
12497 vim_free(to);
12498#endif
12499}
12500
12501/*
12502 * "indent()" function
12503 */
12504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012505f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012506 typval_T *argvars;
12507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508{
12509 linenr_T lnum;
12510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516}
12517
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012518/*
12519 * "index()" function
12520 */
12521 static void
12522f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012523 typval_T *argvars;
12524 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012525{
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 list_T *l;
12527 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012528 long idx = 0;
12529 int ic = FALSE;
12530
12531 rettv->vval.v_number = -1;
12532 if (argvars[0].v_type != VAR_LIST)
12533 {
12534 EMSG(_(e_listreq));
12535 return;
12536 }
12537 l = argvars[0].vval.v_list;
12538 if (l != NULL)
12539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012540 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012541 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012543 int error = FALSE;
12544
Bram Moolenaar758711c2005-02-02 23:11:38 +000012545 /* Start at specified item. Use the cached index that list_find()
12546 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012548 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012549 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 ic = get_tv_number_chk(&argvars[3], &error);
12551 if (error)
12552 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012554
Bram Moolenaar758711c2005-02-02 23:11:38 +000012555 for ( ; item != NULL; item = item->li_next, ++idx)
12556 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012557 {
12558 rettv->vval.v_number = idx;
12559 break;
12560 }
12561 }
12562}
12563
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564static int inputsecret_flag = 0;
12565
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012566static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12567
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012569 * This function is used by f_input() and f_inputdialog() functions. The third
12570 * argument to f_input() specifies the type of completion to use at the
12571 * prompt. The third argument to f_inputdialog() specifies the value to return
12572 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 */
12574 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012575get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012576 typval_T *argvars;
12577 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012578 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 char_u *p = NULL;
12582 int c;
12583 char_u buf[NUMBUFLEN];
12584 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012585 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012586 int xp_type = EXPAND_NOTHING;
12587 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591
12592#ifdef NO_CONSOLE_INPUT
12593 /* While starting up, there is no place to enter text. */
12594 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#endif
12597
12598 cmd_silent = FALSE; /* Want to see the prompt. */
12599 if (prompt != NULL)
12600 {
12601 /* Only the part of the message after the last NL is considered as
12602 * prompt for the command line */
12603 p = vim_strrchr(prompt, '\n');
12604 if (p == NULL)
12605 p = prompt;
12606 else
12607 {
12608 ++p;
12609 c = *p;
12610 *p = NUL;
12611 msg_start();
12612 msg_clr_eos();
12613 msg_puts_attr(prompt, echo_attr);
12614 msg_didout = FALSE;
12615 msg_starthere();
12616 *p = c;
12617 }
12618 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012620 if (argvars[1].v_type != VAR_UNKNOWN)
12621 {
12622 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12623 if (defstr != NULL)
12624 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012626 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012627 {
12628 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012629 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012630 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012631
Bram Moolenaar4463f292005-09-25 22:20:24 +000012632 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012633
Bram Moolenaar4463f292005-09-25 22:20:24 +000012634 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12635 if (xp_name == NULL)
12636 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012637
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012638 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012639
Bram Moolenaar4463f292005-09-25 22:20:24 +000012640 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12641 &xp_arg) == FAIL)
12642 return;
12643 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012644 }
12645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 if (defstr != NULL)
12647 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012648 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12649 xp_type, xp_arg);
12650
12651 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 /* since the user typed this, no need to wait for return */
12654 need_wait_return = FALSE;
12655 msg_didout = FALSE;
12656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657 cmd_silent = cmd_silent_save;
12658}
12659
12660/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012661 * "input()" function
12662 * Also handles inputsecret() when inputsecret is set.
12663 */
12664 static void
12665f_input(argvars, rettv)
12666 typval_T *argvars;
12667 typval_T *rettv;
12668{
12669 get_user_input(argvars, rettv, FALSE);
12670}
12671
12672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 * "inputdialog()" function
12674 */
12675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012677 typval_T *argvars;
12678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679{
12680#if defined(FEAT_GUI_TEXTDIALOG)
12681 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12682 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12683 {
12684 char_u *message;
12685 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 message = get_tv_string_chk(&argvars[0]);
12689 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012690 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012691 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 else
12693 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694 if (message != NULL && defstr != NULL
12695 && do_dialog(VIM_QUESTION, NULL, message,
12696 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 else
12699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 if (message != NULL && defstr != NULL
12701 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_string = vim_strsave(
12704 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 }
12710 else
12711#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012712 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713}
12714
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012715/*
12716 * "inputlist()" function
12717 */
12718 static void
12719f_inputlist(argvars, rettv)
12720 typval_T *argvars;
12721 typval_T *rettv;
12722{
12723 listitem_T *li;
12724 int selected;
12725 int mouse_used;
12726
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012727#ifdef NO_CONSOLE_INPUT
12728 /* While starting up, there is no place to enter text. */
12729 if (no_console_input())
12730 return;
12731#endif
12732 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12733 {
12734 EMSG2(_(e_listarg), "inputlist()");
12735 return;
12736 }
12737
12738 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012739 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012740 lines_left = Rows; /* avoid more prompt */
12741 msg_scroll = TRUE;
12742 msg_clr_eos();
12743
12744 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12745 {
12746 msg_puts(get_tv_string(&li->li_tv));
12747 msg_putchar('\n');
12748 }
12749
12750 /* Ask for choice. */
12751 selected = prompt_for_number(&mouse_used);
12752 if (mouse_used)
12753 selected -= lines_left;
12754
12755 rettv->vval.v_number = selected;
12756}
12757
12758
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12760
12761/*
12762 * "inputrestore()" function
12763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768{
12769 if (ga_userinput.ga_len > 0)
12770 {
12771 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12773 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012774 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 }
12776 else if (p_verbose > 1)
12777 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012778 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 }
12781}
12782
12783/*
12784 * "inputsave()" function
12785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012788 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012791 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 if (ga_grow(&ga_userinput, 1) == OK)
12793 {
12794 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12795 + ga_userinput.ga_len);
12796 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012797 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 }
12799 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801}
12802
12803/*
12804 * "inputsecret()" function
12805 */
12806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 typval_T *argvars;
12809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
12811 ++cmdline_star;
12812 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 --cmdline_star;
12815 --inputsecret_flag;
12816}
12817
12818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012819 * "insert()" function
12820 */
12821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *argvars;
12824 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825{
12826 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 listitem_T *item;
12828 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012829 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012830
12831 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012832 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012833 else if ((l = argvars[0].vval.v_list) != NULL
12834 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012835 {
12836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 before = get_tv_number_chk(&argvars[2], &error);
12838 if (error)
12839 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012840
Bram Moolenaar758711c2005-02-02 23:11:38 +000012841 if (before == l->lv_len)
12842 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012843 else
12844 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012845 item = list_find(l, before);
12846 if (item == NULL)
12847 {
12848 EMSGN(_(e_listidx), before);
12849 l = NULL;
12850 }
12851 }
12852 if (l != NULL)
12853 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012854 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012855 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012856 }
12857 }
12858}
12859
12860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 * "isdirectory()" function
12862 */
12863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 typval_T *argvars;
12866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869}
12870
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012871/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012872 * "islocked()" function
12873 */
12874 static void
12875f_islocked(argvars, rettv)
12876 typval_T *argvars;
12877 typval_T *rettv;
12878{
12879 lval_T lv;
12880 char_u *end;
12881 dictitem_T *di;
12882
12883 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012884 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12885 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012886 if (end != NULL && lv.ll_name != NULL)
12887 {
12888 if (*end != NUL)
12889 EMSG(_(e_trailing));
12890 else
12891 {
12892 if (lv.ll_tv == NULL)
12893 {
12894 if (check_changedtick(lv.ll_name))
12895 rettv->vval.v_number = 1; /* always locked */
12896 else
12897 {
12898 di = find_var(lv.ll_name, NULL);
12899 if (di != NULL)
12900 {
12901 /* Consider a variable locked when:
12902 * 1. the variable itself is locked
12903 * 2. the value of the variable is locked.
12904 * 3. the List or Dict value is locked.
12905 */
12906 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12907 || tv_islocked(&di->di_tv));
12908 }
12909 }
12910 }
12911 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012912 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012913 else if (lv.ll_newkey != NULL)
12914 EMSG2(_(e_dictkey), lv.ll_newkey);
12915 else if (lv.ll_list != NULL)
12916 /* List item. */
12917 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12918 else
12919 /* Dictionary item. */
12920 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12921 }
12922 }
12923
12924 clear_lval(&lv);
12925}
12926
Bram Moolenaar33570922005-01-25 22:26:29 +000012927static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012928
12929/*
12930 * Turn a dict into a list:
12931 * "what" == 0: list of keys
12932 * "what" == 1: list of values
12933 * "what" == 2: list of items
12934 */
12935 static void
12936dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012937 typval_T *argvars;
12938 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012939 int what;
12940{
Bram Moolenaar33570922005-01-25 22:26:29 +000012941 list_T *l2;
12942 dictitem_T *di;
12943 hashitem_T *hi;
12944 listitem_T *li;
12945 listitem_T *li2;
12946 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012947 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012948
Bram Moolenaar8c711452005-01-14 21:53:12 +000012949 if (argvars[0].v_type != VAR_DICT)
12950 {
12951 EMSG(_(e_dictreq));
12952 return;
12953 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012954 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012955 return;
12956
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012957 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012958 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012959
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012960 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012962 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012963 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012964 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012965 --todo;
12966 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012967
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012968 li = listitem_alloc();
12969 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012970 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012971 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012972
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012973 if (what == 0)
12974 {
12975 /* keys() */
12976 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012977 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012978 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12979 }
12980 else if (what == 1)
12981 {
12982 /* values() */
12983 copy_tv(&di->di_tv, &li->li_tv);
12984 }
12985 else
12986 {
12987 /* items() */
12988 l2 = list_alloc();
12989 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012990 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012991 li->li_tv.vval.v_list = l2;
12992 if (l2 == NULL)
12993 break;
12994 ++l2->lv_refcount;
12995
12996 li2 = listitem_alloc();
12997 if (li2 == NULL)
12998 break;
12999 list_append(l2, li2);
13000 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013001 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013002 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13003
13004 li2 = listitem_alloc();
13005 if (li2 == NULL)
13006 break;
13007 list_append(l2, li2);
13008 copy_tv(&di->di_tv, &li2->li_tv);
13009 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013010 }
13011 }
13012}
13013
13014/*
13015 * "items(dict)" function
13016 */
13017 static void
13018f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021{
13022 dict_list(argvars, rettv, 2);
13023}
13024
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013026 * "join()" function
13027 */
13028 static void
13029f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013030 typval_T *argvars;
13031 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013032{
13033 garray_T ga;
13034 char_u *sep;
13035
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013036 if (argvars[0].v_type != VAR_LIST)
13037 {
13038 EMSG(_(e_listreq));
13039 return;
13040 }
13041 if (argvars[0].vval.v_list == NULL)
13042 return;
13043 if (argvars[1].v_type == VAR_UNKNOWN)
13044 sep = (char_u *)" ";
13045 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013047
13048 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049
13050 if (sep != NULL)
13051 {
13052 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013053 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 ga_append(&ga, NUL);
13055 rettv->vval.v_string = (char_u *)ga.ga_data;
13056 }
13057 else
13058 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013059}
13060
13061/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013062 * "keys()" function
13063 */
13064 static void
13065f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 typval_T *argvars;
13067 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013068{
13069 dict_list(argvars, rettv, 0);
13070}
13071
13072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 * "last_buffer_nr()" function.
13074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013077 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079{
13080 int n = 0;
13081 buf_T *buf;
13082
13083 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13084 if (n < buf->b_fnum)
13085 n = buf->b_fnum;
13086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013087 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013088}
13089
13090/*
13091 * "len()" function
13092 */
13093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013094f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013095 typval_T *argvars;
13096 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013097{
13098 switch (argvars[0].v_type)
13099 {
13100 case VAR_STRING:
13101 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102 rettv->vval.v_number = (varnumber_T)STRLEN(
13103 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013104 break;
13105 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013107 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013108 case VAR_DICT:
13109 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13110 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013111 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013112 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013113 break;
13114 }
13115}
13116
Bram Moolenaar33570922005-01-25 22:26:29 +000013117static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013118
13119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013120libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013121 typval_T *argvars;
13122 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013123 int type;
13124{
13125#ifdef FEAT_LIBCALL
13126 char_u *string_in;
13127 char_u **string_result;
13128 int nr_result;
13129#endif
13130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013132 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013134
13135 if (check_restricted() || check_secure())
13136 return;
13137
13138#ifdef FEAT_LIBCALL
13139 /* The first two args must be strings, otherwise its meaningless */
13140 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13141 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013142 string_in = NULL;
13143 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013144 string_in = argvars[2].vval.v_string;
13145 if (type == VAR_NUMBER)
13146 string_result = NULL;
13147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013149 if (mch_libcall(argvars[0].vval.v_string,
13150 argvars[1].vval.v_string,
13151 string_in,
13152 argvars[2].vval.v_number,
13153 string_result,
13154 &nr_result) == OK
13155 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157 }
13158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013162 * "libcall()" function
13163 */
13164 static void
13165f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
13167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013168{
13169 libcall_common(argvars, rettv, VAR_STRING);
13170}
13171
13172/*
13173 * "libcallnr()" function
13174 */
13175 static void
13176f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013179{
13180 libcall_common(argvars, rettv, VAR_NUMBER);
13181}
13182
13183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 * "line(string)" function
13185 */
13186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 typval_T *argvars;
13189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190{
13191 linenr_T lnum = 0;
13192 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013193 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013195 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 if (fp != NULL)
13197 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199}
13200
13201/*
13202 * "line2byte(lnum)" function
13203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013206 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208{
13209#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211#else
13212 linenr_T lnum;
13213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013218 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13219 if (rettv->vval.v_number >= 0)
13220 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221#endif
13222}
13223
13224/*
13225 * "lispindent(lnum)" function
13226 */
13227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231{
13232#ifdef FEAT_LISP
13233 pos_T pos;
13234 linenr_T lnum;
13235
13236 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13239 {
13240 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 curwin->w_cursor = pos;
13243 }
13244 else
13245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247}
13248
13249/*
13250 * "localtime()" function
13251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013254 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258}
13259
Bram Moolenaar33570922005-01-25 22:26:29 +000013260static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261
13262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *argvars;
13265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 int exact;
13267{
13268 char_u *keys;
13269 char_u *which;
13270 char_u buf[NUMBUFLEN];
13271 char_u *keys_buf = NULL;
13272 char_u *rhs;
13273 int mode;
13274 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013275 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276
13277 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 rettv->v_type = VAR_STRING;
13279 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 if (*keys == NUL)
13283 return;
13284
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013285 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013288 if (argvars[2].v_type != VAR_UNKNOWN)
13289 abbr = get_tv_number(&argvars[2]);
13290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 else
13292 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013293 if (which == NULL)
13294 return;
13295
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 mode = get_map_mode(&which, 0);
13297
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013298 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013299 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 vim_free(keys_buf);
13301 if (rhs != NULL)
13302 {
13303 ga_init(&ga);
13304 ga.ga_itemsize = 1;
13305 ga.ga_growsize = 40;
13306
13307 while (*rhs != NUL)
13308 ga_concat(&ga, str2special(&rhs, FALSE));
13309
13310 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 }
13313}
13314
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013315#ifdef FEAT_FLOAT
13316/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013317 * "log()" function
13318 */
13319 static void
13320f_log(argvars, rettv)
13321 typval_T *argvars;
13322 typval_T *rettv;
13323{
13324 float_T f;
13325
13326 rettv->v_type = VAR_FLOAT;
13327 if (get_float_arg(argvars, &f) == OK)
13328 rettv->vval.v_float = log(f);
13329 else
13330 rettv->vval.v_float = 0.0;
13331}
13332
13333/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013334 * "log10()" function
13335 */
13336 static void
13337f_log10(argvars, rettv)
13338 typval_T *argvars;
13339 typval_T *rettv;
13340{
13341 float_T f;
13342
13343 rettv->v_type = VAR_FLOAT;
13344 if (get_float_arg(argvars, &f) == OK)
13345 rettv->vval.v_float = log10(f);
13346 else
13347 rettv->vval.v_float = 0.0;
13348}
13349#endif
13350
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013352 * "map()" function
13353 */
13354 static void
13355f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013356 typval_T *argvars;
13357 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013358{
13359 filter_map(argvars, rettv, TRUE);
13360}
13361
13362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013363 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 */
13365 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013366f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013370 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371}
13372
13373/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013374 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 */
13376 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 typval_T *argvars;
13379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013381 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382}
13383
Bram Moolenaar33570922005-01-25 22:26:29 +000013384static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385
13386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 typval_T *argvars;
13389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 int type;
13391{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013392 char_u *str = NULL;
13393 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 char_u *pat;
13395 regmatch_T regmatch;
13396 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013397 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 char_u *save_cpo;
13399 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013400 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013401 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013402 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 list_T *l = NULL;
13404 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013405 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013406 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407
13408 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13409 save_cpo = p_cpo;
13410 p_cpo = (char_u *)"";
13411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013412 rettv->vval.v_number = -1;
13413 if (type == 3)
13414 {
13415 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013416 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013417 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013418 }
13419 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 rettv->v_type = VAR_STRING;
13422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013425 if (argvars[0].v_type == VAR_LIST)
13426 {
13427 if ((l = argvars[0].vval.v_list) == NULL)
13428 goto theend;
13429 li = l->lv_first;
13430 }
13431 else
13432 expr = str = get_tv_string(&argvars[0]);
13433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13435 if (pat == NULL)
13436 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 int error = FALSE;
13441
13442 start = get_tv_number_chk(&argvars[2], &error);
13443 if (error)
13444 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013445 if (l != NULL)
13446 {
13447 li = list_find(l, start);
13448 if (li == NULL)
13449 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013450 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013451 }
13452 else
13453 {
13454 if (start < 0)
13455 start = 0;
13456 if (start > (long)STRLEN(str))
13457 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013458 /* When "count" argument is there ignore matches before "start",
13459 * otherwise skip part of the string. Differs when pattern is "^"
13460 * or "\<". */
13461 if (argvars[3].v_type != VAR_UNKNOWN)
13462 startcol = start;
13463 else
13464 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013465 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013466
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013467 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 nth = get_tv_number_chk(&argvars[3], &error);
13469 if (error)
13470 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 }
13472
13473 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13474 if (regmatch.regprog != NULL)
13475 {
13476 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013477
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013478 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013479 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013480 if (l != NULL)
13481 {
13482 if (li == NULL)
13483 {
13484 match = FALSE;
13485 break;
13486 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013487 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013488 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013489 if (str == NULL)
13490 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013491 }
13492
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013493 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013494
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013495 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013496 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013497 if (l == NULL && !match)
13498 break;
13499
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013500 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013501 if (l != NULL)
13502 {
13503 li = li->li_next;
13504 ++idx;
13505 }
13506 else
13507 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013508#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013509 startcol = (colnr_T)(regmatch.startp[0]
13510 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013511#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013512 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013513#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013515 }
13516
13517 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013519 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013520 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013521 int i;
13522
13523 /* return list with matched string and submatches */
13524 for (i = 0; i < NSUBEXP; ++i)
13525 {
13526 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013527 {
13528 if (list_append_string(rettv->vval.v_list,
13529 (char_u *)"", 0) == FAIL)
13530 break;
13531 }
13532 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013533 regmatch.startp[i],
13534 (int)(regmatch.endp[i] - regmatch.startp[i]))
13535 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013536 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013537 }
13538 }
13539 else if (type == 2)
13540 {
13541 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013542 if (l != NULL)
13543 copy_tv(&li->li_tv, rettv);
13544 else
13545 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013547 }
13548 else if (l != NULL)
13549 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 else
13551 {
13552 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 (varnumber_T)(regmatch.startp[0] - str);
13555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013558 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 }
13560 }
13561 vim_free(regmatch.regprog);
13562 }
13563
13564theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 p_cpo = save_cpo;
13567}
13568
13569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013570 * "match()" function
13571 */
13572 static void
13573f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013574 typval_T *argvars;
13575 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013576{
13577 find_some_match(argvars, rettv, 1);
13578}
13579
13580/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013581 * "matchadd()" function
13582 */
13583 static void
13584f_matchadd(argvars, rettv)
13585 typval_T *argvars;
13586 typval_T *rettv;
13587{
13588#ifdef FEAT_SEARCH_EXTRA
13589 char_u buf[NUMBUFLEN];
13590 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13591 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13592 int prio = 10; /* default priority */
13593 int id = -1;
13594 int error = FALSE;
13595
13596 rettv->vval.v_number = -1;
13597
13598 if (grp == NULL || pat == NULL)
13599 return;
13600 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013601 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013602 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013603 if (argvars[3].v_type != VAR_UNKNOWN)
13604 id = get_tv_number_chk(&argvars[3], &error);
13605 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013606 if (error == TRUE)
13607 return;
13608 if (id >= 1 && id <= 3)
13609 {
13610 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13611 return;
13612 }
13613
13614 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13615#endif
13616}
13617
13618/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013619 * "matcharg()" function
13620 */
13621 static void
13622f_matcharg(argvars, rettv)
13623 typval_T *argvars;
13624 typval_T *rettv;
13625{
13626 if (rettv_list_alloc(rettv) == OK)
13627 {
13628#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013629 int id = get_tv_number(&argvars[0]);
13630 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013631
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013632 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013633 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013634 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13635 {
13636 list_append_string(rettv->vval.v_list,
13637 syn_id2name(m->hlg_id), -1);
13638 list_append_string(rettv->vval.v_list, m->pattern, -1);
13639 }
13640 else
13641 {
13642 list_append_string(rettv->vval.v_list, NUL, -1);
13643 list_append_string(rettv->vval.v_list, NUL, -1);
13644 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013645 }
13646#endif
13647 }
13648}
13649
13650/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013651 * "matchdelete()" function
13652 */
13653 static void
13654f_matchdelete(argvars, rettv)
13655 typval_T *argvars;
13656 typval_T *rettv;
13657{
13658#ifdef FEAT_SEARCH_EXTRA
13659 rettv->vval.v_number = match_delete(curwin,
13660 (int)get_tv_number(&argvars[0]), TRUE);
13661#endif
13662}
13663
13664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013665 * "matchend()" function
13666 */
13667 static void
13668f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671{
13672 find_some_match(argvars, rettv, 0);
13673}
13674
13675/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013676 * "matchlist()" function
13677 */
13678 static void
13679f_matchlist(argvars, rettv)
13680 typval_T *argvars;
13681 typval_T *rettv;
13682{
13683 find_some_match(argvars, rettv, 3);
13684}
13685
13686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013687 * "matchstr()" function
13688 */
13689 static void
13690f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 typval_T *argvars;
13692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013693{
13694 find_some_match(argvars, rettv, 2);
13695}
13696
Bram Moolenaar33570922005-01-25 22:26:29 +000013697static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013698
13699 static void
13700max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013701 typval_T *argvars;
13702 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013703 int domax;
13704{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013705 long n = 0;
13706 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013708
13709 if (argvars[0].v_type == VAR_LIST)
13710 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 list_T *l;
13712 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013713
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013714 l = argvars[0].vval.v_list;
13715 if (l != NULL)
13716 {
13717 li = l->lv_first;
13718 if (li != NULL)
13719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013720 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013721 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013722 {
13723 li = li->li_next;
13724 if (li == NULL)
13725 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013726 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013727 if (domax ? i > n : i < n)
13728 n = i;
13729 }
13730 }
13731 }
13732 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013733 else if (argvars[0].v_type == VAR_DICT)
13734 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013735 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013736 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013737 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013738 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013739
13740 d = argvars[0].vval.v_dict;
13741 if (d != NULL)
13742 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013743 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013746 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013747 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013748 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013750 if (first)
13751 {
13752 n = i;
13753 first = FALSE;
13754 }
13755 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013756 n = i;
13757 }
13758 }
13759 }
13760 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013761 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013762 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013764}
13765
13766/*
13767 * "max()" function
13768 */
13769 static void
13770f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 typval_T *argvars;
13772 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013773{
13774 max_min(argvars, rettv, TRUE);
13775}
13776
13777/*
13778 * "min()" function
13779 */
13780 static void
13781f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013782 typval_T *argvars;
13783 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013784{
13785 max_min(argvars, rettv, FALSE);
13786}
13787
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013788static int mkdir_recurse __ARGS((char_u *dir, int prot));
13789
13790/*
13791 * Create the directory in which "dir" is located, and higher levels when
13792 * needed.
13793 */
13794 static int
13795mkdir_recurse(dir, prot)
13796 char_u *dir;
13797 int prot;
13798{
13799 char_u *p;
13800 char_u *updir;
13801 int r = FAIL;
13802
13803 /* Get end of directory name in "dir".
13804 * We're done when it's "/" or "c:/". */
13805 p = gettail_sep(dir);
13806 if (p <= get_past_head(dir))
13807 return OK;
13808
13809 /* If the directory exists we're done. Otherwise: create it.*/
13810 updir = vim_strnsave(dir, (int)(p - dir));
13811 if (updir == NULL)
13812 return FAIL;
13813 if (mch_isdir(updir))
13814 r = OK;
13815 else if (mkdir_recurse(updir, prot) == OK)
13816 r = vim_mkdir_emsg(updir, prot);
13817 vim_free(updir);
13818 return r;
13819}
13820
13821#ifdef vim_mkdir
13822/*
13823 * "mkdir()" function
13824 */
13825 static void
13826f_mkdir(argvars, rettv)
13827 typval_T *argvars;
13828 typval_T *rettv;
13829{
13830 char_u *dir;
13831 char_u buf[NUMBUFLEN];
13832 int prot = 0755;
13833
13834 rettv->vval.v_number = FAIL;
13835 if (check_restricted() || check_secure())
13836 return;
13837
13838 dir = get_tv_string_buf(&argvars[0], buf);
13839 if (argvars[1].v_type != VAR_UNKNOWN)
13840 {
13841 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 prot = get_tv_number_chk(&argvars[2], NULL);
13843 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013844 mkdir_recurse(dir, prot);
13845 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013846 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013847}
13848#endif
13849
Bram Moolenaar0d660222005-01-07 21:51:51 +000013850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 * "mode()" function
13852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013855 typval_T *argvars;
13856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013858 char_u buf[3];
13859
13860 buf[1] = NUL;
13861 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862
13863#ifdef FEAT_VISUAL
13864 if (VIsual_active)
13865 {
13866 if (VIsual_select)
13867 buf[0] = VIsual_mode + 's' - 'v';
13868 else
13869 buf[0] = VIsual_mode;
13870 }
13871 else
13872#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013873 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13874 || State == CONFIRM)
13875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013877 if (State == ASKMORE)
13878 buf[1] = 'm';
13879 else if (State == CONFIRM)
13880 buf[1] = '?';
13881 }
13882 else if (State == EXTERNCMD)
13883 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 else if (State & INSERT)
13885 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013886#ifdef FEAT_VREPLACE
13887 if (State & VREPLACE_FLAG)
13888 {
13889 buf[0] = 'R';
13890 buf[1] = 'v';
13891 }
13892 else
13893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 if (State & REPLACE_FLAG)
13895 buf[0] = 'R';
13896 else
13897 buf[0] = 'i';
13898 }
13899 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013902 if (exmode_active)
13903 buf[1] = 'v';
13904 }
13905 else if (exmode_active)
13906 {
13907 buf[0] = 'c';
13908 buf[1] = 'e';
13909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013913 if (finish_op)
13914 buf[1] = 'o';
13915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013917 /* Clear out the minor mode when the argument is not a non-zero number or
13918 * non-empty string. */
13919 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013920 buf[1] = NUL;
13921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 rettv->vval.v_string = vim_strsave(buf);
13923 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924}
13925
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013926#ifdef FEAT_MZSCHEME
13927/*
13928 * "mzeval()" function
13929 */
13930 static void
13931f_mzeval(argvars, rettv)
13932 typval_T *argvars;
13933 typval_T *rettv;
13934{
13935 char_u *str;
13936 char_u buf[NUMBUFLEN];
13937
13938 str = get_tv_string_buf(&argvars[0], buf);
13939 do_mzeval(str, rettv);
13940}
13941#endif
13942
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013944 * "nextnonblank()" function
13945 */
13946 static void
13947f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013948 typval_T *argvars;
13949 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950{
13951 linenr_T lnum;
13952
13953 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 {
13957 lnum = 0;
13958 break;
13959 }
13960 if (*skipwhite(ml_get(lnum)) != NUL)
13961 break;
13962 }
13963 rettv->vval.v_number = lnum;
13964}
13965
13966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 * "nr2char()" function
13968 */
13969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013971 typval_T *argvars;
13972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973{
13974 char_u buf[NUMBUFLEN];
13975
13976#ifdef FEAT_MBYTE
13977 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 else
13980#endif
13981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 buf[1] = NUL;
13984 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->v_type = VAR_STRING;
13986 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013987}
13988
13989/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013990 * "pathshorten()" function
13991 */
13992 static void
13993f_pathshorten(argvars, rettv)
13994 typval_T *argvars;
13995 typval_T *rettv;
13996{
13997 char_u *p;
13998
13999 rettv->v_type = VAR_STRING;
14000 p = get_tv_string_chk(&argvars[0]);
14001 if (p == NULL)
14002 rettv->vval.v_string = NULL;
14003 else
14004 {
14005 p = vim_strsave(p);
14006 rettv->vval.v_string = p;
14007 if (p != NULL)
14008 shorten_dir(p);
14009 }
14010}
14011
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014012#ifdef FEAT_FLOAT
14013/*
14014 * "pow()" function
14015 */
14016 static void
14017f_pow(argvars, rettv)
14018 typval_T *argvars;
14019 typval_T *rettv;
14020{
14021 float_T fx, fy;
14022
14023 rettv->v_type = VAR_FLOAT;
14024 if (get_float_arg(argvars, &fx) == OK
14025 && get_float_arg(&argvars[1], &fy) == OK)
14026 rettv->vval.v_float = pow(fx, fy);
14027 else
14028 rettv->vval.v_float = 0.0;
14029}
14030#endif
14031
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014032/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014033 * "prevnonblank()" function
14034 */
14035 static void
14036f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014037 typval_T *argvars;
14038 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014039{
14040 linenr_T lnum;
14041
14042 lnum = get_tv_lnum(argvars);
14043 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14044 lnum = 0;
14045 else
14046 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14047 --lnum;
14048 rettv->vval.v_number = lnum;
14049}
14050
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014051#ifdef HAVE_STDARG_H
14052/* This dummy va_list is here because:
14053 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14054 * - locally in the function results in a "used before set" warning
14055 * - using va_start() to initialize it gives "function with fixed args" error */
14056static va_list ap;
14057#endif
14058
Bram Moolenaar8c711452005-01-14 21:53:12 +000014059/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014060 * "printf()" function
14061 */
14062 static void
14063f_printf(argvars, rettv)
14064 typval_T *argvars;
14065 typval_T *rettv;
14066{
14067 rettv->v_type = VAR_STRING;
14068 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014069#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014070 {
14071 char_u buf[NUMBUFLEN];
14072 int len;
14073 char_u *s;
14074 int saved_did_emsg = did_emsg;
14075 char *fmt;
14076
14077 /* Get the required length, allocate the buffer and do it for real. */
14078 did_emsg = FALSE;
14079 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014080 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014081 if (!did_emsg)
14082 {
14083 s = alloc(len + 1);
14084 if (s != NULL)
14085 {
14086 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014087 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014088 }
14089 }
14090 did_emsg |= saved_did_emsg;
14091 }
14092#endif
14093}
14094
14095/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014096 * "pumvisible()" function
14097 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014098 static void
14099f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014100 typval_T *argvars UNUSED;
14101 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014102{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014103#ifdef FEAT_INS_EXPAND
14104 if (pum_visible())
14105 rettv->vval.v_number = 1;
14106#endif
14107}
14108
14109/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014110 * "range()" function
14111 */
14112 static void
14113f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014114 typval_T *argvars;
14115 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014116{
14117 long start;
14118 long end;
14119 long stride = 1;
14120 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014121 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014123 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014124 if (argvars[1].v_type == VAR_UNKNOWN)
14125 {
14126 end = start - 1;
14127 start = 0;
14128 }
14129 else
14130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014132 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014134 }
14135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 if (error)
14137 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014138 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014139 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014140 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014141 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014142 else
14143 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014144 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014145 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014146 if (list_append_number(rettv->vval.v_list,
14147 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014148 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014149 }
14150}
14151
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014152/*
14153 * "readfile()" function
14154 */
14155 static void
14156f_readfile(argvars, rettv)
14157 typval_T *argvars;
14158 typval_T *rettv;
14159{
14160 int binary = FALSE;
14161 char_u *fname;
14162 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014163 listitem_T *li;
14164#define FREAD_SIZE 200 /* optimized for text lines */
14165 char_u buf[FREAD_SIZE];
14166 int readlen; /* size of last fread() */
14167 int buflen; /* nr of valid chars in buf[] */
14168 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14169 int tolist; /* first byte in buf[] still to be put in list */
14170 int chop; /* how many CR to chop off */
14171 char_u *prev = NULL; /* previously read bytes, if any */
14172 int prevlen = 0; /* length of "prev" if not NULL */
14173 char_u *s;
14174 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014175 long maxline = MAXLNUM;
14176 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014178 if (argvars[1].v_type != VAR_UNKNOWN)
14179 {
14180 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14181 binary = TRUE;
14182 if (argvars[2].v_type != VAR_UNKNOWN)
14183 maxline = get_tv_number(&argvars[2]);
14184 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014185
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014186 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014187 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014188
14189 /* Always open the file in binary mode, library functions have a mind of
14190 * their own about CR-LF conversion. */
14191 fname = get_tv_string(&argvars[0]);
14192 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14193 {
14194 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14195 return;
14196 }
14197
14198 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014199 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014200 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014201 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014202 buflen = filtd + readlen;
14203 tolist = 0;
14204 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14205 {
14206 if (buf[filtd] == '\n' || readlen <= 0)
14207 {
14208 /* Only when in binary mode add an empty list item when the
14209 * last line ends in a '\n'. */
14210 if (!binary && readlen == 0 && filtd == 0)
14211 break;
14212
14213 /* Found end-of-line or end-of-file: add a text line to the
14214 * list. */
14215 chop = 0;
14216 if (!binary)
14217 while (filtd - chop - 1 >= tolist
14218 && buf[filtd - chop - 1] == '\r')
14219 ++chop;
14220 len = filtd - tolist - chop;
14221 if (prev == NULL)
14222 s = vim_strnsave(buf + tolist, len);
14223 else
14224 {
14225 s = alloc((unsigned)(prevlen + len + 1));
14226 if (s != NULL)
14227 {
14228 mch_memmove(s, prev, prevlen);
14229 vim_free(prev);
14230 prev = NULL;
14231 mch_memmove(s + prevlen, buf + tolist, len);
14232 s[prevlen + len] = NUL;
14233 }
14234 }
14235 tolist = filtd + 1;
14236
14237 li = listitem_alloc();
14238 if (li == NULL)
14239 {
14240 vim_free(s);
14241 break;
14242 }
14243 li->li_tv.v_type = VAR_STRING;
14244 li->li_tv.v_lock = 0;
14245 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014246 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014247
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014248 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014249 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014250 if (readlen <= 0)
14251 break;
14252 }
14253 else if (buf[filtd] == NUL)
14254 buf[filtd] = '\n';
14255 }
14256 if (readlen <= 0)
14257 break;
14258
14259 if (tolist == 0)
14260 {
14261 /* "buf" is full, need to move text to an allocated buffer */
14262 if (prev == NULL)
14263 {
14264 prev = vim_strnsave(buf, buflen);
14265 prevlen = buflen;
14266 }
14267 else
14268 {
14269 s = alloc((unsigned)(prevlen + buflen));
14270 if (s != NULL)
14271 {
14272 mch_memmove(s, prev, prevlen);
14273 mch_memmove(s + prevlen, buf, buflen);
14274 vim_free(prev);
14275 prev = s;
14276 prevlen += buflen;
14277 }
14278 }
14279 filtd = 0;
14280 }
14281 else
14282 {
14283 mch_memmove(buf, buf + tolist, buflen - tolist);
14284 filtd -= tolist;
14285 }
14286 }
14287
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014288 /*
14289 * For a negative line count use only the lines at the end of the file,
14290 * free the rest.
14291 */
14292 if (maxline < 0)
14293 while (cnt > -maxline)
14294 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014295 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014296 --cnt;
14297 }
14298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014299 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014300 fclose(fd);
14301}
14302
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014303#if defined(FEAT_RELTIME)
14304static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14305
14306/*
14307 * Convert a List to proftime_T.
14308 * Return FAIL when there is something wrong.
14309 */
14310 static int
14311list2proftime(arg, tm)
14312 typval_T *arg;
14313 proftime_T *tm;
14314{
14315 long n1, n2;
14316 int error = FALSE;
14317
14318 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14319 || arg->vval.v_list->lv_len != 2)
14320 return FAIL;
14321 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14322 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14323# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014324 tm->HighPart = n1;
14325 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014326# else
14327 tm->tv_sec = n1;
14328 tm->tv_usec = n2;
14329# endif
14330 return error ? FAIL : OK;
14331}
14332#endif /* FEAT_RELTIME */
14333
14334/*
14335 * "reltime()" function
14336 */
14337 static void
14338f_reltime(argvars, rettv)
14339 typval_T *argvars;
14340 typval_T *rettv;
14341{
14342#ifdef FEAT_RELTIME
14343 proftime_T res;
14344 proftime_T start;
14345
14346 if (argvars[0].v_type == VAR_UNKNOWN)
14347 {
14348 /* No arguments: get current time. */
14349 profile_start(&res);
14350 }
14351 else if (argvars[1].v_type == VAR_UNKNOWN)
14352 {
14353 if (list2proftime(&argvars[0], &res) == FAIL)
14354 return;
14355 profile_end(&res);
14356 }
14357 else
14358 {
14359 /* Two arguments: compute the difference. */
14360 if (list2proftime(&argvars[0], &start) == FAIL
14361 || list2proftime(&argvars[1], &res) == FAIL)
14362 return;
14363 profile_sub(&res, &start);
14364 }
14365
14366 if (rettv_list_alloc(rettv) == OK)
14367 {
14368 long n1, n2;
14369
14370# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014371 n1 = res.HighPart;
14372 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014373# else
14374 n1 = res.tv_sec;
14375 n2 = res.tv_usec;
14376# endif
14377 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14378 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14379 }
14380#endif
14381}
14382
14383/*
14384 * "reltimestr()" function
14385 */
14386 static void
14387f_reltimestr(argvars, rettv)
14388 typval_T *argvars;
14389 typval_T *rettv;
14390{
14391#ifdef FEAT_RELTIME
14392 proftime_T tm;
14393#endif
14394
14395 rettv->v_type = VAR_STRING;
14396 rettv->vval.v_string = NULL;
14397#ifdef FEAT_RELTIME
14398 if (list2proftime(&argvars[0], &tm) == OK)
14399 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14400#endif
14401}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014402
Bram Moolenaar0d660222005-01-07 21:51:51 +000014403#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14404static void make_connection __ARGS((void));
14405static int check_connection __ARGS((void));
14406
14407 static void
14408make_connection()
14409{
14410 if (X_DISPLAY == NULL
14411# ifdef FEAT_GUI
14412 && !gui.in_use
14413# endif
14414 )
14415 {
14416 x_force_connect = TRUE;
14417 setup_term_clip();
14418 x_force_connect = FALSE;
14419 }
14420}
14421
14422 static int
14423check_connection()
14424{
14425 make_connection();
14426 if (X_DISPLAY == NULL)
14427 {
14428 EMSG(_("E240: No connection to Vim server"));
14429 return FAIL;
14430 }
14431 return OK;
14432}
14433#endif
14434
14435#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014436static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437
14438 static void
14439remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 typval_T *argvars;
14441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014442 int expr;
14443{
14444 char_u *server_name;
14445 char_u *keys;
14446 char_u *r = NULL;
14447 char_u buf[NUMBUFLEN];
14448# ifdef WIN32
14449 HWND w;
14450# else
14451 Window w;
14452# endif
14453
14454 if (check_restricted() || check_secure())
14455 return;
14456
14457# ifdef FEAT_X11
14458 if (check_connection() == FAIL)
14459 return;
14460# endif
14461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014462 server_name = get_tv_string_chk(&argvars[0]);
14463 if (server_name == NULL)
14464 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 keys = get_tv_string_buf(&argvars[1], buf);
14466# ifdef WIN32
14467 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14468# else
14469 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14470 < 0)
14471# endif
14472 {
14473 if (r != NULL)
14474 EMSG(r); /* sending worked but evaluation failed */
14475 else
14476 EMSG2(_("E241: Unable to send to %s"), server_name);
14477 return;
14478 }
14479
14480 rettv->vval.v_string = r;
14481
14482 if (argvars[2].v_type != VAR_UNKNOWN)
14483 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014485 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014486 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014487
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014488 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014489 v.di_tv.v_type = VAR_STRING;
14490 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 idvar = get_tv_string_chk(&argvars[2]);
14492 if (idvar != NULL)
14493 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 }
14496}
14497#endif
14498
14499/*
14500 * "remote_expr()" function
14501 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502 static void
14503f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014506{
14507 rettv->v_type = VAR_STRING;
14508 rettv->vval.v_string = NULL;
14509#ifdef FEAT_CLIENTSERVER
14510 remote_common(argvars, rettv, TRUE);
14511#endif
14512}
14513
14514/*
14515 * "remote_foreground()" function
14516 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014517 static void
14518f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014519 typval_T *argvars UNUSED;
14520 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014522#ifdef FEAT_CLIENTSERVER
14523# ifdef WIN32
14524 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014525 {
14526 char_u *server_name = get_tv_string_chk(&argvars[0]);
14527
14528 if (server_name != NULL)
14529 serverForeground(server_name);
14530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014531# else
14532 /* Send a foreground() expression to the server. */
14533 argvars[1].v_type = VAR_STRING;
14534 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14535 argvars[2].v_type = VAR_UNKNOWN;
14536 remote_common(argvars, rettv, TRUE);
14537 vim_free(argvars[1].vval.v_string);
14538# endif
14539#endif
14540}
14541
Bram Moolenaar0d660222005-01-07 21:51:51 +000014542 static void
14543f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014544 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546{
14547#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549 char_u *s = NULL;
14550# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014551 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014552# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554
14555 if (check_restricted() || check_secure())
14556 {
14557 rettv->vval.v_number = -1;
14558 return;
14559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 serverid = get_tv_string_chk(&argvars[0]);
14561 if (serverid == NULL)
14562 {
14563 rettv->vval.v_number = -1;
14564 return; /* type error; errmsg already given */
14565 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014567 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 if (n == 0)
14569 rettv->vval.v_number = -1;
14570 else
14571 {
14572 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14573 rettv->vval.v_number = (s != NULL);
14574 }
14575# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 if (check_connection() == FAIL)
14577 return;
14578
14579 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581# endif
14582
14583 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014585 char_u *retvar;
14586
Bram Moolenaar33570922005-01-25 22:26:29 +000014587 v.di_tv.v_type = VAR_STRING;
14588 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 retvar = get_tv_string_chk(&argvars[1]);
14590 if (retvar != NULL)
14591 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 }
14594#else
14595 rettv->vval.v_number = -1;
14596#endif
14597}
14598
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 static void
14600f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014601 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603{
14604 char_u *r = NULL;
14605
14606#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607 char_u *serverid = get_tv_string_chk(&argvars[0]);
14608
14609 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610 {
14611# ifdef WIN32
14612 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014613 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014615 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616 if (n != 0)
14617 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14618 if (r == NULL)
14619# else
14620 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622# endif
14623 EMSG(_("E277: Unable to read a server reply"));
14624 }
14625#endif
14626 rettv->v_type = VAR_STRING;
14627 rettv->vval.v_string = r;
14628}
14629
14630/*
14631 * "remote_send()" function
14632 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 static void
14634f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637{
14638 rettv->v_type = VAR_STRING;
14639 rettv->vval.v_string = NULL;
14640#ifdef FEAT_CLIENTSERVER
14641 remote_common(argvars, rettv, FALSE);
14642#endif
14643}
14644
14645/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014646 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014647 */
14648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 typval_T *argvars;
14651 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014652{
Bram Moolenaar33570922005-01-25 22:26:29 +000014653 list_T *l;
14654 listitem_T *item, *item2;
14655 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014656 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014657 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014658 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014659 dict_T *d;
14660 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014661
Bram Moolenaar8c711452005-01-14 21:53:12 +000014662 if (argvars[0].v_type == VAR_DICT)
14663 {
14664 if (argvars[2].v_type != VAR_UNKNOWN)
14665 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014666 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014667 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 key = get_tv_string_chk(&argvars[1]);
14670 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014672 di = dict_find(d, key, -1);
14673 if (di == NULL)
14674 EMSG2(_(e_dictkey), key);
14675 else
14676 {
14677 *rettv = di->di_tv;
14678 init_tv(&di->di_tv);
14679 dictitem_remove(d, di);
14680 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014682 }
14683 }
14684 else if (argvars[0].v_type != VAR_LIST)
14685 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014686 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014687 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014689 int error = FALSE;
14690
14691 idx = get_tv_number_chk(&argvars[1], &error);
14692 if (error)
14693 ; /* type error: do nothing, errmsg already given */
14694 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014695 EMSGN(_(e_listidx), idx);
14696 else
14697 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014698 if (argvars[2].v_type == VAR_UNKNOWN)
14699 {
14700 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014701 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014702 *rettv = item->li_tv;
14703 vim_free(item);
14704 }
14705 else
14706 {
14707 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014708 end = get_tv_number_chk(&argvars[2], &error);
14709 if (error)
14710 ; /* type error: do nothing */
14711 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014712 EMSGN(_(e_listidx), end);
14713 else
14714 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014715 int cnt = 0;
14716
14717 for (li = item; li != NULL; li = li->li_next)
14718 {
14719 ++cnt;
14720 if (li == item2)
14721 break;
14722 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014723 if (li == NULL) /* didn't find "item2" after "item" */
14724 EMSG(_(e_invrange));
14725 else
14726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014727 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014728 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014729 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014730 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014731 l->lv_first = item;
14732 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014733 item->li_prev = NULL;
14734 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014735 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014736 }
14737 }
14738 }
14739 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014740 }
14741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742}
14743
14744/*
14745 * "rename({from}, {to})" function
14746 */
14747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014749 typval_T *argvars;
14750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751{
14752 char_u buf[NUMBUFLEN];
14753
14754 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14758 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759}
14760
14761/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014762 * "repeat()" function
14763 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014764 static void
14765f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014766 typval_T *argvars;
14767 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014768{
14769 char_u *p;
14770 int n;
14771 int slen;
14772 int len;
14773 char_u *r;
14774 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014775
14776 n = get_tv_number(&argvars[1]);
14777 if (argvars[0].v_type == VAR_LIST)
14778 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014779 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014780 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014781 if (list_extend(rettv->vval.v_list,
14782 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014783 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014784 }
14785 else
14786 {
14787 p = get_tv_string(&argvars[0]);
14788 rettv->v_type = VAR_STRING;
14789 rettv->vval.v_string = NULL;
14790
14791 slen = (int)STRLEN(p);
14792 len = slen * n;
14793 if (len <= 0)
14794 return;
14795
14796 r = alloc(len + 1);
14797 if (r != NULL)
14798 {
14799 for (i = 0; i < n; i++)
14800 mch_memmove(r + i * slen, p, (size_t)slen);
14801 r[len] = NUL;
14802 }
14803
14804 rettv->vval.v_string = r;
14805 }
14806}
14807
14808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 * "resolve()" function
14810 */
14811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014812f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014813 typval_T *argvars;
14814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815{
14816 char_u *p;
14817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014818 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819#ifdef FEAT_SHORTCUT
14820 {
14821 char_u *v = NULL;
14822
14823 v = mch_resolve_shortcut(p);
14824 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 }
14829#else
14830# ifdef HAVE_READLINK
14831 {
14832 char_u buf[MAXPATHL + 1];
14833 char_u *cpy;
14834 int len;
14835 char_u *remain = NULL;
14836 char_u *q;
14837 int is_relative_to_current = FALSE;
14838 int has_trailing_pathsep = FALSE;
14839 int limit = 100;
14840
14841 p = vim_strsave(p);
14842
14843 if (p[0] == '.' && (vim_ispathsep(p[1])
14844 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14845 is_relative_to_current = TRUE;
14846
14847 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014848 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 has_trailing_pathsep = TRUE;
14850
14851 q = getnextcomp(p);
14852 if (*q != NUL)
14853 {
14854 /* Separate the first path component in "p", and keep the
14855 * remainder (beginning with the path separator). */
14856 remain = vim_strsave(q - 1);
14857 q[-1] = NUL;
14858 }
14859
14860 for (;;)
14861 {
14862 for (;;)
14863 {
14864 len = readlink((char *)p, (char *)buf, MAXPATHL);
14865 if (len <= 0)
14866 break;
14867 buf[len] = NUL;
14868
14869 if (limit-- == 0)
14870 {
14871 vim_free(p);
14872 vim_free(remain);
14873 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 goto fail;
14876 }
14877
14878 /* Ensure that the result will have a trailing path separator
14879 * if the argument has one. */
14880 if (remain == NULL && has_trailing_pathsep)
14881 add_pathsep(buf);
14882
14883 /* Separate the first path component in the link value and
14884 * concatenate the remainders. */
14885 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14886 if (*q != NUL)
14887 {
14888 if (remain == NULL)
14889 remain = vim_strsave(q - 1);
14890 else
14891 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014892 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893 if (cpy != NULL)
14894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 vim_free(remain);
14896 remain = cpy;
14897 }
14898 }
14899 q[-1] = NUL;
14900 }
14901
14902 q = gettail(p);
14903 if (q > p && *q == NUL)
14904 {
14905 /* Ignore trailing path separator. */
14906 q[-1] = NUL;
14907 q = gettail(p);
14908 }
14909 if (q > p && !mch_isFullName(buf))
14910 {
14911 /* symlink is relative to directory of argument */
14912 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14913 if (cpy != NULL)
14914 {
14915 STRCPY(cpy, p);
14916 STRCPY(gettail(cpy), buf);
14917 vim_free(p);
14918 p = cpy;
14919 }
14920 }
14921 else
14922 {
14923 vim_free(p);
14924 p = vim_strsave(buf);
14925 }
14926 }
14927
14928 if (remain == NULL)
14929 break;
14930
14931 /* Append the first path component of "remain" to "p". */
14932 q = getnextcomp(remain + 1);
14933 len = q - remain - (*q != NUL);
14934 cpy = vim_strnsave(p, STRLEN(p) + len);
14935 if (cpy != NULL)
14936 {
14937 STRNCAT(cpy, remain, len);
14938 vim_free(p);
14939 p = cpy;
14940 }
14941 /* Shorten "remain". */
14942 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014943 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 else
14945 {
14946 vim_free(remain);
14947 remain = NULL;
14948 }
14949 }
14950
14951 /* If the result is a relative path name, make it explicitly relative to
14952 * the current directory if and only if the argument had this form. */
14953 if (!vim_ispathsep(*p))
14954 {
14955 if (is_relative_to_current
14956 && *p != NUL
14957 && !(p[0] == '.'
14958 && (p[1] == NUL
14959 || vim_ispathsep(p[1])
14960 || (p[1] == '.'
14961 && (p[2] == NUL
14962 || vim_ispathsep(p[2]))))))
14963 {
14964 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014965 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 if (cpy != NULL)
14967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 vim_free(p);
14969 p = cpy;
14970 }
14971 }
14972 else if (!is_relative_to_current)
14973 {
14974 /* Strip leading "./". */
14975 q = p;
14976 while (q[0] == '.' && vim_ispathsep(q[1]))
14977 q += 2;
14978 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014979 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 }
14981 }
14982
14983 /* Ensure that the result will have no trailing path separator
14984 * if the argument had none. But keep "/" or "//". */
14985 if (!has_trailing_pathsep)
14986 {
14987 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014988 if (after_pathsep(p, q))
14989 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 }
14991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014992 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 }
14994# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014995 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996# endif
14997#endif
14998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000
15001#ifdef HAVE_READLINK
15002fail:
15003#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005}
15006
15007/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 */
15010 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015012 typval_T *argvars;
15013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014{
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 list_T *l;
15016 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 if (argvars[0].v_type != VAR_LIST)
15019 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015020 else if ((l = argvars[0].vval.v_list) != NULL
15021 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022 {
15023 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015024 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015025 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026 while (li != NULL)
15027 {
15028 ni = li->li_prev;
15029 list_append(l, li);
15030 li = ni;
15031 }
15032 rettv->vval.v_list = l;
15033 rettv->v_type = VAR_LIST;
15034 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015035 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037}
15038
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015039#define SP_NOMOVE 0x01 /* don't move cursor */
15040#define SP_REPEAT 0x02 /* repeat to find outer pair */
15041#define SP_RETCOUNT 0x04 /* return matchcount */
15042#define SP_SETPCMARK 0x08 /* set previous context mark */
15043#define SP_START 0x10 /* accept match at start position */
15044#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15045#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015046
Bram Moolenaar33570922005-01-25 22:26:29 +000015047static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048
15049/*
15050 * Get flags for a search function.
15051 * Possibly sets "p_ws".
15052 * Returns BACKWARD, FORWARD or zero (for an error).
15053 */
15054 static int
15055get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015056 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057 int *flagsp;
15058{
15059 int dir = FORWARD;
15060 char_u *flags;
15061 char_u nbuf[NUMBUFLEN];
15062 int mask;
15063
15064 if (varp->v_type != VAR_UNKNOWN)
15065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015066 flags = get_tv_string_buf_chk(varp, nbuf);
15067 if (flags == NULL)
15068 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069 while (*flags != NUL)
15070 {
15071 switch (*flags)
15072 {
15073 case 'b': dir = BACKWARD; break;
15074 case 'w': p_ws = TRUE; break;
15075 case 'W': p_ws = FALSE; break;
15076 default: mask = 0;
15077 if (flagsp != NULL)
15078 switch (*flags)
15079 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015080 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015081 case 'e': mask = SP_END; break;
15082 case 'm': mask = SP_RETCOUNT; break;
15083 case 'n': mask = SP_NOMOVE; break;
15084 case 'p': mask = SP_SUBPAT; break;
15085 case 'r': mask = SP_REPEAT; break;
15086 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 }
15088 if (mask == 0)
15089 {
15090 EMSG2(_(e_invarg2), flags);
15091 dir = 0;
15092 }
15093 else
15094 *flagsp |= mask;
15095 }
15096 if (dir == 0)
15097 break;
15098 ++flags;
15099 }
15100 }
15101 return dir;
15102}
15103
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015105 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015107 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015108search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015109 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015110 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015111 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015113 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 char_u *pat;
15115 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015116 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 int save_p_ws = p_ws;
15118 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015119 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015120 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015121 proftime_T tm;
15122#ifdef FEAT_RELTIME
15123 long time_limit = 0;
15124#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015125 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015126 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015129 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015130 if (dir == 0)
15131 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015132 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015133 if (flags & SP_START)
15134 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015135 if (flags & SP_END)
15136 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015137
Bram Moolenaar76929292008-01-06 19:07:36 +000015138 /* Optional arguments: line number to stop searching and timeout. */
15139 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015140 {
15141 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15142 if (lnum_stop < 0)
15143 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015144#ifdef FEAT_RELTIME
15145 if (argvars[3].v_type != VAR_UNKNOWN)
15146 {
15147 time_limit = get_tv_number_chk(&argvars[3], NULL);
15148 if (time_limit < 0)
15149 goto theend;
15150 }
15151#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015152 }
15153
Bram Moolenaar76929292008-01-06 19:07:36 +000015154#ifdef FEAT_RELTIME
15155 /* Set the time limit, if there is one. */
15156 profile_setlimit(time_limit, &tm);
15157#endif
15158
Bram Moolenaar231334e2005-07-25 20:46:57 +000015159 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015160 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015161 * Check to make sure only those flags are set.
15162 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15163 * flags cannot be set. Check for that condition also.
15164 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015165 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015166 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015168 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015169 goto theend;
15170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015172 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015173 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015174 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015175 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015177 if (flags & SP_SUBPAT)
15178 retval = subpatnum;
15179 else
15180 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015181 if (flags & SP_SETPCMARK)
15182 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015184 if (match_pos != NULL)
15185 {
15186 /* Store the match cursor position */
15187 match_pos->lnum = pos.lnum;
15188 match_pos->col = pos.col + 1;
15189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 /* "/$" will put the cursor after the end of the line, may need to
15191 * correct that here */
15192 check_cursor();
15193 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015194
15195 /* If 'n' flag is used: restore cursor position. */
15196 if (flags & SP_NOMOVE)
15197 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015198 else
15199 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015200theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015202
15203 return retval;
15204}
15205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015206#ifdef FEAT_FLOAT
15207/*
15208 * "round({float})" function
15209 */
15210 static void
15211f_round(argvars, rettv)
15212 typval_T *argvars;
15213 typval_T *rettv;
15214{
15215 float_T f;
15216
15217 rettv->v_type = VAR_FLOAT;
15218 if (get_float_arg(argvars, &f) == OK)
15219 /* round() is not in C90, use ceil() or floor() instead. */
15220 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15221 else
15222 rettv->vval.v_float = 0.0;
15223}
15224#endif
15225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015226/*
15227 * "search()" function
15228 */
15229 static void
15230f_search(argvars, rettv)
15231 typval_T *argvars;
15232 typval_T *rettv;
15233{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015234 int flags = 0;
15235
15236 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237}
15238
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015240 * "searchdecl()" function
15241 */
15242 static void
15243f_searchdecl(argvars, rettv)
15244 typval_T *argvars;
15245 typval_T *rettv;
15246{
15247 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015248 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015249 int error = FALSE;
15250 char_u *name;
15251
15252 rettv->vval.v_number = 1; /* default: FAIL */
15253
15254 name = get_tv_string_chk(&argvars[0]);
15255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015256 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015257 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015258 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15259 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15260 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015261 if (!error && name != NULL)
15262 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015263 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015264}
15265
15266/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015267 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015269 static int
15270searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015271 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015272 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273{
15274 char_u *spat, *mpat, *epat;
15275 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 int dir;
15278 int flags = 0;
15279 char_u nbuf1[NUMBUFLEN];
15280 char_u nbuf2[NUMBUFLEN];
15281 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015282 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015283 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015284 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015287 spat = get_tv_string_chk(&argvars[0]);
15288 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15289 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15290 if (spat == NULL || mpat == NULL || epat == NULL)
15291 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 /* Handle the optional fourth argument: flags */
15294 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015295 if (dir == 0)
15296 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015297
15298 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015299 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15300 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015301 if ((flags & (SP_END | SP_SUBPAT)) != 0
15302 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015303 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015304 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015305 goto theend;
15306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015308 /* Using 'r' implies 'W', otherwise it doesn't work. */
15309 if (flags & SP_REPEAT)
15310 p_ws = FALSE;
15311
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015312 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015313 if (argvars[3].v_type == VAR_UNKNOWN
15314 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 skip = (char_u *)"";
15316 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015319 if (argvars[5].v_type != VAR_UNKNOWN)
15320 {
15321 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15322 if (lnum_stop < 0)
15323 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015324#ifdef FEAT_RELTIME
15325 if (argvars[6].v_type != VAR_UNKNOWN)
15326 {
15327 time_limit = get_tv_number_chk(&argvars[6], NULL);
15328 if (time_limit < 0)
15329 goto theend;
15330 }
15331#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 }
15333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015334 if (skip == NULL)
15335 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015337 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015338 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015339
15340theend:
15341 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015342
15343 return retval;
15344}
15345
15346/*
15347 * "searchpair()" function
15348 */
15349 static void
15350f_searchpair(argvars, rettv)
15351 typval_T *argvars;
15352 typval_T *rettv;
15353{
15354 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15355}
15356
15357/*
15358 * "searchpairpos()" function
15359 */
15360 static void
15361f_searchpairpos(argvars, rettv)
15362 typval_T *argvars;
15363 typval_T *rettv;
15364{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015365 pos_T match_pos;
15366 int lnum = 0;
15367 int col = 0;
15368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015370 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015371
15372 if (searchpair_cmn(argvars, &match_pos) > 0)
15373 {
15374 lnum = match_pos.lnum;
15375 col = match_pos.col;
15376 }
15377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015378 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15379 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015380}
15381
15382/*
15383 * Search for a start/middle/end thing.
15384 * Used by searchpair(), see its documentation for the details.
15385 * Returns 0 or -1 for no match,
15386 */
15387 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015388do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15389 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015390 char_u *spat; /* start pattern */
15391 char_u *mpat; /* middle pattern */
15392 char_u *epat; /* end pattern */
15393 int dir; /* BACKWARD or FORWARD */
15394 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015395 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015396 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015397 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015398 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015399{
15400 char_u *save_cpo;
15401 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15402 long retval = 0;
15403 pos_T pos;
15404 pos_T firstpos;
15405 pos_T foundpos;
15406 pos_T save_cursor;
15407 pos_T save_pos;
15408 int n;
15409 int r;
15410 int nest = 1;
15411 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015412 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015413 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015414
15415 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15416 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015417 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015418
Bram Moolenaar76929292008-01-06 19:07:36 +000015419#ifdef FEAT_RELTIME
15420 /* Set the time limit, if there is one. */
15421 profile_setlimit(time_limit, &tm);
15422#endif
15423
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015424 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15425 * start/middle/end (pat3, for the top pair). */
15426 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15427 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15428 if (pat2 == NULL || pat3 == NULL)
15429 goto theend;
15430 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15431 if (*mpat == NUL)
15432 STRCPY(pat3, pat2);
15433 else
15434 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15435 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015436 if (flags & SP_START)
15437 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015438
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 save_cursor = curwin->w_cursor;
15440 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015441 clearpos(&firstpos);
15442 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 pat = pat3;
15444 for (;;)
15445 {
15446 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015447 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15449 /* didn't find it or found the first match again: FAIL */
15450 break;
15451
15452 if (firstpos.lnum == 0)
15453 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015454 if (equalpos(pos, foundpos))
15455 {
15456 /* Found the same position again. Can happen with a pattern that
15457 * has "\zs" at the end and searching backwards. Advance one
15458 * character and try again. */
15459 if (dir == BACKWARD)
15460 decl(&pos);
15461 else
15462 incl(&pos);
15463 }
15464 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015466 /* clear the start flag to avoid getting stuck here */
15467 options &= ~SEARCH_START;
15468
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 /* If the skip pattern matches, ignore this match. */
15470 if (*skip != NUL)
15471 {
15472 save_pos = curwin->w_cursor;
15473 curwin->w_cursor = pos;
15474 r = eval_to_bool(skip, &err, NULL, FALSE);
15475 curwin->w_cursor = save_pos;
15476 if (err)
15477 {
15478 /* Evaluating {skip} caused an error, break here. */
15479 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015480 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 break;
15482 }
15483 if (r)
15484 continue;
15485 }
15486
15487 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15488 {
15489 /* Found end when searching backwards or start when searching
15490 * forward: nested pair. */
15491 ++nest;
15492 pat = pat2; /* nested, don't search for middle */
15493 }
15494 else
15495 {
15496 /* Found end when searching forward or start when searching
15497 * backward: end of (nested) pair; or found middle in outer pair. */
15498 if (--nest == 1)
15499 pat = pat3; /* outer level, search for middle */
15500 }
15501
15502 if (nest == 0)
15503 {
15504 /* Found the match: return matchcount or line number. */
15505 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015506 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015508 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015509 if (flags & SP_SETPCMARK)
15510 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 curwin->w_cursor = pos;
15512 if (!(flags & SP_REPEAT))
15513 break;
15514 nest = 1; /* search for next unmatched */
15515 }
15516 }
15517
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015518 if (match_pos != NULL)
15519 {
15520 /* Store the match cursor position */
15521 match_pos->lnum = curwin->w_cursor.lnum;
15522 match_pos->col = curwin->w_cursor.col + 1;
15523 }
15524
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015526 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 curwin->w_cursor = save_cursor;
15528
15529theend:
15530 vim_free(pat2);
15531 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015532 if (p_cpo == empty_option)
15533 p_cpo = save_cpo;
15534 else
15535 /* Darn, evaluating the {skip} expression changed the value. */
15536 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015537
15538 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015541/*
15542 * "searchpos()" function
15543 */
15544 static void
15545f_searchpos(argvars, rettv)
15546 typval_T *argvars;
15547 typval_T *rettv;
15548{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015549 pos_T match_pos;
15550 int lnum = 0;
15551 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015552 int n;
15553 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015554
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015555 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015556 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015557
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015558 n = search_cmn(argvars, &match_pos, &flags);
15559 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015560 {
15561 lnum = match_pos.lnum;
15562 col = match_pos.col;
15563 }
15564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015565 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15566 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015567 if (flags & SP_SUBPAT)
15568 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015569}
15570
15571
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 static void
15573f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015574 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015577#ifdef FEAT_CLIENTSERVER
15578 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015579 char_u *server = get_tv_string_chk(&argvars[0]);
15580 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 if (server == NULL || reply == NULL)
15584 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015585 if (check_restricted() || check_secure())
15586 return;
15587# ifdef FEAT_X11
15588 if (check_connection() == FAIL)
15589 return;
15590# endif
15591
15592 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015594 EMSG(_("E258: Unable to send to client"));
15595 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 rettv->vval.v_number = 0;
15598#else
15599 rettv->vval.v_number = -1;
15600#endif
15601}
15602
Bram Moolenaar0d660222005-01-07 21:51:51 +000015603 static void
15604f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015607{
15608 char_u *r = NULL;
15609
15610#ifdef FEAT_CLIENTSERVER
15611# ifdef WIN32
15612 r = serverGetVimNames();
15613# else
15614 make_connection();
15615 if (X_DISPLAY != NULL)
15616 r = serverGetVimNames(X_DISPLAY);
15617# endif
15618#endif
15619 rettv->v_type = VAR_STRING;
15620 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621}
15622
15623/*
15624 * "setbufvar()" function
15625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015628 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015629 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630{
15631 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015634 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 char_u nbuf[NUMBUFLEN];
15636
15637 if (check_restricted() || check_secure())
15638 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15640 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015641 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 varp = &argvars[2];
15643
15644 if (buf != NULL && varname != NULL && varp != NULL)
15645 {
15646 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648
15649 if (*varname == '&')
15650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015651 long numval;
15652 char_u *strval;
15653 int error = FALSE;
15654
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656 numval = get_tv_number_chk(varp, &error);
15657 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 if (!error && strval != NULL)
15659 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 }
15661 else
15662 {
15663 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15664 if (bufvarname != NULL)
15665 {
15666 STRCPY(bufvarname, "b:");
15667 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015668 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 vim_free(bufvarname);
15670 }
15671 }
15672
15673 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676}
15677
15678/*
15679 * "setcmdpos()" function
15680 */
15681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015682f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015683 typval_T *argvars;
15684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015686 int pos = (int)get_tv_number(&argvars[0]) - 1;
15687
15688 if (pos >= 0)
15689 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690}
15691
15692/*
15693 * "setline()" function
15694 */
15695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015696f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015697 typval_T *argvars;
15698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699{
15700 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015701 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015702 list_T *l = NULL;
15703 listitem_T *li = NULL;
15704 long added = 0;
15705 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015707 lnum = get_tv_lnum(&argvars[0]);
15708 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015710 l = argvars[1].vval.v_list;
15711 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015713 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015714 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015715
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015716 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015717 for (;;)
15718 {
15719 if (l != NULL)
15720 {
15721 /* list argument, get next string */
15722 if (li == NULL)
15723 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015725 li = li->li_next;
15726 }
15727
15728 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015730 break;
15731 if (lnum <= curbuf->b_ml.ml_line_count)
15732 {
15733 /* existing line, replace it */
15734 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15735 {
15736 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015737 if (lnum == curwin->w_cursor.lnum)
15738 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015739 rettv->vval.v_number = 0; /* OK */
15740 }
15741 }
15742 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15743 {
15744 /* lnum is one past the last line, append the line */
15745 ++added;
15746 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15747 rettv->vval.v_number = 0; /* OK */
15748 }
15749
15750 if (l == NULL) /* only one string argument */
15751 break;
15752 ++lnum;
15753 }
15754
15755 if (added > 0)
15756 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757}
15758
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015759static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15760
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015762 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015763 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015764 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015765set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015766 win_T *wp UNUSED;
15767 typval_T *list_arg UNUSED;
15768 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015769 typval_T *rettv;
15770{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015771#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015772 char_u *act;
15773 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015774#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015775
Bram Moolenaar2641f772005-03-25 21:58:17 +000015776 rettv->vval.v_number = -1;
15777
15778#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015779 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015780 EMSG(_(e_listreq));
15781 else
15782 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015783 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015784
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015785 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015786 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015787 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015788 if (act == NULL)
15789 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015790 if (*act == 'a' || *act == 'r')
15791 action = *act;
15792 }
15793
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015794 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015795 rettv->vval.v_number = 0;
15796 }
15797#endif
15798}
15799
15800/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015801 * "setloclist()" function
15802 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015803 static void
15804f_setloclist(argvars, rettv)
15805 typval_T *argvars;
15806 typval_T *rettv;
15807{
15808 win_T *win;
15809
15810 rettv->vval.v_number = -1;
15811
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015812 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015813 if (win != NULL)
15814 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15815}
15816
15817/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015818 * "setmatches()" function
15819 */
15820 static void
15821f_setmatches(argvars, rettv)
15822 typval_T *argvars;
15823 typval_T *rettv;
15824{
15825#ifdef FEAT_SEARCH_EXTRA
15826 list_T *l;
15827 listitem_T *li;
15828 dict_T *d;
15829
15830 rettv->vval.v_number = -1;
15831 if (argvars[0].v_type != VAR_LIST)
15832 {
15833 EMSG(_(e_listreq));
15834 return;
15835 }
15836 if ((l = argvars[0].vval.v_list) != NULL)
15837 {
15838
15839 /* To some extent make sure that we are dealing with a list from
15840 * "getmatches()". */
15841 li = l->lv_first;
15842 while (li != NULL)
15843 {
15844 if (li->li_tv.v_type != VAR_DICT
15845 || (d = li->li_tv.vval.v_dict) == NULL)
15846 {
15847 EMSG(_(e_invarg));
15848 return;
15849 }
15850 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15851 && dict_find(d, (char_u *)"pattern", -1) != NULL
15852 && dict_find(d, (char_u *)"priority", -1) != NULL
15853 && dict_find(d, (char_u *)"id", -1) != NULL))
15854 {
15855 EMSG(_(e_invarg));
15856 return;
15857 }
15858 li = li->li_next;
15859 }
15860
15861 clear_matches(curwin);
15862 li = l->lv_first;
15863 while (li != NULL)
15864 {
15865 d = li->li_tv.vval.v_dict;
15866 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15867 get_dict_string(d, (char_u *)"pattern", FALSE),
15868 (int)get_dict_number(d, (char_u *)"priority"),
15869 (int)get_dict_number(d, (char_u *)"id"));
15870 li = li->li_next;
15871 }
15872 rettv->vval.v_number = 0;
15873 }
15874#endif
15875}
15876
15877/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015878 * "setpos()" function
15879 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015880 static void
15881f_setpos(argvars, rettv)
15882 typval_T *argvars;
15883 typval_T *rettv;
15884{
15885 pos_T pos;
15886 int fnum;
15887 char_u *name;
15888
Bram Moolenaar08250432008-02-13 11:42:46 +000015889 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015890 name = get_tv_string_chk(argvars);
15891 if (name != NULL)
15892 {
15893 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15894 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015895 if (--pos.col < 0)
15896 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015897 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015898 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015899 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015900 if (fnum == curbuf->b_fnum)
15901 {
15902 curwin->w_cursor = pos;
15903 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015904 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015905 }
15906 else
15907 EMSG(_(e_invarg));
15908 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015909 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15910 {
15911 /* set mark */
15912 if (setmark_pos(name[1], &pos, fnum) == OK)
15913 rettv->vval.v_number = 0;
15914 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015915 else
15916 EMSG(_(e_invarg));
15917 }
15918 }
15919}
15920
15921/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015922 * "setqflist()" function
15923 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015924 static void
15925f_setqflist(argvars, rettv)
15926 typval_T *argvars;
15927 typval_T *rettv;
15928{
15929 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15930}
15931
15932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 * "setreg()" function
15934 */
15935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015936f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015937 typval_T *argvars;
15938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939{
15940 int regname;
15941 char_u *strregname;
15942 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015943 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 int append;
15945 char_u yank_type;
15946 long block_len;
15947
15948 block_len = -1;
15949 yank_type = MAUTO;
15950 append = FALSE;
15951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015952 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015953 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015955 if (strregname == NULL)
15956 return; /* type error; errmsg already given */
15957 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 if (regname == 0 || regname == '@')
15959 regname = '"';
15960 else if (regname == '=')
15961 return;
15962
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015965 stropt = get_tv_string_chk(&argvars[2]);
15966 if (stropt == NULL)
15967 return; /* type error */
15968 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 switch (*stropt)
15970 {
15971 case 'a': case 'A': /* append */
15972 append = TRUE;
15973 break;
15974 case 'v': case 'c': /* character-wise selection */
15975 yank_type = MCHAR;
15976 break;
15977 case 'V': case 'l': /* line-wise selection */
15978 yank_type = MLINE;
15979 break;
15980#ifdef FEAT_VISUAL
15981 case 'b': case Ctrl_V: /* block-wise selection */
15982 yank_type = MBLOCK;
15983 if (VIM_ISDIGIT(stropt[1]))
15984 {
15985 ++stropt;
15986 block_len = getdigits(&stropt) - 1;
15987 --stropt;
15988 }
15989 break;
15990#endif
15991 }
15992 }
15993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015994 strval = get_tv_string_chk(&argvars[1]);
15995 if (strval != NULL)
15996 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015998 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999}
16000
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016001/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016002 * "settabvar()" function
16003 */
16004 static void
16005f_settabvar(argvars, rettv)
16006 typval_T *argvars;
16007 typval_T *rettv;
16008{
16009 tabpage_T *save_curtab;
16010 char_u *varname, *tabvarname;
16011 typval_T *varp;
16012 tabpage_T *tp;
16013
16014 rettv->vval.v_number = 0;
16015
16016 if (check_restricted() || check_secure())
16017 return;
16018
16019 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16020 varname = get_tv_string_chk(&argvars[1]);
16021 varp = &argvars[2];
16022
16023 if (tp != NULL && varname != NULL && varp != NULL)
16024 {
16025 save_curtab = curtab;
16026 goto_tabpage_tp(tp);
16027
16028 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16029 if (tabvarname != NULL)
16030 {
16031 STRCPY(tabvarname, "t:");
16032 STRCPY(tabvarname + 2, varname);
16033 set_var(tabvarname, varp, TRUE);
16034 vim_free(tabvarname);
16035 }
16036
16037 /* Restore current tabpage */
16038 if (valid_tabpage(save_curtab))
16039 goto_tabpage_tp(save_curtab);
16040 }
16041}
16042
16043/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016044 * "settabwinvar()" function
16045 */
16046 static void
16047f_settabwinvar(argvars, rettv)
16048 typval_T *argvars;
16049 typval_T *rettv;
16050{
16051 setwinvar(argvars, rettv, 1);
16052}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053
16054/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016055 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016058f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 typval_T *argvars;
16060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016062 setwinvar(argvars, rettv, 0);
16063}
16064
16065/*
16066 * "setwinvar()" and "settabwinvar()" functions
16067 */
16068 static void
16069setwinvar(argvars, rettv, off)
16070 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016071 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016072 int off;
16073{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 win_T *win;
16075#ifdef FEAT_WINDOWS
16076 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016077 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078#endif
16079 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016082 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083
16084 if (check_restricted() || check_secure())
16085 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016086
16087#ifdef FEAT_WINDOWS
16088 if (off == 1)
16089 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16090 else
16091 tp = curtab;
16092#endif
16093 win = find_win_by_nr(&argvars[off], tp);
16094 varname = get_tv_string_chk(&argvars[off + 1]);
16095 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096
16097 if (win != NULL && varname != NULL && varp != NULL)
16098 {
16099#ifdef FEAT_WINDOWS
16100 /* set curwin to be our win, temporarily */
16101 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016102 save_curtab = curtab;
16103 goto_tabpage_tp(tp);
16104 if (!win_valid(win))
16105 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 curwin = win;
16107 curbuf = curwin->w_buffer;
16108#endif
16109
16110 if (*varname == '&')
16111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016112 long numval;
16113 char_u *strval;
16114 int error = FALSE;
16115
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016117 numval = get_tv_number_chk(varp, &error);
16118 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016119 if (!error && strval != NULL)
16120 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 }
16122 else
16123 {
16124 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16125 if (winvarname != NULL)
16126 {
16127 STRCPY(winvarname, "w:");
16128 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016129 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 vim_free(winvarname);
16131 }
16132 }
16133
16134#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016135 /* Restore current tabpage and window, if still valid (autocomands can
16136 * make them invalid). */
16137 if (valid_tabpage(save_curtab))
16138 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 if (win_valid(save_curwin))
16140 {
16141 curwin = save_curwin;
16142 curbuf = curwin->w_buffer;
16143 }
16144#endif
16145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146}
16147
16148/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016149 * "shellescape({string})" function
16150 */
16151 static void
16152f_shellescape(argvars, rettv)
16153 typval_T *argvars;
16154 typval_T *rettv;
16155{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016156 rettv->vval.v_string = vim_strsave_shellescape(
16157 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016158 rettv->v_type = VAR_STRING;
16159}
16160
16161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 */
16164 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 typval_T *argvars;
16167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171 p = get_tv_string(&argvars[0]);
16172 rettv->vval.v_string = vim_strsave(p);
16173 simplify_filename(rettv->vval.v_string); /* simplify in place */
16174 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175}
16176
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016177#ifdef FEAT_FLOAT
16178/*
16179 * "sin()" function
16180 */
16181 static void
16182f_sin(argvars, rettv)
16183 typval_T *argvars;
16184 typval_T *rettv;
16185{
16186 float_T f;
16187
16188 rettv->v_type = VAR_FLOAT;
16189 if (get_float_arg(argvars, &f) == OK)
16190 rettv->vval.v_float = sin(f);
16191 else
16192 rettv->vval.v_float = 0.0;
16193}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016194
16195/*
16196 * "sinh()" function
16197 */
16198 static void
16199f_sinh(argvars, rettv)
16200 typval_T *argvars;
16201 typval_T *rettv;
16202{
16203 float_T f;
16204
16205 rettv->v_type = VAR_FLOAT;
16206 if (get_float_arg(argvars, &f) == OK)
16207 rettv->vval.v_float = sinh(f);
16208 else
16209 rettv->vval.v_float = 0.0;
16210}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016211#endif
16212
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213static int
16214#ifdef __BORLANDC__
16215 _RTLENTRYF
16216#endif
16217 item_compare __ARGS((const void *s1, const void *s2));
16218static int
16219#ifdef __BORLANDC__
16220 _RTLENTRYF
16221#endif
16222 item_compare2 __ARGS((const void *s1, const void *s2));
16223
16224static int item_compare_ic;
16225static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227#define ITEM_COMPARE_FAIL 999
16228
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016232 static int
16233#ifdef __BORLANDC__
16234_RTLENTRYF
16235#endif
16236item_compare(s1, s2)
16237 const void *s1;
16238 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240 char_u *p1, *p2;
16241 char_u *tofree1, *tofree2;
16242 int res;
16243 char_u numbuf1[NUMBUFLEN];
16244 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016246 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16247 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016248 if (p1 == NULL)
16249 p1 = (char_u *)"";
16250 if (p2 == NULL)
16251 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 if (item_compare_ic)
16253 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 res = STRCMP(p1, p2);
16256 vim_free(tofree1);
16257 vim_free(tofree2);
16258 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259}
16260
16261 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262#ifdef __BORLANDC__
16263_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265item_compare2(s1, s2)
16266 const void *s1;
16267 const void *s2;
16268{
16269 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016270 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016271 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016272 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 /* shortcut after failure in previous call; compare all items equal */
16275 if (item_compare_func_err)
16276 return 0;
16277
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016278 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16279 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16281 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282
16283 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016284 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016285 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 clear_tv(&argv[0]);
16287 clear_tv(&argv[1]);
16288
16289 if (res == FAIL)
16290 res = ITEM_COMPARE_FAIL;
16291 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16293 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016294 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 clear_tv(&rettv);
16296 return res;
16297}
16298
16299/*
16300 * "sort({list})" function
16301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 typval_T *argvars;
16305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306{
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 list_T *l;
16308 listitem_T *li;
16309 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 long len;
16311 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313 if (argvars[0].v_type != VAR_LIST)
16314 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 else
16316 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016318 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319 return;
16320 rettv->vval.v_list = l;
16321 rettv->v_type = VAR_LIST;
16322 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323
Bram Moolenaar0d660222005-01-07 21:51:51 +000016324 len = list_len(l);
16325 if (len <= 1)
16326 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 item_compare_ic = FALSE;
16329 item_compare_func = NULL;
16330 if (argvars[1].v_type != VAR_UNKNOWN)
16331 {
16332 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016334 else
16335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016336 int error = FALSE;
16337
16338 i = get_tv_number_chk(&argvars[1], &error);
16339 if (error)
16340 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341 if (i == 1)
16342 item_compare_ic = TRUE;
16343 else
16344 item_compare_func = get_tv_string(&argvars[1]);
16345 }
16346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350 if (ptrs == NULL)
16351 return;
16352 i = 0;
16353 for (li = l->lv_first; li != NULL; li = li->li_next)
16354 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016356 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 /* test the compare function */
16358 if (item_compare_func != NULL
16359 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16360 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016361 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 {
16364 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016365 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366 item_compare_func == NULL ? item_compare : item_compare2);
16367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 if (!item_compare_func_err)
16369 {
16370 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016371 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016372 l->lv_len = 0;
16373 for (i = 0; i < len; ++i)
16374 list_append(l, ptrs[i]);
16375 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 }
16377
16378 vim_free(ptrs);
16379 }
16380}
16381
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016382/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016383 * "soundfold({word})" function
16384 */
16385 static void
16386f_soundfold(argvars, rettv)
16387 typval_T *argvars;
16388 typval_T *rettv;
16389{
16390 char_u *s;
16391
16392 rettv->v_type = VAR_STRING;
16393 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016394#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016395 rettv->vval.v_string = eval_soundfold(s);
16396#else
16397 rettv->vval.v_string = vim_strsave(s);
16398#endif
16399}
16400
16401/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016402 * "spellbadword()" function
16403 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016404 static void
16405f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016406 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016407 typval_T *rettv;
16408{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016409 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016410 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016411 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016414 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016415
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016416#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016417 if (argvars[0].v_type == VAR_UNKNOWN)
16418 {
16419 /* Find the start and length of the badly spelled word. */
16420 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16421 if (len != 0)
16422 word = ml_get_cursor();
16423 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016424 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016425 {
16426 char_u *str = get_tv_string_chk(&argvars[0]);
16427 int capcol = -1;
16428
16429 if (str != NULL)
16430 {
16431 /* Check the argument for spelling. */
16432 while (*str != NUL)
16433 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016434 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016435 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016436 {
16437 word = str;
16438 break;
16439 }
16440 str += len;
16441 }
16442 }
16443 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016444#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016445
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016446 list_append_string(rettv->vval.v_list, word, len);
16447 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016448 attr == HLF_SPB ? "bad" :
16449 attr == HLF_SPR ? "rare" :
16450 attr == HLF_SPL ? "local" :
16451 attr == HLF_SPC ? "caps" :
16452 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016453}
16454
16455/*
16456 * "spellsuggest()" function
16457 */
16458 static void
16459f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016460 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016461 typval_T *rettv;
16462{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016463#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016464 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016465 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016466 int maxcount;
16467 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016468 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016469 listitem_T *li;
16470 int need_capital = FALSE;
16471#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016472
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016473 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016474 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016475
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016476#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016477 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016478 {
16479 str = get_tv_string(&argvars[0]);
16480 if (argvars[1].v_type != VAR_UNKNOWN)
16481 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016482 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016483 if (maxcount <= 0)
16484 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016485 if (argvars[2].v_type != VAR_UNKNOWN)
16486 {
16487 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16488 if (typeerr)
16489 return;
16490 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016491 }
16492 else
16493 maxcount = 25;
16494
Bram Moolenaar4770d092006-01-12 23:22:24 +000016495 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016496
16497 for (i = 0; i < ga.ga_len; ++i)
16498 {
16499 str = ((char_u **)ga.ga_data)[i];
16500
16501 li = listitem_alloc();
16502 if (li == NULL)
16503 vim_free(str);
16504 else
16505 {
16506 li->li_tv.v_type = VAR_STRING;
16507 li->li_tv.v_lock = 0;
16508 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016509 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016510 }
16511 }
16512 ga_clear(&ga);
16513 }
16514#endif
16515}
16516
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016518f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016519 typval_T *argvars;
16520 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521{
16522 char_u *str;
16523 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016524 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 regmatch_T regmatch;
16526 char_u patbuf[NUMBUFLEN];
16527 char_u *save_cpo;
16528 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016530 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532
16533 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16534 save_cpo = p_cpo;
16535 p_cpo = (char_u *)"";
16536
16537 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016538 if (argvars[1].v_type != VAR_UNKNOWN)
16539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016540 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16541 if (pat == NULL)
16542 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016544 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016545 }
16546 if (pat == NULL || *pat == NUL)
16547 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016549 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 if (typeerr)
16552 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16555 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016558 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016560 if (*str == NUL)
16561 match = FALSE; /* empty item at the end */
16562 else
16563 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564 if (match)
16565 end = regmatch.startp[0];
16566 else
16567 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016568 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16569 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016570 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016571 if (list_append_string(rettv->vval.v_list, str,
16572 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574 }
16575 if (!match)
16576 break;
16577 /* Advance to just after the match. */
16578 if (regmatch.endp[0] > str)
16579 col = 0;
16580 else
16581 {
16582 /* Don't get stuck at the same match. */
16583#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016584 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585#else
16586 col = 1;
16587#endif
16588 }
16589 str = regmatch.endp[0];
16590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594
Bram Moolenaar0d660222005-01-07 21:51:51 +000016595 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596}
16597
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016598#ifdef FEAT_FLOAT
16599/*
16600 * "sqrt()" function
16601 */
16602 static void
16603f_sqrt(argvars, rettv)
16604 typval_T *argvars;
16605 typval_T *rettv;
16606{
16607 float_T f;
16608
16609 rettv->v_type = VAR_FLOAT;
16610 if (get_float_arg(argvars, &f) == OK)
16611 rettv->vval.v_float = sqrt(f);
16612 else
16613 rettv->vval.v_float = 0.0;
16614}
16615
16616/*
16617 * "str2float()" function
16618 */
16619 static void
16620f_str2float(argvars, rettv)
16621 typval_T *argvars;
16622 typval_T *rettv;
16623{
16624 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16625
16626 if (*p == '+')
16627 p = skipwhite(p + 1);
16628 (void)string2float(p, &rettv->vval.v_float);
16629 rettv->v_type = VAR_FLOAT;
16630}
16631#endif
16632
Bram Moolenaar2c932302006-03-18 21:42:09 +000016633/*
16634 * "str2nr()" function
16635 */
16636 static void
16637f_str2nr(argvars, rettv)
16638 typval_T *argvars;
16639 typval_T *rettv;
16640{
16641 int base = 10;
16642 char_u *p;
16643 long n;
16644
16645 if (argvars[1].v_type != VAR_UNKNOWN)
16646 {
16647 base = get_tv_number(&argvars[1]);
16648 if (base != 8 && base != 10 && base != 16)
16649 {
16650 EMSG(_(e_invarg));
16651 return;
16652 }
16653 }
16654
16655 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016656 if (*p == '+')
16657 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016658 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16659 rettv->vval.v_number = n;
16660}
16661
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662#ifdef HAVE_STRFTIME
16663/*
16664 * "strftime({format}[, {time}])" function
16665 */
16666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016667f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016668 typval_T *argvars;
16669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670{
16671 char_u result_buf[256];
16672 struct tm *curtime;
16673 time_t seconds;
16674 char_u *p;
16675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016679 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 seconds = time(NULL);
16681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016682 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683 curtime = localtime(&seconds);
16684 /* MSVC returns NULL for an invalid value of seconds. */
16685 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016686 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 else
16688 {
16689# ifdef FEAT_MBYTE
16690 vimconv_T conv;
16691 char_u *enc;
16692
16693 conv.vc_type = CONV_NONE;
16694 enc = enc_locale();
16695 convert_setup(&conv, p_enc, enc);
16696 if (conv.vc_type != CONV_NONE)
16697 p = string_convert(&conv, p, NULL);
16698# endif
16699 if (p != NULL)
16700 (void)strftime((char *)result_buf, sizeof(result_buf),
16701 (char *)p, curtime);
16702 else
16703 result_buf[0] = NUL;
16704
16705# ifdef FEAT_MBYTE
16706 if (conv.vc_type != CONV_NONE)
16707 vim_free(p);
16708 convert_setup(&conv, enc, p_enc);
16709 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016710 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 else
16712# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714
16715# ifdef FEAT_MBYTE
16716 /* Release conversion descriptors */
16717 convert_setup(&conv, NULL, NULL);
16718 vim_free(enc);
16719# endif
16720 }
16721}
16722#endif
16723
16724/*
16725 * "stridx()" function
16726 */
16727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 typval_T *argvars;
16730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731{
16732 char_u buf[NUMBUFLEN];
16733 char_u *needle;
16734 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016735 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016737 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016739 needle = get_tv_string_chk(&argvars[1]);
16740 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016741 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 if (needle == NULL || haystack == NULL)
16743 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744
Bram Moolenaar33570922005-01-25 22:26:29 +000016745 if (argvars[2].v_type != VAR_UNKNOWN)
16746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016747 int error = FALSE;
16748
16749 start_idx = get_tv_number_chk(&argvars[2], &error);
16750 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016752 if (start_idx >= 0)
16753 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016754 }
16755
16756 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16757 if (pos != NULL)
16758 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759}
16760
16761/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016762 * "string()" function
16763 */
16764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 typval_T *argvars;
16767 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016768{
16769 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016770 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016773 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016774 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016775 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016776 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777}
16778
16779/*
16780 * "strlen()" function
16781 */
16782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783f_strlen(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 Moolenaarc70646c2005-01-04 21:52:38 +000016787 rettv->vval.v_number = (varnumber_T)(STRLEN(
16788 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789}
16790
16791/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016792 * "strchars()" function
16793 */
16794 static void
16795f_strchars(argvars, rettv)
16796 typval_T *argvars;
16797 typval_T *rettv;
16798{
16799 char_u *s = get_tv_string(&argvars[0]);
16800#ifdef FEAT_MBYTE
16801 varnumber_T len = 0;
16802
16803 while (*s != NUL)
16804 {
16805 mb_cptr2char_adv(&s);
16806 ++len;
16807 }
16808 rettv->vval.v_number = len;
16809#else
16810 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16811#endif
16812}
16813
16814/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016815 * "strdisplaywidth()" function
16816 */
16817 static void
16818f_strdisplaywidth(argvars, rettv)
16819 typval_T *argvars;
16820 typval_T *rettv;
16821{
16822 char_u *s = get_tv_string(&argvars[0]);
16823 int col = 0;
16824
16825 if (argvars[1].v_type != VAR_UNKNOWN)
16826 col = get_tv_number(&argvars[1]);
16827
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016828 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016829}
16830
16831/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016832 * "strwidth()" function
16833 */
16834 static void
16835f_strwidth(argvars, rettv)
16836 typval_T *argvars;
16837 typval_T *rettv;
16838{
16839 char_u *s = get_tv_string(&argvars[0]);
16840
16841 rettv->vval.v_number = (varnumber_T)(
16842#ifdef FEAT_MBYTE
16843 mb_string2cells(s, -1)
16844#else
16845 STRLEN(s)
16846#endif
16847 );
16848}
16849
16850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 * "strpart()" function
16852 */
16853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016854f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 typval_T *argvars;
16856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857{
16858 char_u *p;
16859 int n;
16860 int len;
16861 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016864 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 slen = (int)STRLEN(p);
16866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 n = get_tv_number_chk(&argvars[1], &error);
16868 if (error)
16869 len = 0;
16870 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016871 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872 else
16873 len = slen - n; /* default len: all bytes that are available. */
16874
16875 /*
16876 * Only return the overlap between the specified part and the actual
16877 * string.
16878 */
16879 if (n < 0)
16880 {
16881 len += n;
16882 n = 0;
16883 }
16884 else if (n > slen)
16885 n = slen;
16886 if (len < 0)
16887 len = 0;
16888 else if (n + len > slen)
16889 len = slen - n;
16890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016891 rettv->v_type = VAR_STRING;
16892 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893}
16894
16895/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016896 * "strridx()" function
16897 */
16898 static void
16899f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 typval_T *argvars;
16901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902{
16903 char_u buf[NUMBUFLEN];
16904 char_u *needle;
16905 char_u *haystack;
16906 char_u *rest;
16907 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016908 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016910 needle = get_tv_string_chk(&argvars[1]);
16911 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912
16913 rettv->vval.v_number = -1;
16914 if (needle == NULL || haystack == NULL)
16915 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016916
16917 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016918 if (argvars[2].v_type != VAR_UNKNOWN)
16919 {
16920 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016921 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016922 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016923 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016924 }
16925 else
16926 end_idx = haystack_len;
16927
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016929 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016931 lastmatch = haystack + end_idx;
16932 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016934 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 for (rest = haystack; *rest != '\0'; ++rest)
16936 {
16937 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016938 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 break;
16940 lastmatch = rest;
16941 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016942 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943
16944 if (lastmatch == NULL)
16945 rettv->vval.v_number = -1;
16946 else
16947 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16948}
16949
16950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 * "strtrans()" function
16952 */
16953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016954f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 typval_T *argvars;
16956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958 rettv->v_type = VAR_STRING;
16959 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960}
16961
16962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 * "submatch()" function
16964 */
16965 static void
16966f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016967 typval_T *argvars;
16968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969{
16970 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971 rettv->vval.v_string =
16972 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973}
16974
16975/*
16976 * "substitute()" function
16977 */
16978 static void
16979f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 typval_T *argvars;
16981 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982{
16983 char_u patbuf[NUMBUFLEN];
16984 char_u subbuf[NUMBUFLEN];
16985 char_u flagsbuf[NUMBUFLEN];
16986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016987 char_u *str = get_tv_string_chk(&argvars[0]);
16988 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16989 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16990 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16991
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16994 rettv->vval.v_string = NULL;
16995 else
16996 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997}
16998
16999/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017000 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017004 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006{
17007 int id = 0;
17008#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017009 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 long col;
17011 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017012 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017014 lnum = get_tv_lnum(argvars); /* -1 on type error */
17015 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17016 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017019 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017020 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021#endif
17022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017023 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024}
17025
17026/*
17027 * "synIDattr(id, what [, mode])" function
17028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017030f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033{
17034 char_u *p = NULL;
17035#ifdef FEAT_SYN_HL
17036 int id;
17037 char_u *what;
17038 char_u *mode;
17039 char_u modebuf[NUMBUFLEN];
17040 int modec;
17041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 id = get_tv_number(&argvars[0]);
17043 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017046 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017048 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 modec = 0; /* replace invalid with current */
17050 }
17051 else
17052 {
17053#ifdef FEAT_GUI
17054 if (gui.in_use)
17055 modec = 'g';
17056 else
17057#endif
17058 if (t_colors > 1)
17059 modec = 'c';
17060 else
17061 modec = 't';
17062 }
17063
17064
17065 switch (TOLOWER_ASC(what[0]))
17066 {
17067 case 'b':
17068 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17069 p = highlight_color(id, what, modec);
17070 else /* bold */
17071 p = highlight_has_attr(id, HL_BOLD, modec);
17072 break;
17073
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017074 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 p = highlight_color(id, what, modec);
17076 break;
17077
17078 case 'i':
17079 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17080 p = highlight_has_attr(id, HL_INVERSE, modec);
17081 else /* italic */
17082 p = highlight_has_attr(id, HL_ITALIC, modec);
17083 break;
17084
17085 case 'n': /* name */
17086 p = get_highlight_name(NULL, id - 1);
17087 break;
17088
17089 case 'r': /* reverse */
17090 p = highlight_has_attr(id, HL_INVERSE, modec);
17091 break;
17092
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017093 case 's':
17094 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17095 p = highlight_color(id, what, modec);
17096 else /* standout */
17097 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 break;
17099
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017100 case 'u':
17101 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17102 /* underline */
17103 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17104 else
17105 /* undercurl */
17106 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 break;
17108 }
17109
17110 if (p != NULL)
17111 p = vim_strsave(p);
17112#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113 rettv->v_type = VAR_STRING;
17114 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115}
17116
17117/*
17118 * "synIDtrans(id)" function
17119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017122 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124{
17125 int id;
17126
17127#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129
17130 if (id > 0)
17131 id = syn_get_final_id(id);
17132 else
17133#endif
17134 id = 0;
17135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017136 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137}
17138
17139/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017140 * "synconcealed(lnum, col)" function
17141 */
17142 static void
17143f_synconcealed(argvars, rettv)
17144 typval_T *argvars UNUSED;
17145 typval_T *rettv;
17146{
17147#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17148 long lnum;
17149 long col;
17150 int syntax_flags = 0;
17151 int cchar;
17152 int matchid = 0;
17153 char_u str[NUMBUFLEN];
17154#endif
17155
17156 rettv->v_type = VAR_LIST;
17157 rettv->vval.v_list = NULL;
17158
17159#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17160 lnum = get_tv_lnum(argvars); /* -1 on type error */
17161 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17162
17163 vim_memset(str, NUL, sizeof(str));
17164
17165 if (rettv_list_alloc(rettv) != FAIL)
17166 {
17167 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17168 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17169 && curwin->w_p_cole > 0)
17170 {
17171 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17172 syntax_flags = get_syntax_info(&matchid);
17173
17174 /* get the conceal character */
17175 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17176 {
17177 cchar = syn_get_sub_char();
17178 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17179 cchar = lcs_conceal;
17180 if (cchar != NUL)
17181 {
17182# ifdef FEAT_MBYTE
17183 if (has_mbyte)
17184 (*mb_char2bytes)(cchar, str);
17185 else
17186# endif
17187 str[0] = cchar;
17188 }
17189 }
17190 }
17191
17192 list_append_number(rettv->vval.v_list,
17193 (syntax_flags & HL_CONCEAL) != 0);
17194 /* -1 to auto-determine strlen */
17195 list_append_string(rettv->vval.v_list, str, -1);
17196 list_append_number(rettv->vval.v_list, matchid);
17197 }
17198#endif
17199}
17200
17201/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017202 * "synstack(lnum, col)" function
17203 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017204 static void
17205f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017206 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017207 typval_T *rettv;
17208{
17209#ifdef FEAT_SYN_HL
17210 long lnum;
17211 long col;
17212 int i;
17213 int id;
17214#endif
17215
17216 rettv->v_type = VAR_LIST;
17217 rettv->vval.v_list = NULL;
17218
17219#ifdef FEAT_SYN_HL
17220 lnum = get_tv_lnum(argvars); /* -1 on type error */
17221 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17222
17223 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017224 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017225 && rettv_list_alloc(rettv) != FAIL)
17226 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017227 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017228 for (i = 0; ; ++i)
17229 {
17230 id = syn_get_stack_item(i);
17231 if (id < 0)
17232 break;
17233 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17234 break;
17235 }
17236 }
17237#endif
17238}
17239
17240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 * "system()" function
17242 */
17243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017244f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017245 typval_T *argvars;
17246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017248 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017250 char_u *infile = NULL;
17251 char_u buf[NUMBUFLEN];
17252 int err = FALSE;
17253 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017255 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017256 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017257
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017258 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017259 {
17260 /*
17261 * Write the string to a temp file, to be used for input of the shell
17262 * command.
17263 */
17264 if ((infile = vim_tempname('i')) == NULL)
17265 {
17266 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017267 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017268 }
17269
17270 fd = mch_fopen((char *)infile, WRITEBIN);
17271 if (fd == NULL)
17272 {
17273 EMSG2(_(e_notopen), infile);
17274 goto done;
17275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017276 p = get_tv_string_buf_chk(&argvars[1], buf);
17277 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017278 {
17279 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017281 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017282 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17283 err = TRUE;
17284 if (fclose(fd) != 0)
17285 err = TRUE;
17286 if (err)
17287 {
17288 EMSG(_("E677: Error writing temp file"));
17289 goto done;
17290 }
17291 }
17292
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017293 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17294 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017295
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296#ifdef USE_CR
17297 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017298 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 {
17300 char_u *s;
17301
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017302 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 {
17304 if (*s == CAR)
17305 *s = NL;
17306 }
17307 }
17308#else
17309# ifdef USE_CRNL
17310 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017311 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 {
17313 char_u *s, *d;
17314
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017315 d = res;
17316 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 {
17318 if (s[0] == CAR && s[1] == NL)
17319 ++s;
17320 *d++ = *s;
17321 }
17322 *d = NUL;
17323 }
17324# endif
17325#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017326
17327done:
17328 if (infile != NULL)
17329 {
17330 mch_remove(infile);
17331 vim_free(infile);
17332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333 rettv->v_type = VAR_STRING;
17334 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335}
17336
17337/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017338 * "tabpagebuflist()" function
17339 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017340 static void
17341f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017342 typval_T *argvars UNUSED;
17343 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017344{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017345#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017346 tabpage_T *tp;
17347 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017348
17349 if (argvars[0].v_type == VAR_UNKNOWN)
17350 wp = firstwin;
17351 else
17352 {
17353 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17354 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017355 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017356 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017357 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017358 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017359 for (; wp != NULL; wp = wp->w_next)
17360 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017361 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017362 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017363 }
17364#endif
17365}
17366
17367
17368/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017369 * "tabpagenr()" function
17370 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017371 static void
17372f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017373 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017374 typval_T *rettv;
17375{
17376 int nr = 1;
17377#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017378 char_u *arg;
17379
17380 if (argvars[0].v_type != VAR_UNKNOWN)
17381 {
17382 arg = get_tv_string_chk(&argvars[0]);
17383 nr = 0;
17384 if (arg != NULL)
17385 {
17386 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017387 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017388 else
17389 EMSG2(_(e_invexpr2), arg);
17390 }
17391 }
17392 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017393 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017394#endif
17395 rettv->vval.v_number = nr;
17396}
17397
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017398
17399#ifdef FEAT_WINDOWS
17400static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17401
17402/*
17403 * Common code for tabpagewinnr() and winnr().
17404 */
17405 static int
17406get_winnr(tp, argvar)
17407 tabpage_T *tp;
17408 typval_T *argvar;
17409{
17410 win_T *twin;
17411 int nr = 1;
17412 win_T *wp;
17413 char_u *arg;
17414
17415 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17416 if (argvar->v_type != VAR_UNKNOWN)
17417 {
17418 arg = get_tv_string_chk(argvar);
17419 if (arg == NULL)
17420 nr = 0; /* type error; errmsg already given */
17421 else if (STRCMP(arg, "$") == 0)
17422 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17423 else if (STRCMP(arg, "#") == 0)
17424 {
17425 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17426 if (twin == NULL)
17427 nr = 0;
17428 }
17429 else
17430 {
17431 EMSG2(_(e_invexpr2), arg);
17432 nr = 0;
17433 }
17434 }
17435
17436 if (nr > 0)
17437 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17438 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017439 {
17440 if (wp == NULL)
17441 {
17442 /* didn't find it in this tabpage */
17443 nr = 0;
17444 break;
17445 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017446 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017447 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017448 return nr;
17449}
17450#endif
17451
17452/*
17453 * "tabpagewinnr()" function
17454 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017455 static void
17456f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017457 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017458 typval_T *rettv;
17459{
17460 int nr = 1;
17461#ifdef FEAT_WINDOWS
17462 tabpage_T *tp;
17463
17464 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17465 if (tp == NULL)
17466 nr = 0;
17467 else
17468 nr = get_winnr(tp, &argvars[1]);
17469#endif
17470 rettv->vval.v_number = nr;
17471}
17472
17473
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017474/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017475 * "tagfiles()" function
17476 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017477 static void
17478f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017479 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017480 typval_T *rettv;
17481{
17482 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017483 tagname_T tn;
17484 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017486 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017487 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017488
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017489 for (first = TRUE; ; first = FALSE)
17490 if (get_tagfname(&tn, first, fname) == FAIL
17491 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017492 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017493 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017494}
17495
17496/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017497 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017498 */
17499 static void
17500f_taglist(argvars, rettv)
17501 typval_T *argvars;
17502 typval_T *rettv;
17503{
17504 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017505
17506 tag_pattern = get_tv_string(&argvars[0]);
17507
17508 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017509 if (*tag_pattern == NUL)
17510 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017511
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017512 if (rettv_list_alloc(rettv) == OK)
17513 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017514}
17515
17516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 * "tempname()" function
17518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017521 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523{
17524 static int x = 'A';
17525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526 rettv->v_type = VAR_STRING;
17527 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528
17529 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17530 * names. Skip 'I' and 'O', they are used for shell redirection. */
17531 do
17532 {
17533 if (x == 'Z')
17534 x = '0';
17535 else if (x == '9')
17536 x = 'A';
17537 else
17538 {
17539#ifdef EBCDIC
17540 if (x == 'I')
17541 x = 'J';
17542 else if (x == 'R')
17543 x = 'S';
17544 else
17545#endif
17546 ++x;
17547 }
17548 } while (x == 'I' || x == 'O');
17549}
17550
17551/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017552 * "test(list)" function: Just checking the walls...
17553 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017554 static void
17555f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017556 typval_T *argvars UNUSED;
17557 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017558{
17559 /* Used for unit testing. Change the code below to your liking. */
17560#if 0
17561 listitem_T *li;
17562 list_T *l;
17563 char_u *bad, *good;
17564
17565 if (argvars[0].v_type != VAR_LIST)
17566 return;
17567 l = argvars[0].vval.v_list;
17568 if (l == NULL)
17569 return;
17570 li = l->lv_first;
17571 if (li == NULL)
17572 return;
17573 bad = get_tv_string(&li->li_tv);
17574 li = li->li_next;
17575 if (li == NULL)
17576 return;
17577 good = get_tv_string(&li->li_tv);
17578 rettv->vval.v_number = test_edit_score(bad, good);
17579#endif
17580}
17581
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017582#ifdef FEAT_FLOAT
17583/*
17584 * "tan()" function
17585 */
17586 static void
17587f_tan(argvars, rettv)
17588 typval_T *argvars;
17589 typval_T *rettv;
17590{
17591 float_T f;
17592
17593 rettv->v_type = VAR_FLOAT;
17594 if (get_float_arg(argvars, &f) == OK)
17595 rettv->vval.v_float = tan(f);
17596 else
17597 rettv->vval.v_float = 0.0;
17598}
17599
17600/*
17601 * "tanh()" function
17602 */
17603 static void
17604f_tanh(argvars, rettv)
17605 typval_T *argvars;
17606 typval_T *rettv;
17607{
17608 float_T f;
17609
17610 rettv->v_type = VAR_FLOAT;
17611 if (get_float_arg(argvars, &f) == OK)
17612 rettv->vval.v_float = tanh(f);
17613 else
17614 rettv->vval.v_float = 0.0;
17615}
17616#endif
17617
Bram Moolenaard52d9742005-08-21 22:20:28 +000017618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 * "tolower(string)" function
17620 */
17621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 typval_T *argvars;
17624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625{
17626 char_u *p;
17627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628 p = vim_strsave(get_tv_string(&argvars[0]));
17629 rettv->v_type = VAR_STRING;
17630 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
17632 if (p != NULL)
17633 while (*p != NUL)
17634 {
17635#ifdef FEAT_MBYTE
17636 int l;
17637
17638 if (enc_utf8)
17639 {
17640 int c, lc;
17641
17642 c = utf_ptr2char(p);
17643 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017644 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 /* TODO: reallocate string when byte count changes. */
17646 if (utf_char2len(lc) == l)
17647 utf_char2bytes(lc, p);
17648 p += l;
17649 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017650 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 p += l; /* skip multi-byte character */
17652 else
17653#endif
17654 {
17655 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17656 ++p;
17657 }
17658 }
17659}
17660
17661/*
17662 * "toupper(string)" function
17663 */
17664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017665f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017666 typval_T *argvars;
17667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017669 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017670 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671}
17672
17673/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017674 * "tr(string, fromstr, tostr)" function
17675 */
17676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017677f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 typval_T *argvars;
17679 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017680{
17681 char_u *instr;
17682 char_u *fromstr;
17683 char_u *tostr;
17684 char_u *p;
17685#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017686 int inlen;
17687 int fromlen;
17688 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017689 int idx;
17690 char_u *cpstr;
17691 int cplen;
17692 int first = TRUE;
17693#endif
17694 char_u buf[NUMBUFLEN];
17695 char_u buf2[NUMBUFLEN];
17696 garray_T ga;
17697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017698 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017699 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17700 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017701
17702 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017703 rettv->v_type = VAR_STRING;
17704 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017705 if (fromstr == NULL || tostr == NULL)
17706 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017707 ga_init2(&ga, (int)sizeof(char), 80);
17708
17709#ifdef FEAT_MBYTE
17710 if (!has_mbyte)
17711#endif
17712 /* not multi-byte: fromstr and tostr must be the same length */
17713 if (STRLEN(fromstr) != STRLEN(tostr))
17714 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017715#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017716error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017717#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017718 EMSG2(_(e_invarg2), fromstr);
17719 ga_clear(&ga);
17720 return;
17721 }
17722
17723 /* fromstr and tostr have to contain the same number of chars */
17724 while (*instr != NUL)
17725 {
17726#ifdef FEAT_MBYTE
17727 if (has_mbyte)
17728 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017729 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017730 cpstr = instr;
17731 cplen = inlen;
17732 idx = 0;
17733 for (p = fromstr; *p != NUL; p += fromlen)
17734 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017735 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017736 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17737 {
17738 for (p = tostr; *p != NUL; p += tolen)
17739 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017740 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017741 if (idx-- == 0)
17742 {
17743 cplen = tolen;
17744 cpstr = p;
17745 break;
17746 }
17747 }
17748 if (*p == NUL) /* tostr is shorter than fromstr */
17749 goto error;
17750 break;
17751 }
17752 ++idx;
17753 }
17754
17755 if (first && cpstr == instr)
17756 {
17757 /* Check that fromstr and tostr have the same number of
17758 * (multi-byte) characters. Done only once when a character
17759 * of instr doesn't appear in fromstr. */
17760 first = FALSE;
17761 for (p = tostr; *p != NUL; p += tolen)
17762 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017763 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017764 --idx;
17765 }
17766 if (idx != 0)
17767 goto error;
17768 }
17769
17770 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017771 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017772 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017773
17774 instr += inlen;
17775 }
17776 else
17777#endif
17778 {
17779 /* When not using multi-byte chars we can do it faster. */
17780 p = vim_strchr(fromstr, *instr);
17781 if (p != NULL)
17782 ga_append(&ga, tostr[p - fromstr]);
17783 else
17784 ga_append(&ga, *instr);
17785 ++instr;
17786 }
17787 }
17788
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017789 /* add a terminating NUL */
17790 ga_grow(&ga, 1);
17791 ga_append(&ga, NUL);
17792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017793 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017794}
17795
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017796#ifdef FEAT_FLOAT
17797/*
17798 * "trunc({float})" function
17799 */
17800 static void
17801f_trunc(argvars, rettv)
17802 typval_T *argvars;
17803 typval_T *rettv;
17804{
17805 float_T f;
17806
17807 rettv->v_type = VAR_FLOAT;
17808 if (get_float_arg(argvars, &f) == OK)
17809 /* trunc() is not in C90, use floor() or ceil() instead. */
17810 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17811 else
17812 rettv->vval.v_float = 0.0;
17813}
17814#endif
17815
Bram Moolenaar8299df92004-07-10 09:47:34 +000017816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 * "type(expr)" function
17818 */
17819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017820f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017821 typval_T *argvars;
17822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017824 int n;
17825
17826 switch (argvars[0].v_type)
17827 {
17828 case VAR_NUMBER: n = 0; break;
17829 case VAR_STRING: n = 1; break;
17830 case VAR_FUNC: n = 2; break;
17831 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017832 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017833#ifdef FEAT_FLOAT
17834 case VAR_FLOAT: n = 5; break;
17835#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017836 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17837 }
17838 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839}
17840
17841/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017842 * "undofile(name)" function
17843 */
17844 static void
17845f_undofile(argvars, rettv)
17846 typval_T *argvars;
17847 typval_T *rettv;
17848{
17849 rettv->v_type = VAR_STRING;
17850#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017851 {
17852 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17853
17854 if (ffname != NULL)
17855 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17856 vim_free(ffname);
17857 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017858#else
17859 rettv->vval.v_string = NULL;
17860#endif
17861}
17862
17863/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017864 * "undotree()" function
17865 */
17866 static void
17867f_undotree(argvars, rettv)
17868 typval_T *argvars UNUSED;
17869 typval_T *rettv;
17870{
17871 if (rettv_dict_alloc(rettv) == OK)
17872 {
17873 dict_T *dict = rettv->vval.v_dict;
17874 list_T *list;
17875
Bram Moolenaar730cde92010-06-27 05:18:54 +020017876 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017877 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017878 dict_add_nr_str(dict, "save_last",
17879 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017880 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17881 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017882 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017883
17884 list = list_alloc();
17885 if (list != NULL)
17886 {
17887 u_eval_tree(curbuf->b_u_oldhead, list);
17888 dict_add_list(dict, "entries", list);
17889 }
17890 }
17891}
17892
17893/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017894 * "values(dict)" function
17895 */
17896 static void
17897f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017898 typval_T *argvars;
17899 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017900{
17901 dict_list(argvars, rettv, 1);
17902}
17903
17904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 * "virtcol(string)" function
17906 */
17907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017908f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017909 typval_T *argvars;
17910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911{
17912 colnr_T vcol = 0;
17913 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017914 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017916 fp = var2fpos(&argvars[0], FALSE, &fnum);
17917 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17918 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 {
17920 getvvcol(curwin, fp, NULL, NULL, &vcol);
17921 ++vcol;
17922 }
17923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017924 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925}
17926
17927/*
17928 * "visualmode()" function
17929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017932 typval_T *argvars UNUSED;
17933 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934{
17935#ifdef FEAT_VISUAL
17936 char_u str[2];
17937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 str[0] = curbuf->b_visual_mode_eval;
17940 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017941 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942
17943 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017944 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946#endif
17947}
17948
17949/*
17950 * "winbufnr(nr)" function
17951 */
17952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 typval_T *argvars;
17955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956{
17957 win_T *wp;
17958
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017959 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017961 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964}
17965
17966/*
17967 * "wincol()" function
17968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017970f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017971 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973{
17974 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017975 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976}
17977
17978/*
17979 * "winheight(nr)" function
17980 */
17981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017982f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017983 typval_T *argvars;
17984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985{
17986 win_T *wp;
17987
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017988 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993}
17994
17995/*
17996 * "winline()" function
17997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017999f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018000 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002{
18003 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005}
18006
18007/*
18008 * "winnr()" function
18009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018011f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018012 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014{
18015 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018016
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018018 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018020 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021}
18022
18023/*
18024 * "winrestcmd()" function
18025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018027f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030{
18031#ifdef FEAT_WINDOWS
18032 win_T *wp;
18033 int winnr = 1;
18034 garray_T ga;
18035 char_u buf[50];
18036
18037 ga_init2(&ga, (int)sizeof(char), 70);
18038 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18039 {
18040 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18041 ga_concat(&ga, buf);
18042# ifdef FEAT_VERTSPLIT
18043 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18044 ga_concat(&ga, buf);
18045# endif
18046 ++winnr;
18047 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018048 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018050 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055}
18056
18057/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018058 * "winrestview()" function
18059 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018060 static void
18061f_winrestview(argvars, rettv)
18062 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018063 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018064{
18065 dict_T *dict;
18066
18067 if (argvars[0].v_type != VAR_DICT
18068 || (dict = argvars[0].vval.v_dict) == NULL)
18069 EMSG(_(e_invarg));
18070 else
18071 {
18072 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18073 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18074#ifdef FEAT_VIRTUALEDIT
18075 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18076#endif
18077 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018078 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018079
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018080 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018081#ifdef FEAT_DIFF
18082 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18083#endif
18084 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18085 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18086
18087 check_cursor();
18088 changed_cline_bef_curs();
18089 invalidate_botline();
18090 redraw_later(VALID);
18091
18092 if (curwin->w_topline == 0)
18093 curwin->w_topline = 1;
18094 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18095 curwin->w_topline = curbuf->b_ml.ml_line_count;
18096#ifdef FEAT_DIFF
18097 check_topfill(curwin, TRUE);
18098#endif
18099 }
18100}
18101
18102/*
18103 * "winsaveview()" function
18104 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018105 static void
18106f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018107 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018108 typval_T *rettv;
18109{
18110 dict_T *dict;
18111
Bram Moolenaara800b422010-06-27 01:15:55 +020018112 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018113 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018114 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018115
18116 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18117 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18118#ifdef FEAT_VIRTUALEDIT
18119 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18120#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018121 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018122 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18123
18124 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18125#ifdef FEAT_DIFF
18126 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18127#endif
18128 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18129 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18130}
18131
18132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 * "winwidth(nr)" function
18134 */
18135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018137 typval_T *argvars;
18138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139{
18140 win_T *wp;
18141
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018142 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 else
18146#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018147 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018149 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150#endif
18151}
18152
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018154 * "writefile()" function
18155 */
18156 static void
18157f_writefile(argvars, rettv)
18158 typval_T *argvars;
18159 typval_T *rettv;
18160{
18161 int binary = FALSE;
18162 char_u *fname;
18163 FILE *fd;
18164 listitem_T *li;
18165 char_u *s;
18166 int ret = 0;
18167 int c;
18168
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018169 if (check_restricted() || check_secure())
18170 return;
18171
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018172 if (argvars[0].v_type != VAR_LIST)
18173 {
18174 EMSG2(_(e_listarg), "writefile()");
18175 return;
18176 }
18177 if (argvars[0].vval.v_list == NULL)
18178 return;
18179
18180 if (argvars[2].v_type != VAR_UNKNOWN
18181 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18182 binary = TRUE;
18183
18184 /* Always open the file in binary mode, library functions have a mind of
18185 * their own about CR-LF conversion. */
18186 fname = get_tv_string(&argvars[1]);
18187 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18188 {
18189 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18190 ret = -1;
18191 }
18192 else
18193 {
18194 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18195 li = li->li_next)
18196 {
18197 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18198 {
18199 if (*s == '\n')
18200 c = putc(NUL, fd);
18201 else
18202 c = putc(*s, fd);
18203 if (c == EOF)
18204 {
18205 ret = -1;
18206 break;
18207 }
18208 }
18209 if (!binary || li->li_next != NULL)
18210 if (putc('\n', fd) == EOF)
18211 {
18212 ret = -1;
18213 break;
18214 }
18215 if (ret < 0)
18216 {
18217 EMSG(_(e_write));
18218 break;
18219 }
18220 }
18221 fclose(fd);
18222 }
18223
18224 rettv->vval.v_number = ret;
18225}
18226
18227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018229 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 */
18231 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018232var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018233 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018234 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018235 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018237 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018239 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240
Bram Moolenaara5525202006-03-02 22:52:09 +000018241 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018242 if (varp->v_type == VAR_LIST)
18243 {
18244 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018245 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018246 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018247 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018248
18249 l = varp->vval.v_list;
18250 if (l == NULL)
18251 return NULL;
18252
18253 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018254 pos.lnum = list_find_nr(l, 0L, &error);
18255 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018256 return NULL; /* invalid line number */
18257
18258 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018259 pos.col = list_find_nr(l, 1L, &error);
18260 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018261 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018262 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018263
18264 /* We accept "$" for the column number: last column. */
18265 li = list_find(l, 1L);
18266 if (li != NULL && li->li_tv.v_type == VAR_STRING
18267 && li->li_tv.vval.v_string != NULL
18268 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18269 pos.col = len + 1;
18270
Bram Moolenaara5525202006-03-02 22:52:09 +000018271 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018272 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018273 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018274 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018275
Bram Moolenaara5525202006-03-02 22:52:09 +000018276#ifdef FEAT_VIRTUALEDIT
18277 /* Get the virtual offset. Defaults to zero. */
18278 pos.coladd = list_find_nr(l, 2L, &error);
18279 if (error)
18280 pos.coladd = 0;
18281#endif
18282
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018283 return &pos;
18284 }
18285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018286 name = get_tv_string_chk(varp);
18287 if (name == NULL)
18288 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018289 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018291#ifdef FEAT_VISUAL
18292 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18293 {
18294 if (VIsual_active)
18295 return &VIsual;
18296 return &curwin->w_cursor;
18297 }
18298#endif
18299 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018301 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18303 return NULL;
18304 return pp;
18305 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018306
18307#ifdef FEAT_VIRTUALEDIT
18308 pos.coladd = 0;
18309#endif
18310
Bram Moolenaar477933c2007-07-17 14:32:23 +000018311 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018312 {
18313 pos.col = 0;
18314 if (name[1] == '0') /* "w0": first visible line */
18315 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018316 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018317 pos.lnum = curwin->w_topline;
18318 return &pos;
18319 }
18320 else if (name[1] == '$') /* "w$": last visible line */
18321 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018322 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018323 pos.lnum = curwin->w_botline - 1;
18324 return &pos;
18325 }
18326 }
18327 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018329 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 {
18331 pos.lnum = curbuf->b_ml.ml_line_count;
18332 pos.col = 0;
18333 }
18334 else
18335 {
18336 pos.lnum = curwin->w_cursor.lnum;
18337 pos.col = (colnr_T)STRLEN(ml_get_curline());
18338 }
18339 return &pos;
18340 }
18341 return NULL;
18342}
18343
18344/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018345 * Convert list in "arg" into a position and optional file number.
18346 * When "fnump" is NULL there is no file number, only 3 items.
18347 * Note that the column is passed on as-is, the caller may want to decrement
18348 * it to use 1 for the first column.
18349 * Return FAIL when conversion is not possible, doesn't check the position for
18350 * validity.
18351 */
18352 static int
18353list2fpos(arg, posp, fnump)
18354 typval_T *arg;
18355 pos_T *posp;
18356 int *fnump;
18357{
18358 list_T *l = arg->vval.v_list;
18359 long i = 0;
18360 long n;
18361
Bram Moolenaarbde35262006-07-23 20:12:24 +000018362 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18363 * when "fnump" isn't NULL and "coladd" is optional. */
18364 if (arg->v_type != VAR_LIST
18365 || l == NULL
18366 || l->lv_len < (fnump == NULL ? 2 : 3)
18367 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018368 return FAIL;
18369
18370 if (fnump != NULL)
18371 {
18372 n = list_find_nr(l, i++, NULL); /* fnum */
18373 if (n < 0)
18374 return FAIL;
18375 if (n == 0)
18376 n = curbuf->b_fnum; /* current buffer */
18377 *fnump = n;
18378 }
18379
18380 n = list_find_nr(l, i++, NULL); /* lnum */
18381 if (n < 0)
18382 return FAIL;
18383 posp->lnum = n;
18384
18385 n = list_find_nr(l, i++, NULL); /* col */
18386 if (n < 0)
18387 return FAIL;
18388 posp->col = n;
18389
18390#ifdef FEAT_VIRTUALEDIT
18391 n = list_find_nr(l, i, NULL);
18392 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018393 posp->coladd = 0;
18394 else
18395 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018396#endif
18397
18398 return OK;
18399}
18400
18401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 * Get the length of an environment variable name.
18403 * Advance "arg" to the first character after the name.
18404 * Return 0 for error.
18405 */
18406 static int
18407get_env_len(arg)
18408 char_u **arg;
18409{
18410 char_u *p;
18411 int len;
18412
18413 for (p = *arg; vim_isIDc(*p); ++p)
18414 ;
18415 if (p == *arg) /* no name found */
18416 return 0;
18417
18418 len = (int)(p - *arg);
18419 *arg = p;
18420 return len;
18421}
18422
18423/*
18424 * Get the length of the name of a function or internal variable.
18425 * "arg" is advanced to the first non-white character after the name.
18426 * Return 0 if something is wrong.
18427 */
18428 static int
18429get_id_len(arg)
18430 char_u **arg;
18431{
18432 char_u *p;
18433 int len;
18434
18435 /* Find the end of the name. */
18436 for (p = *arg; eval_isnamec(*p); ++p)
18437 ;
18438 if (p == *arg) /* no name found */
18439 return 0;
18440
18441 len = (int)(p - *arg);
18442 *arg = skipwhite(p);
18443
18444 return len;
18445}
18446
18447/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018448 * Get the length of the name of a variable or function.
18449 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018451 * Return -1 if curly braces expansion failed.
18452 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 * If the name contains 'magic' {}'s, expand them and return the
18454 * expanded name in an allocated string via 'alias' - caller must free.
18455 */
18456 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018457get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 char_u **arg;
18459 char_u **alias;
18460 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018461 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462{
18463 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 char_u *p;
18465 char_u *expr_start;
18466 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467
18468 *alias = NULL; /* default to no alias */
18469
18470 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18471 && (*arg)[2] == (int)KE_SNR)
18472 {
18473 /* hard coded <SNR>, already translated */
18474 *arg += 3;
18475 return get_id_len(arg) + 3;
18476 }
18477 len = eval_fname_script(*arg);
18478 if (len > 0)
18479 {
18480 /* literal "<SID>", "s:" or "<SNR>" */
18481 *arg += len;
18482 }
18483
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018485 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018487 p = find_name_end(*arg, &expr_start, &expr_end,
18488 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 if (expr_start != NULL)
18490 {
18491 char_u *temp_string;
18492
18493 if (!evaluate)
18494 {
18495 len += (int)(p - *arg);
18496 *arg = skipwhite(p);
18497 return len;
18498 }
18499
18500 /*
18501 * Include any <SID> etc in the expanded string:
18502 * Thus the -len here.
18503 */
18504 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18505 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018506 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 *alias = temp_string;
18508 *arg = skipwhite(p);
18509 return (int)STRLEN(temp_string);
18510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511
18512 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018513 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 EMSG2(_(e_invexpr2), *arg);
18515
18516 return len;
18517}
18518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018519/*
18520 * Find the end of a variable or function name, taking care of magic braces.
18521 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18522 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018523 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018524 * Return a pointer to just after the name. Equal to "arg" if there is no
18525 * valid name.
18526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018528find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 char_u *arg;
18530 char_u **expr_start;
18531 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018532 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534 int mb_nest = 0;
18535 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 char_u *p;
18537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018540 *expr_start = NULL;
18541 *expr_end = NULL;
18542 }
18543
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018544 /* Quick check for valid starting character. */
18545 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18546 return arg;
18547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548 for (p = arg; *p != NUL
18549 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018550 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018551 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018552 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018553 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018555 if (*p == '\'')
18556 {
18557 /* skip over 'string' to avoid counting [ and ] inside it. */
18558 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18559 ;
18560 if (*p == NUL)
18561 break;
18562 }
18563 else if (*p == '"')
18564 {
18565 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18566 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18567 if (*p == '\\' && p[1] != NUL)
18568 ++p;
18569 if (*p == NUL)
18570 break;
18571 }
18572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 if (*p == '[')
18576 ++br_nest;
18577 else if (*p == ']')
18578 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 if (*p == '{')
18584 {
18585 mb_nest++;
18586 if (expr_start != NULL && *expr_start == NULL)
18587 *expr_start = p;
18588 }
18589 else if (*p == '}')
18590 {
18591 mb_nest--;
18592 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18593 *expr_end = p;
18594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 }
18597
18598 return p;
18599}
18600
18601/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018602 * Expands out the 'magic' {}'s in a variable/function name.
18603 * Note that this can call itself recursively, to deal with
18604 * constructs like foo{bar}{baz}{bam}
18605 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18606 * "in_start" ^
18607 * "expr_start" ^
18608 * "expr_end" ^
18609 * "in_end" ^
18610 *
18611 * Returns a new allocated string, which the caller must free.
18612 * Returns NULL for failure.
18613 */
18614 static char_u *
18615make_expanded_name(in_start, expr_start, expr_end, in_end)
18616 char_u *in_start;
18617 char_u *expr_start;
18618 char_u *expr_end;
18619 char_u *in_end;
18620{
18621 char_u c1;
18622 char_u *retval = NULL;
18623 char_u *temp_result;
18624 char_u *nextcmd = NULL;
18625
18626 if (expr_end == NULL || in_end == NULL)
18627 return NULL;
18628 *expr_start = NUL;
18629 *expr_end = NUL;
18630 c1 = *in_end;
18631 *in_end = NUL;
18632
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018633 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018634 if (temp_result != NULL && nextcmd == NULL)
18635 {
18636 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18637 + (in_end - expr_end) + 1));
18638 if (retval != NULL)
18639 {
18640 STRCPY(retval, in_start);
18641 STRCAT(retval, temp_result);
18642 STRCAT(retval, expr_end + 1);
18643 }
18644 }
18645 vim_free(temp_result);
18646
18647 *in_end = c1; /* put char back for error messages */
18648 *expr_start = '{';
18649 *expr_end = '}';
18650
18651 if (retval != NULL)
18652 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018653 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018654 if (expr_start != NULL)
18655 {
18656 /* Further expansion! */
18657 temp_result = make_expanded_name(retval, expr_start,
18658 expr_end, temp_result);
18659 vim_free(retval);
18660 retval = temp_result;
18661 }
18662 }
18663
18664 return retval;
18665}
18666
18667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018669 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 */
18671 static int
18672eval_isnamec(c)
18673 int c;
18674{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018675 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18676}
18677
18678/*
18679 * Return TRUE if character "c" can be used as the first character in a
18680 * variable or function name (excluding '{' and '}').
18681 */
18682 static int
18683eval_isnamec1(c)
18684 int c;
18685{
18686 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687}
18688
18689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 * Set number v: variable to "val".
18691 */
18692 void
18693set_vim_var_nr(idx, val)
18694 int idx;
18695 long val;
18696{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018697 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698}
18699
18700/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018701 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 */
18703 long
18704get_vim_var_nr(idx)
18705 int idx;
18706{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018707 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708}
18709
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018710/*
18711 * Get string v: variable value. Uses a static buffer, can only be used once.
18712 */
18713 char_u *
18714get_vim_var_str(idx)
18715 int idx;
18716{
18717 return get_tv_string(&vimvars[idx].vv_tv);
18718}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018719
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018721 * Get List v: variable value. Caller must take care of reference count when
18722 * needed.
18723 */
18724 list_T *
18725get_vim_var_list(idx)
18726 int idx;
18727{
18728 return vimvars[idx].vv_list;
18729}
18730
18731/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018732 * Set v:char to character "c".
18733 */
18734 void
18735set_vim_var_char(c)
18736 int c;
18737{
18738#ifdef FEAT_MBYTE
18739 char_u buf[MB_MAXBYTES];
18740#else
18741 char_u buf[2];
18742#endif
18743
18744#ifdef FEAT_MBYTE
18745 if (has_mbyte)
18746 buf[(*mb_char2bytes)(c, buf)] = NUL;
18747 else
18748#endif
18749 {
18750 buf[0] = c;
18751 buf[1] = NUL;
18752 }
18753 set_vim_var_string(VV_CHAR, buf, -1);
18754}
18755
18756/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018757 * Set v:count to "count" and v:count1 to "count1".
18758 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 */
18760 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018761set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 long count;
18763 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018764 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018766 if (set_prevcount)
18767 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018768 vimvars[VV_COUNT].vv_nr = count;
18769 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770}
18771
18772/*
18773 * Set string v: variable to a copy of "val".
18774 */
18775 void
18776set_vim_var_string(idx, val, len)
18777 int idx;
18778 char_u *val;
18779 int len; /* length of "val" to use or -1 (whole string) */
18780{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018781 /* Need to do this (at least) once, since we can't initialize a union.
18782 * Will always be invoked when "v:progname" is set. */
18783 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18784
Bram Moolenaare9a41262005-01-15 22:18:47 +000018785 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018787 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018789 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018791 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792}
18793
18794/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018795 * Set List v: variable to "val".
18796 */
18797 void
18798set_vim_var_list(idx, val)
18799 int idx;
18800 list_T *val;
18801{
18802 list_unref(vimvars[idx].vv_list);
18803 vimvars[idx].vv_list = val;
18804 if (val != NULL)
18805 ++val->lv_refcount;
18806}
18807
18808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 * Set v:register if needed.
18810 */
18811 void
18812set_reg_var(c)
18813 int c;
18814{
18815 char_u regname;
18816
18817 if (c == 0 || c == ' ')
18818 regname = '"';
18819 else
18820 regname = c;
18821 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018822 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 set_vim_var_string(VV_REG, &regname, 1);
18824}
18825
18826/*
18827 * Get or set v:exception. If "oldval" == NULL, return the current value.
18828 * Otherwise, restore the value to "oldval" and return NULL.
18829 * Must always be called in pairs to save and restore v:exception! Does not
18830 * take care of memory allocations.
18831 */
18832 char_u *
18833v_exception(oldval)
18834 char_u *oldval;
18835{
18836 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018837 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838
Bram Moolenaare9a41262005-01-15 22:18:47 +000018839 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 return NULL;
18841}
18842
18843/*
18844 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18845 * Otherwise, restore the value to "oldval" and return NULL.
18846 * Must always be called in pairs to save and restore v:throwpoint! Does not
18847 * take care of memory allocations.
18848 */
18849 char_u *
18850v_throwpoint(oldval)
18851 char_u *oldval;
18852{
18853 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018854 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855
Bram Moolenaare9a41262005-01-15 22:18:47 +000018856 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 return NULL;
18858}
18859
18860#if defined(FEAT_AUTOCMD) || defined(PROTO)
18861/*
18862 * Set v:cmdarg.
18863 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18864 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18865 * Must always be called in pairs!
18866 */
18867 char_u *
18868set_cmdarg(eap, oldarg)
18869 exarg_T *eap;
18870 char_u *oldarg;
18871{
18872 char_u *oldval;
18873 char_u *newval;
18874 unsigned len;
18875
Bram Moolenaare9a41262005-01-15 22:18:47 +000018876 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018877 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018879 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018880 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018881 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 }
18883
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018884 if (eap->force_bin == FORCE_BIN)
18885 len = 6;
18886 else if (eap->force_bin == FORCE_NOBIN)
18887 len = 8;
18888 else
18889 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018890
18891 if (eap->read_edit)
18892 len += 7;
18893
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018894 if (eap->force_ff != 0)
18895 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18896# ifdef FEAT_MBYTE
18897 if (eap->force_enc != 0)
18898 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018899 if (eap->bad_char != 0)
18900 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018901# endif
18902
18903 newval = alloc(len + 1);
18904 if (newval == NULL)
18905 return NULL;
18906
18907 if (eap->force_bin == FORCE_BIN)
18908 sprintf((char *)newval, " ++bin");
18909 else if (eap->force_bin == FORCE_NOBIN)
18910 sprintf((char *)newval, " ++nobin");
18911 else
18912 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018913
18914 if (eap->read_edit)
18915 STRCAT(newval, " ++edit");
18916
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018917 if (eap->force_ff != 0)
18918 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18919 eap->cmd + eap->force_ff);
18920# ifdef FEAT_MBYTE
18921 if (eap->force_enc != 0)
18922 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18923 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018924 if (eap->bad_char == BAD_KEEP)
18925 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18926 else if (eap->bad_char == BAD_DROP)
18927 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18928 else if (eap->bad_char != 0)
18929 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018930# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018931 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018932 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933}
18934#endif
18935
18936/*
18937 * Get the value of internal variable "name".
18938 * Return OK or FAIL.
18939 */
18940 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018941get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 char_u *name;
18943 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018945 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946{
18947 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 typval_T *tv = NULL;
18949 typval_T atv;
18950 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952
18953 /* truncate the name, so that we can use strcmp() */
18954 cc = name[len];
18955 name[len] = NUL;
18956
18957 /*
18958 * Check for "b:changedtick".
18959 */
18960 if (STRCMP(name, "b:changedtick") == 0)
18961 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018962 atv.v_type = VAR_NUMBER;
18963 atv.vval.v_number = curbuf->b_changedtick;
18964 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 }
18966
18967 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 * Check for user-defined variables.
18969 */
18970 else
18971 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018972 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018974 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 }
18976
Bram Moolenaare9a41262005-01-15 22:18:47 +000018977 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018979 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 ret = FAIL;
18982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018984 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985
18986 name[len] = cc;
18987
18988 return ret;
18989}
18990
18991/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018992 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18993 * Also handle function call with Funcref variable: func(expr)
18994 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18995 */
18996 static int
18997handle_subscript(arg, rettv, evaluate, verbose)
18998 char_u **arg;
18999 typval_T *rettv;
19000 int evaluate; /* do more than finding the end */
19001 int verbose; /* give error messages */
19002{
19003 int ret = OK;
19004 dict_T *selfdict = NULL;
19005 char_u *s;
19006 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019007 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019008
19009 while (ret == OK
19010 && (**arg == '['
19011 || (**arg == '.' && rettv->v_type == VAR_DICT)
19012 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19013 && !vim_iswhite(*(*arg - 1)))
19014 {
19015 if (**arg == '(')
19016 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019017 /* need to copy the funcref so that we can clear rettv */
19018 functv = *rettv;
19019 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019020
19021 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019022 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019023 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019024 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19025 &len, evaluate, selfdict);
19026
19027 /* Clear the funcref afterwards, so that deleting it while
19028 * evaluating the arguments is possible (see test55). */
19029 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019030
19031 /* Stop the expression evaluation when immediately aborting on
19032 * error, or when an interrupt occurred or an exception was thrown
19033 * but not caught. */
19034 if (aborting())
19035 {
19036 if (ret == OK)
19037 clear_tv(rettv);
19038 ret = FAIL;
19039 }
19040 dict_unref(selfdict);
19041 selfdict = NULL;
19042 }
19043 else /* **arg == '[' || **arg == '.' */
19044 {
19045 dict_unref(selfdict);
19046 if (rettv->v_type == VAR_DICT)
19047 {
19048 selfdict = rettv->vval.v_dict;
19049 if (selfdict != NULL)
19050 ++selfdict->dv_refcount;
19051 }
19052 else
19053 selfdict = NULL;
19054 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19055 {
19056 clear_tv(rettv);
19057 ret = FAIL;
19058 }
19059 }
19060 }
19061 dict_unref(selfdict);
19062 return ret;
19063}
19064
19065/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019066 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019067 * value).
19068 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019069 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019071{
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019073}
19074
19075/*
19076 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 * The string "s" must have been allocated, it is consumed.
19078 * Return NULL for out of memory, the variable otherwise.
19079 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019080 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 char_u *s;
19083{
Bram Moolenaar33570922005-01-25 22:26:29 +000019084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019086 rettv = alloc_tv();
19087 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019089 rettv->v_type = VAR_STRING;
19090 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 }
19092 else
19093 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095}
19096
19097/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019098 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019100 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019102 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103{
19104 if (varp != NULL)
19105 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019106 switch (varp->v_type)
19107 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019108 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019109 func_unref(varp->vval.v_string);
19110 /*FALLTHROUGH*/
19111 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019112 vim_free(varp->vval.v_string);
19113 break;
19114 case VAR_LIST:
19115 list_unref(varp->vval.v_list);
19116 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019117 case VAR_DICT:
19118 dict_unref(varp->vval.v_dict);
19119 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019120 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019121#ifdef FEAT_FLOAT
19122 case VAR_FLOAT:
19123#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019124 case VAR_UNKNOWN:
19125 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019126 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019127 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019128 break;
19129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 vim_free(varp);
19131 }
19132}
19133
19134/*
19135 * Free the memory for a variable value and set the value to NULL or 0.
19136 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019137 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019138clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019139 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140{
19141 if (varp != NULL)
19142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019145 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019146 func_unref(varp->vval.v_string);
19147 /*FALLTHROUGH*/
19148 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019149 vim_free(varp->vval.v_string);
19150 varp->vval.v_string = NULL;
19151 break;
19152 case VAR_LIST:
19153 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019154 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019155 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019156 case VAR_DICT:
19157 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019158 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019159 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019161 varp->vval.v_number = 0;
19162 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019163#ifdef FEAT_FLOAT
19164 case VAR_FLOAT:
19165 varp->vval.v_float = 0.0;
19166 break;
19167#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 case VAR_UNKNOWN:
19169 break;
19170 default:
19171 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019173 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 }
19175}
19176
19177/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 * Set the value of a variable to NULL without freeing items.
19179 */
19180 static void
19181init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183{
19184 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019185 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186}
19187
19188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 * Get the number value of a variable.
19190 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019191 * For incompatible types, return 0.
19192 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19193 * caller of incompatible types: it sets *denote to TRUE if "denote"
19194 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 */
19196 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019200 int error = FALSE;
19201
19202 return get_tv_number_chk(varp, &error); /* return 0L on error */
19203}
19204
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019205 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019206get_tv_number_chk(varp, denote)
19207 typval_T *varp;
19208 int *denote;
19209{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019210 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019212 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019214 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019215 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019216#ifdef FEAT_FLOAT
19217 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019218 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019219 break;
19220#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019222 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 break;
19224 case VAR_STRING:
19225 if (varp->vval.v_string != NULL)
19226 vim_str2nr(varp->vval.v_string, NULL, NULL,
19227 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019228 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019229 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019230 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019231 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019232 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019233 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019234 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019235 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019236 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019239 if (denote == NULL) /* useful for values that must be unsigned */
19240 n = -1;
19241 else
19242 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019243 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244}
19245
19246/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019247 * Get the lnum from the first argument.
19248 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019249 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 */
19251 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019253 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254{
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 linenr_T lnum;
19257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019258 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 if (lnum == 0) /* no valid number, try using line() */
19260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 rettv.v_type = VAR_NUMBER;
19262 f_line(argvars, &rettv);
19263 lnum = rettv.vval.v_number;
19264 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 }
19266 return lnum;
19267}
19268
19269/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019270 * Get the lnum from the first argument.
19271 * Also accepts "$", then "buf" is used.
19272 * Returns 0 on error.
19273 */
19274 static linenr_T
19275get_tv_lnum_buf(argvars, buf)
19276 typval_T *argvars;
19277 buf_T *buf;
19278{
19279 if (argvars[0].v_type == VAR_STRING
19280 && argvars[0].vval.v_string != NULL
19281 && argvars[0].vval.v_string[0] == '$'
19282 && buf != NULL)
19283 return buf->b_ml.ml_line_count;
19284 return get_tv_number_chk(&argvars[0], NULL);
19285}
19286
19287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 * Get the string value of a variable.
19289 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019290 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19291 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 * If the String variable has never been set, return an empty string.
19293 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019294 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19295 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 */
19297 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019298get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300{
19301 static char_u mybuf[NUMBUFLEN];
19302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019303 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304}
19305
19306 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019309 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019311 char_u *res = get_tv_string_buf_chk(varp, buf);
19312
19313 return res != NULL ? res : (char_u *)"";
19314}
19315
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019316 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019317get_tv_string_chk(varp)
19318 typval_T *varp;
19319{
19320 static char_u mybuf[NUMBUFLEN];
19321
19322 return get_tv_string_buf_chk(varp, mybuf);
19323}
19324
19325 static char_u *
19326get_tv_string_buf_chk(varp, buf)
19327 typval_T *varp;
19328 char_u *buf;
19329{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019330 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019332 case VAR_NUMBER:
19333 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19334 return buf;
19335 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019336 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019337 break;
19338 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019339 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019340 break;
19341 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019342 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019343 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019344#ifdef FEAT_FLOAT
19345 case VAR_FLOAT:
19346 EMSG(_("E806: using Float as a String"));
19347 break;
19348#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019349 case VAR_STRING:
19350 if (varp->vval.v_string != NULL)
19351 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019353 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019354 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019357 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358}
19359
19360/*
19361 * Find variable "name" in the list of variables.
19362 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019363 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019364 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019365 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019368find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019373 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374
Bram Moolenaara7043832005-01-21 11:56:39 +000019375 ht = find_var_ht(name, &varname);
19376 if (htp != NULL)
19377 *htp = ht;
19378 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019380 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381}
19382
19383/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019384 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019385 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019387 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019388find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019390 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019391 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019392{
Bram Moolenaar33570922005-01-25 22:26:29 +000019393 hashitem_T *hi;
19394
19395 if (*varname == NUL)
19396 {
19397 /* Must be something like "s:", otherwise "ht" would be NULL. */
19398 switch (varname[-2])
19399 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019400 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019401 case 'g': return &globvars_var;
19402 case 'v': return &vimvars_var;
19403 case 'b': return &curbuf->b_bufvar;
19404 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019405#ifdef FEAT_WINDOWS
19406 case 't': return &curtab->tp_winvar;
19407#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019408 case 'l': return current_funccal == NULL
19409 ? NULL : &current_funccal->l_vars_var;
19410 case 'a': return current_funccal == NULL
19411 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019412 }
19413 return NULL;
19414 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019415
19416 hi = hash_find(ht, varname);
19417 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019418 {
19419 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019420 * worked find the variable again. Don't auto-load a script if it was
19421 * loaded already, otherwise it would be loaded every time when
19422 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019423 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019424 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019425 hi = hash_find(ht, varname);
19426 if (HASHITEM_EMPTY(hi))
19427 return NULL;
19428 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019430}
19431
19432/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019433 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019434 * Set "varname" to the start of name without ':'.
19435 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019436 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019437find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 char_u *name;
19439 char_u **varname;
19440{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019441 hashitem_T *hi;
19442
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 if (name[1] != ':')
19444 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019445 /* The name must not start with a colon or #. */
19446 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 return NULL;
19448 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019449
19450 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019451 hi = hash_find(&compat_hashtab, name);
19452 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019453 return &compat_hashtab;
19454
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019456 return &globvarht; /* global variable */
19457 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 }
19459 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019460 if (*name == 'g') /* global variable */
19461 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019462 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19463 */
19464 if (vim_strchr(name + 2, ':') != NULL
19465 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019466 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019468 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019470 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019471#ifdef FEAT_WINDOWS
19472 if (*name == 't') /* tab page variable */
19473 return &curtab->tp_vars.dv_hashtab;
19474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019475 if (*name == 'v') /* v: variable */
19476 return &vimvarht;
19477 if (*name == 'a' && current_funccal != NULL) /* function argument */
19478 return &current_funccal->l_avars.dv_hashtab;
19479 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19480 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 if (*name == 's' /* script variable */
19482 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19483 return &SCRIPT_VARS(current_SID);
19484 return NULL;
19485}
19486
19487/*
19488 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019489 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 * Returns NULL when it doesn't exist.
19491 */
19492 char_u *
19493get_var_value(name)
19494 char_u *name;
19495{
Bram Moolenaar33570922005-01-25 22:26:29 +000019496 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497
Bram Moolenaara7043832005-01-21 11:56:39 +000019498 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 if (v == NULL)
19500 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019501 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502}
19503
19504/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019505 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 * sourcing this script and when executing functions defined in the script.
19507 */
19508 void
19509new_script_vars(id)
19510 scid_T id;
19511{
Bram Moolenaara7043832005-01-21 11:56:39 +000019512 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019513 hashtab_T *ht;
19514 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019515
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19517 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 /* Re-allocating ga_data means that an ht_array pointing to
19519 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019521 for (i = 1; i <= ga_scripts.ga_len; ++i)
19522 {
19523 ht = &SCRIPT_VARS(i);
19524 if (ht->ht_mask == HT_INIT_SIZE - 1)
19525 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019526 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019528 }
19529
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 while (ga_scripts.ga_len < id)
19531 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019532 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019533 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 }
19537 }
19538}
19539
19540/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019541 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19542 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 */
19544 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019545init_var_dict(dict, dict_var)
19546 dict_T *dict;
19547 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548{
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019550 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019551 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019552 dict_var->di_tv.vval.v_dict = dict;
19553 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019554 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19556 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557}
19558
19559/*
19560 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 * Frees all allocated variables and the value they contain.
19562 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 */
19564 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019565vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019566 hashtab_T *ht;
19567{
19568 vars_clear_ext(ht, TRUE);
19569}
19570
19571/*
19572 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19573 */
19574 static void
19575vars_clear_ext(ht, free_val)
19576 hashtab_T *ht;
19577 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578{
Bram Moolenaara7043832005-01-21 11:56:39 +000019579 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 hashitem_T *hi;
19581 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019584 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019585 for (hi = ht->ht_array; todo > 0; ++hi)
19586 {
19587 if (!HASHITEM_EMPTY(hi))
19588 {
19589 --todo;
19590
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019592 * ht_array might change then. hash_clear() takes care of it
19593 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019594 v = HI2DI(hi);
19595 if (free_val)
19596 clear_tv(&v->di_tv);
19597 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19598 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019599 }
19600 }
19601 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019602 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603}
19604
Bram Moolenaara7043832005-01-21 11:56:39 +000019605/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 * Delete a variable from hashtab "ht" at item "hi".
19607 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019610delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 hashtab_T *ht;
19612 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613{
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019615
19616 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 clear_tv(&di->di_tv);
19618 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619}
19620
19621/*
19622 * List the value of one internal variable.
19623 */
19624 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019625list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019628 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019630 char_u *tofree;
19631 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019632 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019633
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019634 current_copyID += COPYID_INC;
19635 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019637 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019638 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639}
19640
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019642list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 char_u *prefix;
19644 char_u *name;
19645 int type;
19646 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019647 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648{
Bram Moolenaar31859182007-08-14 20:41:13 +000019649 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19650 msg_start();
19651 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 if (name != NULL) /* "a:" vars don't have a name stored */
19653 msg_puts(name);
19654 msg_putchar(' ');
19655 msg_advance(22);
19656 if (type == VAR_NUMBER)
19657 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019658 else if (type == VAR_FUNC)
19659 msg_putchar('*');
19660 else if (type == VAR_LIST)
19661 {
19662 msg_putchar('[');
19663 if (*string == '[')
19664 ++string;
19665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019666 else if (type == VAR_DICT)
19667 {
19668 msg_putchar('{');
19669 if (*string == '{')
19670 ++string;
19671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 else
19673 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019674
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019676
19677 if (type == VAR_FUNC)
19678 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019679 if (*first)
19680 {
19681 msg_clr_eos();
19682 *first = FALSE;
19683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684}
19685
19686/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019687 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 * If the variable already exists, the value is updated.
19689 * Otherwise the variable is created.
19690 */
19691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696{
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019699 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019700 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019702 ht = find_var_ht(name, &varname);
19703 if (ht == NULL || *varname == NUL)
19704 {
19705 EMSG2(_(e_illvar), name);
19706 return;
19707 }
19708 v = find_var_in_ht(ht, varname, TRUE);
19709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019710 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019711 {
19712 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19713 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19714 ? name[2] : name[0]))
19715 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019716 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019717 return;
19718 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019719 /* Don't allow hiding a function. When "v" is not NULL we migth be
19720 * assigning another function to the same var, the type is checked
19721 * below. */
19722 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019724 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019725 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019726 return;
19727 }
19728 }
19729
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019733 if (var_check_ro(v->di_flags, name)
19734 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 return;
19736 if (v->di_tv.v_type != tv->v_type
19737 && !((v->di_tv.v_type == VAR_STRING
19738 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019740 || tv->v_type == VAR_NUMBER))
19741#ifdef FEAT_FLOAT
19742 && !((v->di_tv.v_type == VAR_NUMBER
19743 || v->di_tv.v_type == VAR_FLOAT)
19744 && (tv->v_type == VAR_NUMBER
19745 || tv->v_type == VAR_FLOAT))
19746#endif
19747 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019748 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019749 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750 return;
19751 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019752
19753 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019754 * Handle setting internal v: variables separately: we don't change
19755 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 */
19757 if (ht == &vimvarht)
19758 {
19759 if (v->di_tv.v_type == VAR_STRING)
19760 {
19761 vim_free(v->di_tv.vval.v_string);
19762 if (copy || tv->v_type != VAR_STRING)
19763 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19764 else
19765 {
19766 /* Take over the string to avoid an extra alloc/free. */
19767 v->di_tv.vval.v_string = tv->vval.v_string;
19768 tv->vval.v_string = NULL;
19769 }
19770 }
19771 else if (v->di_tv.v_type != VAR_NUMBER)
19772 EMSG2(_(e_intern2), "set_var()");
19773 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019774 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019775 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019776 if (STRCMP(varname, "searchforward") == 0)
19777 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19778 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019779 return;
19780 }
19781
19782 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 }
19784 else /* add a new variable */
19785 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019786 /* Can't add "v:" variable. */
19787 if (ht == &vimvarht)
19788 {
19789 EMSG2(_(e_illvar), name);
19790 return;
19791 }
19792
Bram Moolenaar92124a32005-06-17 22:03:40 +000019793 /* Make sure the variable name is valid. */
19794 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019795 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19796 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019797 {
19798 EMSG2(_(e_illvar), varname);
19799 return;
19800 }
19801
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019802 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19803 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019804 if (v == NULL)
19805 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019807 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019809 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 return;
19811 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019812 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019814
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019815 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019817 else
19818 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019819 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019820 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823}
19824
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019825/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019826 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 * Also give an error message.
19828 */
19829 static int
19830var_check_ro(flags, name)
19831 int flags;
19832 char_u *name;
19833{
19834 if (flags & DI_FLAGS_RO)
19835 {
19836 EMSG2(_(e_readonlyvar), name);
19837 return TRUE;
19838 }
19839 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19840 {
19841 EMSG2(_(e_readonlysbx), name);
19842 return TRUE;
19843 }
19844 return FALSE;
19845}
19846
19847/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019848 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19849 * Also give an error message.
19850 */
19851 static int
19852var_check_fixed(flags, name)
19853 int flags;
19854 char_u *name;
19855{
19856 if (flags & DI_FLAGS_FIX)
19857 {
19858 EMSG2(_("E795: Cannot delete variable %s"), name);
19859 return TRUE;
19860 }
19861 return FALSE;
19862}
19863
19864/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019865 * Return TRUE if typeval "tv" is set to be locked (immutable).
19866 * Also give an error message, using "name".
19867 */
19868 static int
19869tv_check_lock(lock, name)
19870 int lock;
19871 char_u *name;
19872{
19873 if (lock & VAR_LOCKED)
19874 {
19875 EMSG2(_("E741: Value is locked: %s"),
19876 name == NULL ? (char_u *)_("Unknown") : name);
19877 return TRUE;
19878 }
19879 if (lock & VAR_FIXED)
19880 {
19881 EMSG2(_("E742: Cannot change value of %s"),
19882 name == NULL ? (char_u *)_("Unknown") : name);
19883 return TRUE;
19884 }
19885 return FALSE;
19886}
19887
19888/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019889 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019891 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019892 * It is OK for "from" and "to" to point to the same item. This is used to
19893 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019894 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019895 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019896copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 typval_T *from;
19898 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019900 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019901 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019902 switch (from->v_type)
19903 {
19904 case VAR_NUMBER:
19905 to->vval.v_number = from->vval.v_number;
19906 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019907#ifdef FEAT_FLOAT
19908 case VAR_FLOAT:
19909 to->vval.v_float = from->vval.v_float;
19910 break;
19911#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019912 case VAR_STRING:
19913 case VAR_FUNC:
19914 if (from->vval.v_string == NULL)
19915 to->vval.v_string = NULL;
19916 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019917 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019918 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019919 if (from->v_type == VAR_FUNC)
19920 func_ref(to->vval.v_string);
19921 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019922 break;
19923 case VAR_LIST:
19924 if (from->vval.v_list == NULL)
19925 to->vval.v_list = NULL;
19926 else
19927 {
19928 to->vval.v_list = from->vval.v_list;
19929 ++to->vval.v_list->lv_refcount;
19930 }
19931 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019932 case VAR_DICT:
19933 if (from->vval.v_dict == NULL)
19934 to->vval.v_dict = NULL;
19935 else
19936 {
19937 to->vval.v_dict = from->vval.v_dict;
19938 ++to->vval.v_dict->dv_refcount;
19939 }
19940 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019941 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019943 break;
19944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945}
19946
19947/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019948 * Make a copy of an item.
19949 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019950 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19951 * reference to an already copied list/dict can be used.
19952 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019953 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019954 static int
19955item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 typval_T *from;
19957 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019958 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019959 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019960{
19961 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019962 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019963
Bram Moolenaar33570922005-01-25 22:26:29 +000019964 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019965 {
19966 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019967 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019968 }
19969 ++recurse;
19970
19971 switch (from->v_type)
19972 {
19973 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019974#ifdef FEAT_FLOAT
19975 case VAR_FLOAT:
19976#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019977 case VAR_STRING:
19978 case VAR_FUNC:
19979 copy_tv(from, to);
19980 break;
19981 case VAR_LIST:
19982 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019983 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019984 if (from->vval.v_list == NULL)
19985 to->vval.v_list = NULL;
19986 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19987 {
19988 /* use the copy made earlier */
19989 to->vval.v_list = from->vval.v_list->lv_copylist;
19990 ++to->vval.v_list->lv_refcount;
19991 }
19992 else
19993 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19994 if (to->vval.v_list == NULL)
19995 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019996 break;
19997 case VAR_DICT:
19998 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019999 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020000 if (from->vval.v_dict == NULL)
20001 to->vval.v_dict = NULL;
20002 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20003 {
20004 /* use the copy made earlier */
20005 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20006 ++to->vval.v_dict->dv_refcount;
20007 }
20008 else
20009 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20010 if (to->vval.v_dict == NULL)
20011 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020012 break;
20013 default:
20014 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020015 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020016 }
20017 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020018 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020019}
20020
20021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 * ":echo expr1 ..." print each argument separated with a space, add a
20023 * newline at the end.
20024 * ":echon expr1 ..." print each argument plain.
20025 */
20026 void
20027ex_echo(eap)
20028 exarg_T *eap;
20029{
20030 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020032 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 char_u *p;
20034 int needclr = TRUE;
20035 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020036 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037
20038 if (eap->skip)
20039 ++emsg_skip;
20040 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20041 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020042 /* If eval1() causes an error message the text from the command may
20043 * still need to be cleared. E.g., "echo 22,44". */
20044 need_clr_eos = needclr;
20045
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 {
20049 /*
20050 * Report the invalid expression unless the expression evaluation
20051 * has been cancelled due to an aborting error, an interrupt, or an
20052 * exception.
20053 */
20054 if (!aborting())
20055 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020056 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 break;
20058 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020059 need_clr_eos = FALSE;
20060
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 if (!eap->skip)
20062 {
20063 if (atstart)
20064 {
20065 atstart = FALSE;
20066 /* Call msg_start() after eval1(), evaluating the expression
20067 * may cause a message to appear. */
20068 if (eap->cmdidx == CMD_echo)
20069 msg_start();
20070 }
20071 else if (eap->cmdidx == CMD_echo)
20072 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020073 current_copyID += COPYID_INC;
20074 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020075 if (p != NULL)
20076 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020078 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020080 if (*p != TAB && needclr)
20081 {
20082 /* remove any text still there from the command */
20083 msg_clr_eos();
20084 needclr = FALSE;
20085 }
20086 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 }
20088 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020089 {
20090#ifdef FEAT_MBYTE
20091 if (has_mbyte)
20092 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020093 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020094
20095 (void)msg_outtrans_len_attr(p, i, echo_attr);
20096 p += i - 1;
20097 }
20098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020100 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020103 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 arg = skipwhite(arg);
20107 }
20108 eap->nextcmd = check_nextcmd(arg);
20109
20110 if (eap->skip)
20111 --emsg_skip;
20112 else
20113 {
20114 /* remove text that may still be there from the command */
20115 if (needclr)
20116 msg_clr_eos();
20117 if (eap->cmdidx == CMD_echo)
20118 msg_end();
20119 }
20120}
20121
20122/*
20123 * ":echohl {name}".
20124 */
20125 void
20126ex_echohl(eap)
20127 exarg_T *eap;
20128{
20129 int id;
20130
20131 id = syn_name2id(eap->arg);
20132 if (id == 0)
20133 echo_attr = 0;
20134 else
20135 echo_attr = syn_id2attr(id);
20136}
20137
20138/*
20139 * ":execute expr1 ..." execute the result of an expression.
20140 * ":echomsg expr1 ..." Print a message
20141 * ":echoerr expr1 ..." Print an error
20142 * Each gets spaces around each argument and a newline at the end for
20143 * echo commands
20144 */
20145 void
20146ex_execute(eap)
20147 exarg_T *eap;
20148{
20149 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 int ret = OK;
20152 char_u *p;
20153 garray_T ga;
20154 int len;
20155 int save_did_emsg;
20156
20157 ga_init2(&ga, 1, 80);
20158
20159 if (eap->skip)
20160 ++emsg_skip;
20161 while (*arg != NUL && *arg != '|' && *arg != '\n')
20162 {
20163 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 {
20166 /*
20167 * Report the invalid expression unless the expression evaluation
20168 * has been cancelled due to an aborting error, an interrupt, or an
20169 * exception.
20170 */
20171 if (!aborting())
20172 EMSG2(_(e_invexpr2), p);
20173 ret = FAIL;
20174 break;
20175 }
20176
20177 if (!eap->skip)
20178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 len = (int)STRLEN(p);
20181 if (ga_grow(&ga, len + 2) == FAIL)
20182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020183 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 ret = FAIL;
20185 break;
20186 }
20187 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 ga.ga_len += len;
20191 }
20192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 arg = skipwhite(arg);
20195 }
20196
20197 if (ret != FAIL && ga.ga_data != NULL)
20198 {
20199 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020202 out_flush();
20203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 else if (eap->cmdidx == CMD_echoerr)
20205 {
20206 /* We don't want to abort following commands, restore did_emsg. */
20207 save_did_emsg = did_emsg;
20208 EMSG((char_u *)ga.ga_data);
20209 if (!force_abort)
20210 did_emsg = save_did_emsg;
20211 }
20212 else if (eap->cmdidx == CMD_execute)
20213 do_cmdline((char_u *)ga.ga_data,
20214 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20215 }
20216
20217 ga_clear(&ga);
20218
20219 if (eap->skip)
20220 --emsg_skip;
20221
20222 eap->nextcmd = check_nextcmd(arg);
20223}
20224
20225/*
20226 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20227 * "arg" points to the "&" or '+' when called, to "option" when returning.
20228 * Returns NULL when no option name found. Otherwise pointer to the char
20229 * after the option name.
20230 */
20231 static char_u *
20232find_option_end(arg, opt_flags)
20233 char_u **arg;
20234 int *opt_flags;
20235{
20236 char_u *p = *arg;
20237
20238 ++p;
20239 if (*p == 'g' && p[1] == ':')
20240 {
20241 *opt_flags = OPT_GLOBAL;
20242 p += 2;
20243 }
20244 else if (*p == 'l' && p[1] == ':')
20245 {
20246 *opt_flags = OPT_LOCAL;
20247 p += 2;
20248 }
20249 else
20250 *opt_flags = 0;
20251
20252 if (!ASCII_ISALPHA(*p))
20253 return NULL;
20254 *arg = p;
20255
20256 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20257 p += 4; /* termcap option */
20258 else
20259 while (ASCII_ISALPHA(*p))
20260 ++p;
20261 return p;
20262}
20263
20264/*
20265 * ":function"
20266 */
20267 void
20268ex_function(eap)
20269 exarg_T *eap;
20270{
20271 char_u *theline;
20272 int j;
20273 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 char_u *name = NULL;
20276 char_u *p;
20277 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020278 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 garray_T newargs;
20280 garray_T newlines;
20281 int varargs = FALSE;
20282 int mustend = FALSE;
20283 int flags = 0;
20284 ufunc_T *fp;
20285 int indent;
20286 int nesting;
20287 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020288 dictitem_T *v;
20289 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020290 static int func_nr = 0; /* number for nameless function */
20291 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020292 hashtab_T *ht;
20293 int todo;
20294 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020295 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296
20297 /*
20298 * ":function" without argument: list functions.
20299 */
20300 if (ends_excmd(*eap->arg))
20301 {
20302 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020303 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020304 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020305 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020306 {
20307 if (!HASHITEM_EMPTY(hi))
20308 {
20309 --todo;
20310 fp = HI2UF(hi);
20311 if (!isdigit(*fp->uf_name))
20312 list_func_head(fp, FALSE);
20313 }
20314 }
20315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 eap->nextcmd = check_nextcmd(eap->arg);
20317 return;
20318 }
20319
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020320 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020321 * ":function /pat": list functions matching pattern.
20322 */
20323 if (*eap->arg == '/')
20324 {
20325 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20326 if (!eap->skip)
20327 {
20328 regmatch_T regmatch;
20329
20330 c = *p;
20331 *p = NUL;
20332 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20333 *p = c;
20334 if (regmatch.regprog != NULL)
20335 {
20336 regmatch.rm_ic = p_ic;
20337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020338 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20340 {
20341 if (!HASHITEM_EMPTY(hi))
20342 {
20343 --todo;
20344 fp = HI2UF(hi);
20345 if (!isdigit(*fp->uf_name)
20346 && vim_regexec(&regmatch, fp->uf_name, 0))
20347 list_func_head(fp, FALSE);
20348 }
20349 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020350 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020351 }
20352 }
20353 if (*p == '/')
20354 ++p;
20355 eap->nextcmd = check_nextcmd(p);
20356 return;
20357 }
20358
20359 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020360 * Get the function name. There are these situations:
20361 * func normal function name
20362 * "name" == func, "fudi.fd_dict" == NULL
20363 * dict.func new dictionary entry
20364 * "name" == NULL, "fudi.fd_dict" set,
20365 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20366 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020367 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020368 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20369 * dict.func existing dict entry that's not a Funcref
20370 * "name" == NULL, "fudi.fd_dict" set,
20371 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020374 name = trans_function_name(&p, eap->skip, 0, &fudi);
20375 paren = (vim_strchr(p, '(') != NULL);
20376 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 {
20378 /*
20379 * Return on an invalid expression in braces, unless the expression
20380 * evaluation has been cancelled due to an aborting error, an
20381 * interrupt, or an exception.
20382 */
20383 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020384 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020385 if (!eap->skip && fudi.fd_newkey != NULL)
20386 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020387 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 else
20391 eap->skip = TRUE;
20392 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020393
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 /* An error in a function call during evaluation of an expression in magic
20395 * braces should not cause the function not to be defined. */
20396 saved_did_emsg = did_emsg;
20397 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398
20399 /*
20400 * ":function func" with only function name: list function.
20401 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020402 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 {
20404 if (!ends_excmd(*skipwhite(p)))
20405 {
20406 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020407 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 }
20409 eap->nextcmd = check_nextcmd(p);
20410 if (eap->nextcmd != NULL)
20411 *p = NUL;
20412 if (!eap->skip && !got_int)
20413 {
20414 fp = find_func(name);
20415 if (fp != NULL)
20416 {
20417 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020418 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020420 if (FUNCLINE(fp, j) == NULL)
20421 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 msg_putchar('\n');
20423 msg_outnum((long)(j + 1));
20424 if (j < 9)
20425 msg_putchar(' ');
20426 if (j < 99)
20427 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020428 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 out_flush(); /* show a line at a time */
20430 ui_breakcheck();
20431 }
20432 if (!got_int)
20433 {
20434 msg_putchar('\n');
20435 msg_puts((char_u *)" endfunction");
20436 }
20437 }
20438 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020439 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020441 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 }
20443
20444 /*
20445 * ":function name(arg1, arg2)" Define function.
20446 */
20447 p = skipwhite(p);
20448 if (*p != '(')
20449 {
20450 if (!eap->skip)
20451 {
20452 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020453 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 }
20455 /* attempt to continue by skipping some text */
20456 if (vim_strchr(p, '(') != NULL)
20457 p = vim_strchr(p, '(');
20458 }
20459 p = skipwhite(p + 1);
20460
20461 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20462 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20463
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020464 if (!eap->skip)
20465 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020466 /* Check the name of the function. Unless it's a dictionary function
20467 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020468 if (name != NULL)
20469 arg = name;
20470 else
20471 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020472 if (arg != NULL && (fudi.fd_di == NULL
20473 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020474 {
20475 if (*arg == K_SPECIAL)
20476 j = 3;
20477 else
20478 j = 0;
20479 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20480 : eval_isnamec(arg[j])))
20481 ++j;
20482 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020483 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020484 }
20485 }
20486
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 /*
20488 * Isolate the arguments: "arg1, arg2, ...)"
20489 */
20490 while (*p != ')')
20491 {
20492 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20493 {
20494 varargs = TRUE;
20495 p += 3;
20496 mustend = TRUE;
20497 }
20498 else
20499 {
20500 arg = p;
20501 while (ASCII_ISALNUM(*p) || *p == '_')
20502 ++p;
20503 if (arg == p || isdigit(*arg)
20504 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20505 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20506 {
20507 if (!eap->skip)
20508 EMSG2(_("E125: Illegal argument: %s"), arg);
20509 break;
20510 }
20511 if (ga_grow(&newargs, 1) == FAIL)
20512 goto erret;
20513 c = *p;
20514 *p = NUL;
20515 arg = vim_strsave(arg);
20516 if (arg == NULL)
20517 goto erret;
20518 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20519 *p = c;
20520 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 if (*p == ',')
20522 ++p;
20523 else
20524 mustend = TRUE;
20525 }
20526 p = skipwhite(p);
20527 if (mustend && *p != ')')
20528 {
20529 if (!eap->skip)
20530 EMSG2(_(e_invarg2), eap->arg);
20531 break;
20532 }
20533 }
20534 ++p; /* skip the ')' */
20535
Bram Moolenaare9a41262005-01-15 22:18:47 +000020536 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 for (;;)
20538 {
20539 p = skipwhite(p);
20540 if (STRNCMP(p, "range", 5) == 0)
20541 {
20542 flags |= FC_RANGE;
20543 p += 5;
20544 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020545 else if (STRNCMP(p, "dict", 4) == 0)
20546 {
20547 flags |= FC_DICT;
20548 p += 4;
20549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 else if (STRNCMP(p, "abort", 5) == 0)
20551 {
20552 flags |= FC_ABORT;
20553 p += 5;
20554 }
20555 else
20556 break;
20557 }
20558
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020559 /* When there is a line break use what follows for the function body.
20560 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20561 if (*p == '\n')
20562 line_arg = p + 1;
20563 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 EMSG(_(e_trailing));
20565
20566 /*
20567 * Read the body of the function, until ":endfunction" is found.
20568 */
20569 if (KeyTyped)
20570 {
20571 /* Check if the function already exists, don't let the user type the
20572 * whole function before telling him it doesn't work! For a script we
20573 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020574 if (!eap->skip && !eap->forceit)
20575 {
20576 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20577 EMSG(_(e_funcdict));
20578 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020579 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020582 if (!eap->skip && did_emsg)
20583 goto erret;
20584
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 msg_putchar('\n'); /* don't overwrite the function name */
20586 cmdline_row = msg_row;
20587 }
20588
20589 indent = 2;
20590 nesting = 0;
20591 for (;;)
20592 {
20593 msg_scroll = TRUE;
20594 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020595 sourcing_lnum_off = sourcing_lnum;
20596
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020597 if (line_arg != NULL)
20598 {
20599 /* Use eap->arg, split up in parts by line breaks. */
20600 theline = line_arg;
20601 p = vim_strchr(theline, '\n');
20602 if (p == NULL)
20603 line_arg += STRLEN(line_arg);
20604 else
20605 {
20606 *p = NUL;
20607 line_arg = p + 1;
20608 }
20609 }
20610 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 theline = getcmdline(':', 0L, indent);
20612 else
20613 theline = eap->getline(':', eap->cookie, indent);
20614 if (KeyTyped)
20615 lines_left = Rows - 1;
20616 if (theline == NULL)
20617 {
20618 EMSG(_("E126: Missing :endfunction"));
20619 goto erret;
20620 }
20621
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020622 /* Detect line continuation: sourcing_lnum increased more than one. */
20623 if (sourcing_lnum > sourcing_lnum_off + 1)
20624 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20625 else
20626 sourcing_lnum_off = 0;
20627
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 if (skip_until != NULL)
20629 {
20630 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20631 * don't check for ":endfunc". */
20632 if (STRCMP(theline, skip_until) == 0)
20633 {
20634 vim_free(skip_until);
20635 skip_until = NULL;
20636 }
20637 }
20638 else
20639 {
20640 /* skip ':' and blanks*/
20641 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20642 ;
20643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020644 /* Check for "endfunction". */
20645 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020647 if (line_arg == NULL)
20648 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 break;
20650 }
20651
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020652 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 * at "end". */
20654 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20655 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020656 else if (STRNCMP(p, "if", 2) == 0
20657 || STRNCMP(p, "wh", 2) == 0
20658 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 || STRNCMP(p, "try", 3) == 0)
20660 indent += 2;
20661
20662 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020663 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020665 if (*p == '!')
20666 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667 p += eval_fname_script(p);
20668 if (ASCII_ISALPHA(*p))
20669 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020670 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 if (*skipwhite(p) == '(')
20672 {
20673 ++nesting;
20674 indent += 2;
20675 }
20676 }
20677 }
20678
20679 /* Check for ":append" or ":insert". */
20680 p = skip_range(p, NULL);
20681 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20682 || (p[0] == 'i'
20683 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20684 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20685 skip_until = vim_strsave((char_u *)".");
20686
20687 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20688 arg = skipwhite(skiptowhite(p));
20689 if (arg[0] == '<' && arg[1] =='<'
20690 && ((p[0] == 'p' && p[1] == 'y'
20691 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20692 || (p[0] == 'p' && p[1] == 'e'
20693 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20694 || (p[0] == 't' && p[1] == 'c'
20695 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20696 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20697 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020698 || (p[0] == 'm' && p[1] == 'z'
20699 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 ))
20701 {
20702 /* ":python <<" continues until a dot, like ":append" */
20703 p = skipwhite(arg + 2);
20704 if (*p == NUL)
20705 skip_until = vim_strsave((char_u *)".");
20706 else
20707 skip_until = vim_strsave(p);
20708 }
20709 }
20710
20711 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020712 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020713 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020714 if (line_arg == NULL)
20715 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020717 }
20718
20719 /* Copy the line to newly allocated memory. get_one_sourceline()
20720 * allocates 250 bytes per line, this saves 80% on average. The cost
20721 * is an extra alloc/free. */
20722 p = vim_strsave(theline);
20723 if (p != NULL)
20724 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020725 if (line_arg == NULL)
20726 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020727 theline = p;
20728 }
20729
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020730 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20731
20732 /* Add NULL lines for continuation lines, so that the line count is
20733 * equal to the index in the growarray. */
20734 while (sourcing_lnum_off-- > 0)
20735 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020736
20737 /* Check for end of eap->arg. */
20738 if (line_arg != NULL && *line_arg == NUL)
20739 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 }
20741
20742 /* Don't define the function when skipping commands or when an error was
20743 * detected. */
20744 if (eap->skip || did_emsg)
20745 goto erret;
20746
20747 /*
20748 * If there are no errors, add the function
20749 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020750 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020751 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020752 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020753 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020754 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020755 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020756 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020757 goto erret;
20758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020759
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020760 fp = find_func(name);
20761 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 if (!eap->forceit)
20764 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020765 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020766 goto erret;
20767 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020768 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020769 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020770 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020771 name);
20772 goto erret;
20773 }
20774 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020775 ga_clear_strings(&(fp->uf_args));
20776 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020777 vim_free(name);
20778 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 }
20781 else
20782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020783 char numbuf[20];
20784
20785 fp = NULL;
20786 if (fudi.fd_newkey == NULL && !eap->forceit)
20787 {
20788 EMSG(_(e_funcdict));
20789 goto erret;
20790 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020791 if (fudi.fd_di == NULL)
20792 {
20793 /* Can't add a function to a locked dictionary */
20794 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20795 goto erret;
20796 }
20797 /* Can't change an existing function if it is locked */
20798 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20799 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020800
20801 /* Give the function a sequential number. Can only be used with a
20802 * Funcref! */
20803 vim_free(name);
20804 sprintf(numbuf, "%d", ++func_nr);
20805 name = vim_strsave((char_u *)numbuf);
20806 if (name == NULL)
20807 goto erret;
20808 }
20809
20810 if (fp == NULL)
20811 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020812 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020813 {
20814 int slen, plen;
20815 char_u *scriptname;
20816
20817 /* Check that the autoload name matches the script name. */
20818 j = FAIL;
20819 if (sourcing_name != NULL)
20820 {
20821 scriptname = autoload_name(name);
20822 if (scriptname != NULL)
20823 {
20824 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020825 plen = (int)STRLEN(p);
20826 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020827 if (slen > plen && fnamecmp(p,
20828 sourcing_name + slen - plen) == 0)
20829 j = OK;
20830 vim_free(scriptname);
20831 }
20832 }
20833 if (j == FAIL)
20834 {
20835 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20836 goto erret;
20837 }
20838 }
20839
20840 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 if (fp == NULL)
20842 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843
20844 if (fudi.fd_dict != NULL)
20845 {
20846 if (fudi.fd_di == NULL)
20847 {
20848 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020849 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020850 if (fudi.fd_di == NULL)
20851 {
20852 vim_free(fp);
20853 goto erret;
20854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020855 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20856 {
20857 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020858 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020859 goto erret;
20860 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020861 }
20862 else
20863 /* overwrite existing dict entry */
20864 clear_tv(&fudi.fd_di->di_tv);
20865 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020866 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020867 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020868 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020869
20870 /* behave like "dict" was used */
20871 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020872 }
20873
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020875 STRCPY(fp->uf_name, name);
20876 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020878 fp->uf_args = newargs;
20879 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020880#ifdef FEAT_PROFILE
20881 fp->uf_tml_count = NULL;
20882 fp->uf_tml_total = NULL;
20883 fp->uf_tml_self = NULL;
20884 fp->uf_profiling = FALSE;
20885 if (prof_def_func())
20886 func_do_profile(fp);
20887#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020888 fp->uf_varargs = varargs;
20889 fp->uf_flags = flags;
20890 fp->uf_calls = 0;
20891 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020892 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893
20894erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 ga_clear_strings(&newargs);
20896 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020897ret_free:
20898 vim_free(skip_until);
20899 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902}
20903
20904/*
20905 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020906 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020908 * flags:
20909 * TFN_INT: internal function name OK
20910 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 * Advances "pp" to just after the function name (if no error).
20912 */
20913 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 char_u **pp;
20916 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020917 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020918 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020920 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 char_u *start;
20922 char_u *end;
20923 int lead;
20924 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020927
20928 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020931
20932 /* Check for hard coded <SNR>: already translated function ID (from a user
20933 * command). */
20934 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20935 && (*pp)[2] == (int)KE_SNR)
20936 {
20937 *pp += 3;
20938 len = get_id_len(pp) + 3;
20939 return vim_strnsave(start, len);
20940 }
20941
20942 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20943 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020945 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020947
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020948 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20949 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020950 if (end == start)
20951 {
20952 if (!skip)
20953 EMSG(_("E129: Function name required"));
20954 goto theend;
20955 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020956 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020957 {
20958 /*
20959 * Report an invalid expression in braces, unless the expression
20960 * evaluation has been cancelled due to an aborting error, an
20961 * interrupt, or an exception.
20962 */
20963 if (!aborting())
20964 {
20965 if (end != NULL)
20966 EMSG2(_(e_invarg2), start);
20967 }
20968 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020969 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020970 goto theend;
20971 }
20972
20973 if (lv.ll_tv != NULL)
20974 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 if (fdp != NULL)
20976 {
20977 fdp->fd_dict = lv.ll_dict;
20978 fdp->fd_newkey = lv.ll_newkey;
20979 lv.ll_newkey = NULL;
20980 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020982 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20983 {
20984 name = vim_strsave(lv.ll_tv->vval.v_string);
20985 *pp = end;
20986 }
20987 else
20988 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020989 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20990 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 EMSG(_(e_funcref));
20992 else
20993 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020994 name = NULL;
20995 }
20996 goto theend;
20997 }
20998
20999 if (lv.ll_name == NULL)
21000 {
21001 /* Error found, but continue after the function name. */
21002 *pp = end;
21003 goto theend;
21004 }
21005
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021006 /* Check if the name is a Funcref. If so, use the value. */
21007 if (lv.ll_exp_name != NULL)
21008 {
21009 len = (int)STRLEN(lv.ll_exp_name);
21010 name = deref_func_name(lv.ll_exp_name, &len);
21011 if (name == lv.ll_exp_name)
21012 name = NULL;
21013 }
21014 else
21015 {
21016 len = (int)(end - *pp);
21017 name = deref_func_name(*pp, &len);
21018 if (name == *pp)
21019 name = NULL;
21020 }
21021 if (name != NULL)
21022 {
21023 name = vim_strsave(name);
21024 *pp = end;
21025 goto theend;
21026 }
21027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021028 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021030 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021031 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21032 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21033 {
21034 /* When there was "s:" already or the name expanded to get a
21035 * leading "s:" then remove it. */
21036 lv.ll_name += 2;
21037 len -= 2;
21038 lead = 2;
21039 }
21040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021041 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021042 {
21043 if (lead == 2) /* skip over "s:" */
21044 lv.ll_name += 2;
21045 len = (int)(end - lv.ll_name);
21046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021047
21048 /*
21049 * Copy the function name to allocated memory.
21050 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21051 * Accept <SNR>123_name() outside a script.
21052 */
21053 if (skip)
21054 lead = 0; /* do nothing */
21055 else if (lead > 0)
21056 {
21057 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021058 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21059 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021060 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021061 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021062 if (current_SID <= 0)
21063 {
21064 EMSG(_(e_usingsid));
21065 goto theend;
21066 }
21067 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21068 lead += (int)STRLEN(sid_buf);
21069 }
21070 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021071 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021072 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021073 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021074 goto theend;
21075 }
21076 name = alloc((unsigned)(len + lead + 1));
21077 if (name != NULL)
21078 {
21079 if (lead > 0)
21080 {
21081 name[0] = K_SPECIAL;
21082 name[1] = KS_EXTRA;
21083 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021084 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021085 STRCPY(name + 3, sid_buf);
21086 }
21087 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21088 name[len + lead] = NUL;
21089 }
21090 *pp = end;
21091
21092theend:
21093 clear_lval(&lv);
21094 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095}
21096
21097/*
21098 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21099 * Return 2 if "p" starts with "s:".
21100 * Return 0 otherwise.
21101 */
21102 static int
21103eval_fname_script(p)
21104 char_u *p;
21105{
21106 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21107 || STRNICMP(p + 1, "SNR>", 4) == 0))
21108 return 5;
21109 if (p[0] == 's' && p[1] == ':')
21110 return 2;
21111 return 0;
21112}
21113
21114/*
21115 * Return TRUE if "p" starts with "<SID>" or "s:".
21116 * Only works if eval_fname_script() returned non-zero for "p"!
21117 */
21118 static int
21119eval_fname_sid(p)
21120 char_u *p;
21121{
21122 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21123}
21124
21125/*
21126 * List the head of the function: "name(arg1, arg2)".
21127 */
21128 static void
21129list_func_head(fp, indent)
21130 ufunc_T *fp;
21131 int indent;
21132{
21133 int j;
21134
21135 msg_start();
21136 if (indent)
21137 MSG_PUTS(" ");
21138 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021139 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 {
21141 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021142 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 }
21144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021145 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021147 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 {
21149 if (j)
21150 MSG_PUTS(", ");
21151 msg_puts(FUNCARG(fp, j));
21152 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021153 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 {
21155 if (j)
21156 MSG_PUTS(", ");
21157 MSG_PUTS("...");
21158 }
21159 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021160 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021161 if (p_verbose > 0)
21162 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163}
21164
21165/*
21166 * Find a function by name, return pointer to it in ufuncs.
21167 * Return NULL for unknown function.
21168 */
21169 static ufunc_T *
21170find_func(name)
21171 char_u *name;
21172{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021173 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021175 hi = hash_find(&func_hashtab, name);
21176 if (!HASHITEM_EMPTY(hi))
21177 return HI2UF(hi);
21178 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179}
21180
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021181#if defined(EXITFREE) || defined(PROTO)
21182 void
21183free_all_functions()
21184{
21185 hashitem_T *hi;
21186
21187 /* Need to start all over every time, because func_free() may change the
21188 * hash table. */
21189 while (func_hashtab.ht_used > 0)
21190 for (hi = func_hashtab.ht_array; ; ++hi)
21191 if (!HASHITEM_EMPTY(hi))
21192 {
21193 func_free(HI2UF(hi));
21194 break;
21195 }
21196}
21197#endif
21198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021199/*
21200 * Return TRUE if a function "name" exists.
21201 */
21202 static int
21203function_exists(name)
21204 char_u *name;
21205{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021206 char_u *nm = name;
21207 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021208 int n = FALSE;
21209
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021210 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021211 nm = skipwhite(nm);
21212
21213 /* Only accept "funcname", "funcname ", "funcname (..." and
21214 * "funcname(...", not "funcname!...". */
21215 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021216 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021217 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021218 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021219 else
21220 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021221 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021222 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021223 return n;
21224}
21225
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021226/*
21227 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021228 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021229 */
21230 static int
21231builtin_function(name)
21232 char_u *name;
21233{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021234 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21235 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021236}
21237
Bram Moolenaar05159a02005-02-26 23:04:13 +000021238#if defined(FEAT_PROFILE) || defined(PROTO)
21239/*
21240 * Start profiling function "fp".
21241 */
21242 static void
21243func_do_profile(fp)
21244 ufunc_T *fp;
21245{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021246 int len = fp->uf_lines.ga_len;
21247
21248 if (len == 0)
21249 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021250 fp->uf_tm_count = 0;
21251 profile_zero(&fp->uf_tm_self);
21252 profile_zero(&fp->uf_tm_total);
21253 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021254 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021255 if (fp->uf_tml_total == NULL)
21256 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021257 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021258 if (fp->uf_tml_self == NULL)
21259 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021260 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021261 fp->uf_tml_idx = -1;
21262 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21263 || fp->uf_tml_self == NULL)
21264 return; /* out of memory */
21265
21266 fp->uf_profiling = TRUE;
21267}
21268
21269/*
21270 * Dump the profiling results for all functions in file "fd".
21271 */
21272 void
21273func_dump_profile(fd)
21274 FILE *fd;
21275{
21276 hashitem_T *hi;
21277 int todo;
21278 ufunc_T *fp;
21279 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021280 ufunc_T **sorttab;
21281 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021283 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021284 if (todo == 0)
21285 return; /* nothing to dump */
21286
Bram Moolenaar73830342005-02-28 22:48:19 +000021287 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21288
Bram Moolenaar05159a02005-02-26 23:04:13 +000021289 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21290 {
21291 if (!HASHITEM_EMPTY(hi))
21292 {
21293 --todo;
21294 fp = HI2UF(hi);
21295 if (fp->uf_profiling)
21296 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021297 if (sorttab != NULL)
21298 sorttab[st_len++] = fp;
21299
Bram Moolenaar05159a02005-02-26 23:04:13 +000021300 if (fp->uf_name[0] == K_SPECIAL)
21301 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21302 else
21303 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21304 if (fp->uf_tm_count == 1)
21305 fprintf(fd, "Called 1 time\n");
21306 else
21307 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21308 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21309 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21310 fprintf(fd, "\n");
21311 fprintf(fd, "count total (s) self (s)\n");
21312
21313 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21314 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021315 if (FUNCLINE(fp, i) == NULL)
21316 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021317 prof_func_line(fd, fp->uf_tml_count[i],
21318 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021319 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21320 }
21321 fprintf(fd, "\n");
21322 }
21323 }
21324 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021325
21326 if (sorttab != NULL && st_len > 0)
21327 {
21328 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21329 prof_total_cmp);
21330 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21331 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21332 prof_self_cmp);
21333 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21334 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021335
21336 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021337}
Bram Moolenaar73830342005-02-28 22:48:19 +000021338
21339 static void
21340prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21341 FILE *fd;
21342 ufunc_T **sorttab;
21343 int st_len;
21344 char *title;
21345 int prefer_self; /* when equal print only self time */
21346{
21347 int i;
21348 ufunc_T *fp;
21349
21350 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21351 fprintf(fd, "count total (s) self (s) function\n");
21352 for (i = 0; i < 20 && i < st_len; ++i)
21353 {
21354 fp = sorttab[i];
21355 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21356 prefer_self);
21357 if (fp->uf_name[0] == K_SPECIAL)
21358 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21359 else
21360 fprintf(fd, " %s()\n", fp->uf_name);
21361 }
21362 fprintf(fd, "\n");
21363}
21364
21365/*
21366 * Print the count and times for one function or function line.
21367 */
21368 static void
21369prof_func_line(fd, count, total, self, prefer_self)
21370 FILE *fd;
21371 int count;
21372 proftime_T *total;
21373 proftime_T *self;
21374 int prefer_self; /* when equal print only self time */
21375{
21376 if (count > 0)
21377 {
21378 fprintf(fd, "%5d ", count);
21379 if (prefer_self && profile_equal(total, self))
21380 fprintf(fd, " ");
21381 else
21382 fprintf(fd, "%s ", profile_msg(total));
21383 if (!prefer_self && profile_equal(total, self))
21384 fprintf(fd, " ");
21385 else
21386 fprintf(fd, "%s ", profile_msg(self));
21387 }
21388 else
21389 fprintf(fd, " ");
21390}
21391
21392/*
21393 * Compare function for total time sorting.
21394 */
21395 static int
21396#ifdef __BORLANDC__
21397_RTLENTRYF
21398#endif
21399prof_total_cmp(s1, s2)
21400 const void *s1;
21401 const void *s2;
21402{
21403 ufunc_T *p1, *p2;
21404
21405 p1 = *(ufunc_T **)s1;
21406 p2 = *(ufunc_T **)s2;
21407 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21408}
21409
21410/*
21411 * Compare function for self time sorting.
21412 */
21413 static int
21414#ifdef __BORLANDC__
21415_RTLENTRYF
21416#endif
21417prof_self_cmp(s1, s2)
21418 const void *s1;
21419 const void *s2;
21420{
21421 ufunc_T *p1, *p2;
21422
21423 p1 = *(ufunc_T **)s1;
21424 p2 = *(ufunc_T **)s2;
21425 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21426}
21427
Bram Moolenaar05159a02005-02-26 23:04:13 +000021428#endif
21429
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021430/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021431 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021432 * Return TRUE if a package was loaded.
21433 */
21434 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021435script_autoload(name, reload)
21436 char_u *name;
21437 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021438{
21439 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021440 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021441 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021442 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021443
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021444 /* Return quickly when autoload disabled. */
21445 if (no_autoload)
21446 return FALSE;
21447
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021448 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021449 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021450 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021451 return FALSE;
21452
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021453 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021454
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021455 /* Find the name in the list of previously loaded package names. Skip
21456 * "autoload/", it's always the same. */
21457 for (i = 0; i < ga_loaded.ga_len; ++i)
21458 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21459 break;
21460 if (!reload && i < ga_loaded.ga_len)
21461 ret = FALSE; /* was loaded already */
21462 else
21463 {
21464 /* Remember the name if it wasn't loaded already. */
21465 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21466 {
21467 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21468 tofree = NULL;
21469 }
21470
21471 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021472 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021473 ret = TRUE;
21474 }
21475
21476 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021477 return ret;
21478}
21479
21480/*
21481 * Return the autoload script name for a function or variable name.
21482 * Returns NULL when out of memory.
21483 */
21484 static char_u *
21485autoload_name(name)
21486 char_u *name;
21487{
21488 char_u *p;
21489 char_u *scriptname;
21490
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021491 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021492 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21493 if (scriptname == NULL)
21494 return FALSE;
21495 STRCPY(scriptname, "autoload/");
21496 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021497 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021498 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021499 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021500 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021501 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021502}
21503
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21505
21506/*
21507 * Function given to ExpandGeneric() to obtain the list of user defined
21508 * function names.
21509 */
21510 char_u *
21511get_user_func_name(xp, idx)
21512 expand_T *xp;
21513 int idx;
21514{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021515 static long_u done;
21516 static hashitem_T *hi;
21517 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518
21519 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021521 done = 0;
21522 hi = func_hashtab.ht_array;
21523 }
21524 if (done < func_hashtab.ht_used)
21525 {
21526 if (done++ > 0)
21527 ++hi;
21528 while (HASHITEM_EMPTY(hi))
21529 ++hi;
21530 fp = HI2UF(hi);
21531
21532 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21533 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534
21535 cat_func_name(IObuff, fp);
21536 if (xp->xp_context != EXPAND_USER_FUNC)
21537 {
21538 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021539 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 STRCAT(IObuff, ")");
21541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 return IObuff;
21543 }
21544 return NULL;
21545}
21546
21547#endif /* FEAT_CMDL_COMPL */
21548
21549/*
21550 * Copy the function name of "fp" to buffer "buf".
21551 * "buf" must be able to hold the function name plus three bytes.
21552 * Takes care of script-local function names.
21553 */
21554 static void
21555cat_func_name(buf, fp)
21556 char_u *buf;
21557 ufunc_T *fp;
21558{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021559 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 {
21561 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021562 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 }
21564 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021565 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566}
21567
21568/*
21569 * ":delfunction {name}"
21570 */
21571 void
21572ex_delfunction(eap)
21573 exarg_T *eap;
21574{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021575 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 char_u *p;
21577 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021578 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579
21580 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021581 name = trans_function_name(&p, eap->skip, 0, &fudi);
21582 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584 {
21585 if (fudi.fd_dict != NULL && !eap->skip)
21586 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 if (!ends_excmd(*skipwhite(p)))
21590 {
21591 vim_free(name);
21592 EMSG(_(e_trailing));
21593 return;
21594 }
21595 eap->nextcmd = check_nextcmd(p);
21596 if (eap->nextcmd != NULL)
21597 *p = NUL;
21598
21599 if (!eap->skip)
21600 fp = find_func(name);
21601 vim_free(name);
21602
21603 if (!eap->skip)
21604 {
21605 if (fp == NULL)
21606 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021607 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 return;
21609 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021610 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 {
21612 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21613 return;
21614 }
21615
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021618 /* Delete the dict item that refers to the function, it will
21619 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021620 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021622 else
21623 func_free(fp);
21624 }
21625}
21626
21627/*
21628 * Free a function and remove it from the list of functions.
21629 */
21630 static void
21631func_free(fp)
21632 ufunc_T *fp;
21633{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021634 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021635
21636 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021637 ga_clear_strings(&(fp->uf_args));
21638 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021639#ifdef FEAT_PROFILE
21640 vim_free(fp->uf_tml_count);
21641 vim_free(fp->uf_tml_total);
21642 vim_free(fp->uf_tml_self);
21643#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021644
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021645 /* remove the function from the function hashtable */
21646 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21647 if (HASHITEM_EMPTY(hi))
21648 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 hash_remove(&func_hashtab, hi);
21651
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021652 vim_free(fp);
21653}
21654
21655/*
21656 * Unreference a Function: decrement the reference count and free it when it
21657 * becomes zero. Only for numbered functions.
21658 */
21659 static void
21660func_unref(name)
21661 char_u *name;
21662{
21663 ufunc_T *fp;
21664
21665 if (name != NULL && isdigit(*name))
21666 {
21667 fp = find_func(name);
21668 if (fp == NULL)
21669 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021670 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021671 {
21672 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021673 * when "uf_calls" becomes zero. */
21674 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021675 func_free(fp);
21676 }
21677 }
21678}
21679
21680/*
21681 * Count a reference to a Function.
21682 */
21683 static void
21684func_ref(name)
21685 char_u *name;
21686{
21687 ufunc_T *fp;
21688
21689 if (name != NULL && isdigit(*name))
21690 {
21691 fp = find_func(name);
21692 if (fp == NULL)
21693 EMSG2(_(e_intern2), "func_ref()");
21694 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021695 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 }
21697}
21698
21699/*
21700 * Call a user function.
21701 */
21702 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021703call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 ufunc_T *fp; /* pointer to function */
21705 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021706 typval_T *argvars; /* arguments */
21707 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 linenr_T firstline; /* first line of range */
21709 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021710 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711{
Bram Moolenaar33570922005-01-25 22:26:29 +000021712 char_u *save_sourcing_name;
21713 linenr_T save_sourcing_lnum;
21714 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021715 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 int save_did_emsg;
21717 static int depth = 0;
21718 dictitem_T *v;
21719 int fixvar_idx = 0; /* index in fixvar[] */
21720 int i;
21721 int ai;
21722 char_u numbuf[NUMBUFLEN];
21723 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021724#ifdef FEAT_PROFILE
21725 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021726 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728
21729 /* If depth of calling is getting too high, don't execute the function */
21730 if (depth >= p_mfd)
21731 {
21732 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021733 rettv->v_type = VAR_NUMBER;
21734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 return;
21736 }
21737 ++depth;
21738
21739 line_breakcheck(); /* check for CTRL-C hit */
21740
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021741 fc = (funccall_T *)alloc(sizeof(funccall_T));
21742 fc->caller = current_funccal;
21743 current_funccal = fc;
21744 fc->func = fp;
21745 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021746 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021747 fc->linenr = 0;
21748 fc->returned = FALSE;
21749 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021751 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21752 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021755 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021756 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21757 * each argument variable and saves a lot of time.
21758 */
21759 /*
21760 * Init l: variables.
21761 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021762 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021763 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021764 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021765 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21766 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021767 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021768 name = v->di_key;
21769 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021770 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021771 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021772 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021773 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 v->di_tv.vval.v_dict = selfdict;
21775 ++selfdict->dv_refcount;
21776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021777
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 /*
21779 * Init a: variables.
21780 * Set a:0 to "argcount".
21781 * Set a:000 to a list with room for the "..." arguments.
21782 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021783 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21784 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021785 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021786 /* Use "name" to avoid a warning from some compiler that checks the
21787 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021788 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021789 name = v->di_key;
21790 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021792 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021793 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021794 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021795 v->di_tv.vval.v_list = &fc->l_varlist;
21796 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21797 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21798 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021799
21800 /*
21801 * Set a:firstline to "firstline" and a:lastline to "lastline".
21802 * Set a:name to named arguments.
21803 * Set a:N to the "..." arguments.
21804 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021805 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021807 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 (varnumber_T)lastline);
21809 for (i = 0; i < argcount; ++i)
21810 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021811 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 if (ai < 0)
21813 /* named argument a:name */
21814 name = FUNCARG(fp, i);
21815 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021816 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021817 /* "..." argument a:1, a:2, etc. */
21818 sprintf((char *)numbuf, "%d", ai + 1);
21819 name = numbuf;
21820 }
21821 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21822 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021823 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21825 }
21826 else
21827 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021828 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21829 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021830 if (v == NULL)
21831 break;
21832 v->di_flags = DI_FLAGS_RO;
21833 }
21834 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021835 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021836
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021837 /* Note: the values are copied directly to avoid alloc/free.
21838 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021840 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021841
21842 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21843 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021844 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21845 fc->l_listitems[ai].li_tv = argvars[i];
21846 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021847 }
21848 }
21849
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 /* Don't redraw while executing the function. */
21851 ++RedrawingDisabled;
21852 save_sourcing_name = sourcing_name;
21853 save_sourcing_lnum = sourcing_lnum;
21854 sourcing_lnum = 1;
21855 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021856 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 if (sourcing_name != NULL)
21858 {
21859 if (save_sourcing_name != NULL
21860 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21861 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21862 else
21863 STRCPY(sourcing_name, "function ");
21864 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21865
21866 if (p_verbose >= 12)
21867 {
21868 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021869 verbose_enter_scroll();
21870
Bram Moolenaar555b2802005-05-19 21:08:39 +000021871 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 if (p_verbose >= 14)
21873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021875 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021876 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021877 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878
21879 msg_puts((char_u *)"(");
21880 for (i = 0; i < argcount; ++i)
21881 {
21882 if (i > 0)
21883 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021884 if (argvars[i].v_type == VAR_NUMBER)
21885 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 else
21887 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021888 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21889 if (s != NULL)
21890 {
21891 trunc_string(s, buf, MSG_BUF_CLEN);
21892 msg_puts(buf);
21893 vim_free(tofree);
21894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 }
21896 }
21897 msg_puts((char_u *)")");
21898 }
21899 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021900
21901 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 --no_wait_return;
21903 }
21904 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021905#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021906 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021907 {
21908 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21909 func_do_profile(fp);
21910 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021911 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021912 {
21913 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021914 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021915 profile_zero(&fp->uf_tm_children);
21916 }
21917 script_prof_save(&wait_start);
21918 }
21919#endif
21920
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021922 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 save_did_emsg = did_emsg;
21924 did_emsg = FALSE;
21925
21926 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021927 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21929
21930 --RedrawingDisabled;
21931
21932 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021933 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935 clear_tv(rettv);
21936 rettv->v_type = VAR_NUMBER;
21937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 }
21939
Bram Moolenaar05159a02005-02-26 23:04:13 +000021940#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021941 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021942 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021943 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021944 profile_end(&call_start);
21945 profile_sub_wait(&wait_start, &call_start);
21946 profile_add(&fp->uf_tm_total, &call_start);
21947 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021948 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021949 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021950 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21951 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021952 }
21953 }
21954#endif
21955
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 /* when being verbose, mention the return value */
21957 if (p_verbose >= 12)
21958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021960 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021963 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021964 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021965 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021966 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021969 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021970 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021971 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021972 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021973
Bram Moolenaar555b2802005-05-19 21:08:39 +000021974 /* The value may be very long. Skip the middle part, so that we
21975 * have some idea how it starts and ends. smsg() would always
21976 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021977 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021978 if (s != NULL)
21979 {
21980 trunc_string(s, buf, MSG_BUF_CLEN);
21981 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21982 vim_free(tofree);
21983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 }
21985 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021986
21987 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 --no_wait_return;
21989 }
21990
21991 vim_free(sourcing_name);
21992 sourcing_name = save_sourcing_name;
21993 sourcing_lnum = save_sourcing_lnum;
21994 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021995#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021996 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021997 script_prof_restore(&wait_start);
21998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999
22000 if (p_verbose >= 12 && sourcing_name != NULL)
22001 {
22002 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022003 verbose_enter_scroll();
22004
Bram Moolenaar555b2802005-05-19 21:08:39 +000022005 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022007
22008 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 --no_wait_return;
22010 }
22011
22012 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022013 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022015
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022016 /* If the a:000 list and the l: and a: dicts are not referenced we can
22017 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022018 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22019 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22020 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22021 {
22022 free_funccal(fc, FALSE);
22023 }
22024 else
22025 {
22026 hashitem_T *hi;
22027 listitem_T *li;
22028 int todo;
22029
22030 /* "fc" is still in use. This can happen when returning "a:000" or
22031 * assigning "l:" to a global variable.
22032 * Link "fc" in the list for garbage collection later. */
22033 fc->caller = previous_funccal;
22034 previous_funccal = fc;
22035
22036 /* Make a copy of the a: variables, since we didn't do that above. */
22037 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22038 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22039 {
22040 if (!HASHITEM_EMPTY(hi))
22041 {
22042 --todo;
22043 v = HI2DI(hi);
22044 copy_tv(&v->di_tv, &v->di_tv);
22045 }
22046 }
22047
22048 /* Make a copy of the a:000 items, since we didn't do that above. */
22049 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22050 copy_tv(&li->li_tv, &li->li_tv);
22051 }
22052}
22053
22054/*
22055 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022056 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022057 */
22058 static int
22059can_free_funccal(fc, copyID)
22060 funccall_T *fc;
22061 int copyID;
22062{
22063 return (fc->l_varlist.lv_copyID != copyID
22064 && fc->l_vars.dv_copyID != copyID
22065 && fc->l_avars.dv_copyID != copyID);
22066}
22067
22068/*
22069 * Free "fc" and what it contains.
22070 */
22071 static void
22072free_funccal(fc, free_val)
22073 funccall_T *fc;
22074 int free_val; /* a: vars were allocated */
22075{
22076 listitem_T *li;
22077
22078 /* The a: variables typevals may not have been allocated, only free the
22079 * allocated variables. */
22080 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22081
22082 /* free all l: variables */
22083 vars_clear(&fc->l_vars.dv_hashtab);
22084
22085 /* Free the a:000 variables if they were allocated. */
22086 if (free_val)
22087 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22088 clear_tv(&li->li_tv);
22089
22090 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091}
22092
22093/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022094 * Add a number variable "name" to dict "dp" with value "nr".
22095 */
22096 static void
22097add_nr_var(dp, v, name, nr)
22098 dict_T *dp;
22099 dictitem_T *v;
22100 char *name;
22101 varnumber_T nr;
22102{
22103 STRCPY(v->di_key, name);
22104 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22105 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22106 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022107 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022108 v->di_tv.vval.v_number = nr;
22109}
22110
22111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 * ":return [expr]"
22113 */
22114 void
22115ex_return(eap)
22116 exarg_T *eap;
22117{
22118 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 int returning = FALSE;
22121
22122 if (current_funccal == NULL)
22123 {
22124 EMSG(_("E133: :return not inside a function"));
22125 return;
22126 }
22127
22128 if (eap->skip)
22129 ++emsg_skip;
22130
22131 eap->nextcmd = NULL;
22132 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 {
22135 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022136 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022138 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 }
22140 /* It's safer to return also on error. */
22141 else if (!eap->skip)
22142 {
22143 /*
22144 * Return unless the expression evaluation has been cancelled due to an
22145 * aborting error, an interrupt, or an exception.
22146 */
22147 if (!aborting())
22148 returning = do_return(eap, FALSE, TRUE, NULL);
22149 }
22150
22151 /* When skipping or the return gets pending, advance to the next command
22152 * in this line (!returning). Otherwise, ignore the rest of the line.
22153 * Following lines will be ignored by get_func_line(). */
22154 if (returning)
22155 eap->nextcmd = NULL;
22156 else if (eap->nextcmd == NULL) /* no argument */
22157 eap->nextcmd = check_nextcmd(arg);
22158
22159 if (eap->skip)
22160 --emsg_skip;
22161}
22162
22163/*
22164 * Return from a function. Possibly makes the return pending. Also called
22165 * for a pending return at the ":endtry" or after returning from an extra
22166 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022167 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022168 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 * FALSE when the return gets pending.
22170 */
22171 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022172do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 exarg_T *eap;
22174 int reanimate;
22175 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177{
22178 int idx;
22179 struct condstack *cstack = eap->cstack;
22180
22181 if (reanimate)
22182 /* Undo the return. */
22183 current_funccal->returned = FALSE;
22184
22185 /*
22186 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22187 * not in its finally clause (which then is to be executed next) is found.
22188 * In this case, make the ":return" pending for execution at the ":endtry".
22189 * Otherwise, return normally.
22190 */
22191 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22192 if (idx >= 0)
22193 {
22194 cstack->cs_pending[idx] = CSTP_RETURN;
22195
22196 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022197 /* A pending return again gets pending. "rettv" points to an
22198 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022200 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 else
22202 {
22203 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022204 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022206 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022208 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 {
22210 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022211 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022212 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 else
22214 EMSG(_(e_outofmem));
22215 }
22216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022217 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218
22219 if (reanimate)
22220 {
22221 /* The pending return value could be overwritten by a ":return"
22222 * without argument in a finally clause; reset the default
22223 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022224 current_funccal->rettv->v_type = VAR_NUMBER;
22225 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 }
22227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022228 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 }
22230 else
22231 {
22232 current_funccal->returned = TRUE;
22233
22234 /* If the return is carried out now, store the return value. For
22235 * a return immediately after reanimation, the value is already
22236 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022237 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022240 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022242 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 }
22244 }
22245
22246 return idx < 0;
22247}
22248
22249/*
22250 * Free the variable with a pending return value.
22251 */
22252 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022253discard_pending_return(rettv)
22254 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255{
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257}
22258
22259/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022260 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 * is an allocated string. Used by report_pending() for verbose messages.
22262 */
22263 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022264get_return_cmd(rettv)
22265 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022267 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022268 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022269 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022271 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022272 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022273 if (s == NULL)
22274 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022275
22276 STRCPY(IObuff, ":return ");
22277 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22278 if (STRLEN(s) + 8 >= IOSIZE)
22279 STRCPY(IObuff + IOSIZE - 4, "...");
22280 vim_free(tofree);
22281 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282}
22283
22284/*
22285 * Get next function line.
22286 * Called by do_cmdline() to get the next line.
22287 * Returns allocated string, or NULL for end of function.
22288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 char_u *
22290get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022291 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022293 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294{
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022296 ufunc_T *fp = fcp->func;
22297 char_u *retval;
22298 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299
22300 /* If breakpoints have been added/deleted need to check for it. */
22301 if (fcp->dbg_tick != debug_tick)
22302 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022303 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 sourcing_lnum);
22305 fcp->dbg_tick = debug_tick;
22306 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022307#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022308 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022309 func_line_end(cookie);
22310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311
Bram Moolenaar05159a02005-02-26 23:04:13 +000022312 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022313 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22314 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 retval = NULL;
22316 else
22317 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022318 /* Skip NULL lines (continuation lines). */
22319 while (fcp->linenr < gap->ga_len
22320 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22321 ++fcp->linenr;
22322 if (fcp->linenr >= gap->ga_len)
22323 retval = NULL;
22324 else
22325 {
22326 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22327 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022328#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022329 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022330 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022331#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 }
22334
22335 /* Did we encounter a breakpoint? */
22336 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22337 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022338 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022340 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 sourcing_lnum);
22342 fcp->dbg_tick = debug_tick;
22343 }
22344
22345 return retval;
22346}
22347
Bram Moolenaar05159a02005-02-26 23:04:13 +000022348#if defined(FEAT_PROFILE) || defined(PROTO)
22349/*
22350 * Called when starting to read a function line.
22351 * "sourcing_lnum" must be correct!
22352 * When skipping lines it may not actually be executed, but we won't find out
22353 * until later and we need to store the time now.
22354 */
22355 void
22356func_line_start(cookie)
22357 void *cookie;
22358{
22359 funccall_T *fcp = (funccall_T *)cookie;
22360 ufunc_T *fp = fcp->func;
22361
22362 if (fp->uf_profiling && sourcing_lnum >= 1
22363 && sourcing_lnum <= fp->uf_lines.ga_len)
22364 {
22365 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022366 /* Skip continuation lines. */
22367 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22368 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022369 fp->uf_tml_execed = FALSE;
22370 profile_start(&fp->uf_tml_start);
22371 profile_zero(&fp->uf_tml_children);
22372 profile_get_wait(&fp->uf_tml_wait);
22373 }
22374}
22375
22376/*
22377 * Called when actually executing a function line.
22378 */
22379 void
22380func_line_exec(cookie)
22381 void *cookie;
22382{
22383 funccall_T *fcp = (funccall_T *)cookie;
22384 ufunc_T *fp = fcp->func;
22385
22386 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22387 fp->uf_tml_execed = TRUE;
22388}
22389
22390/*
22391 * Called when done with a function line.
22392 */
22393 void
22394func_line_end(cookie)
22395 void *cookie;
22396{
22397 funccall_T *fcp = (funccall_T *)cookie;
22398 ufunc_T *fp = fcp->func;
22399
22400 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22401 {
22402 if (fp->uf_tml_execed)
22403 {
22404 ++fp->uf_tml_count[fp->uf_tml_idx];
22405 profile_end(&fp->uf_tml_start);
22406 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022407 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022408 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22409 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022410 }
22411 fp->uf_tml_idx = -1;
22412 }
22413}
22414#endif
22415
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416/*
22417 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022418 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 */
22420 int
22421func_has_ended(cookie)
22422 void *cookie;
22423{
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425
22426 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22427 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022428 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 || fcp->returned);
22430}
22431
22432/*
22433 * return TRUE if cookie indicates a function which "abort"s on errors.
22434 */
22435 int
22436func_has_abort(cookie)
22437 void *cookie;
22438{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022439 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440}
22441
22442#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22443typedef enum
22444{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022445 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22446 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22447 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448} var_flavour_T;
22449
22450static var_flavour_T var_flavour __ARGS((char_u *varname));
22451
22452 static var_flavour_T
22453var_flavour(varname)
22454 char_u *varname;
22455{
22456 char_u *p = varname;
22457
22458 if (ASCII_ISUPPER(*p))
22459 {
22460 while (*(++p))
22461 if (ASCII_ISLOWER(*p))
22462 return VAR_FLAVOUR_SESSION;
22463 return VAR_FLAVOUR_VIMINFO;
22464 }
22465 else
22466 return VAR_FLAVOUR_DEFAULT;
22467}
22468#endif
22469
22470#if defined(FEAT_VIMINFO) || defined(PROTO)
22471/*
22472 * Restore global vars that start with a capital from the viminfo file
22473 */
22474 int
22475read_viminfo_varlist(virp, writing)
22476 vir_T *virp;
22477 int writing;
22478{
22479 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022480 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022481 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482
22483 if (!writing && (find_viminfo_parameter('!') != NULL))
22484 {
22485 tab = vim_strchr(virp->vir_line + 1, '\t');
22486 if (tab != NULL)
22487 {
22488 *tab++ = '\0'; /* isolate the variable name */
22489 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022490 type = VAR_STRING;
22491#ifdef FEAT_FLOAT
22492 else if (*tab == 'F')
22493 type = VAR_FLOAT;
22494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495
22496 tab = vim_strchr(tab, '\t');
22497 if (tab != NULL)
22498 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022499 tv.v_type = type;
22500 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022501 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022503#ifdef FEAT_FLOAT
22504 else if (type == VAR_FLOAT)
22505 (void)string2float(tab + 1, &tv.vval.v_float);
22506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022508 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022509 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022510 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022511 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 }
22513 }
22514 }
22515
22516 return viminfo_readline(virp);
22517}
22518
22519/*
22520 * Write global vars that start with a capital to the viminfo file
22521 */
22522 void
22523write_viminfo_varlist(fp)
22524 FILE *fp;
22525{
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 hashitem_T *hi;
22527 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022528 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022529 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022530 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022531 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022532 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533
22534 if (find_viminfo_parameter('!') == NULL)
22535 return;
22536
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022537 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022538
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022539 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022542 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022544 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 this_var = HI2DI(hi);
22546 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022547 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022548 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022549 {
22550 case VAR_STRING: s = "STR"; break;
22551 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022552#ifdef FEAT_FLOAT
22553 case VAR_FLOAT: s = "FLO"; break;
22554#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022555 default: continue;
22556 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022558 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022559 if (p != NULL)
22560 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022561 vim_free(tofree);
22562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 }
22564 }
22565}
22566#endif
22567
22568#if defined(FEAT_SESSION) || defined(PROTO)
22569 int
22570store_session_globals(fd)
22571 FILE *fd;
22572{
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 hashitem_T *hi;
22574 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022575 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 char_u *p, *t;
22577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022578 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022583 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022584 this_var = HI2DI(hi);
22585 if ((this_var->di_tv.v_type == VAR_NUMBER
22586 || this_var->di_tv.v_type == VAR_STRING)
22587 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022589 /* Escape special characters with a backslash. Turn a LF and
22590 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022592 (char_u *)"\\\"\n\r");
22593 if (p == NULL) /* out of memory */
22594 break;
22595 for (t = p; *t != NUL; ++t)
22596 if (*t == '\n')
22597 *t = 'n';
22598 else if (*t == '\r')
22599 *t = 'r';
22600 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022601 this_var->di_key,
22602 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22603 : ' ',
22604 p,
22605 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22606 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022607 || put_eol(fd) == FAIL)
22608 {
22609 vim_free(p);
22610 return FAIL;
22611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 vim_free(p);
22613 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022614#ifdef FEAT_FLOAT
22615 else if (this_var->di_tv.v_type == VAR_FLOAT
22616 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22617 {
22618 float_T f = this_var->di_tv.vval.v_float;
22619 int sign = ' ';
22620
22621 if (f < 0)
22622 {
22623 f = -f;
22624 sign = '-';
22625 }
22626 if ((fprintf(fd, "let %s = %c&%f",
22627 this_var->di_key, sign, f) < 0)
22628 || put_eol(fd) == FAIL)
22629 return FAIL;
22630 }
22631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 }
22633 }
22634 return OK;
22635}
22636#endif
22637
Bram Moolenaar661b1822005-07-28 22:36:45 +000022638/*
22639 * Display script name where an item was last set.
22640 * Should only be invoked when 'verbose' is non-zero.
22641 */
22642 void
22643last_set_msg(scriptID)
22644 scid_T scriptID;
22645{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022646 char_u *p;
22647
Bram Moolenaar661b1822005-07-28 22:36:45 +000022648 if (scriptID != 0)
22649 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022650 p = home_replace_save(NULL, get_scriptname(scriptID));
22651 if (p != NULL)
22652 {
22653 verbose_enter();
22654 MSG_PUTS(_("\n\tLast set from "));
22655 MSG_PUTS(p);
22656 vim_free(p);
22657 verbose_leave();
22658 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022659 }
22660}
22661
Bram Moolenaard812df62008-11-09 12:46:09 +000022662/*
22663 * List v:oldfiles in a nice way.
22664 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022665 void
22666ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022667 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022668{
22669 list_T *l = vimvars[VV_OLDFILES].vv_list;
22670 listitem_T *li;
22671 int nr = 0;
22672
22673 if (l == NULL)
22674 msg((char_u *)_("No old files"));
22675 else
22676 {
22677 msg_start();
22678 msg_scroll = TRUE;
22679 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22680 {
22681 msg_outnum((long)++nr);
22682 MSG_PUTS(": ");
22683 msg_outtrans(get_tv_string(&li->li_tv));
22684 msg_putchar('\n');
22685 out_flush(); /* output one line at a time */
22686 ui_breakcheck();
22687 }
22688 /* Assume "got_int" was set to truncate the listing. */
22689 got_int = FALSE;
22690
22691#ifdef FEAT_BROWSE_CMD
22692 if (cmdmod.browse)
22693 {
22694 quit_more = FALSE;
22695 nr = prompt_for_number(FALSE);
22696 msg_starthere();
22697 if (nr > 0)
22698 {
22699 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22700 (long)nr);
22701
22702 if (p != NULL)
22703 {
22704 p = expand_env_save(p);
22705 eap->arg = p;
22706 eap->cmdidx = CMD_edit;
22707 cmdmod.browse = FALSE;
22708 do_exedit(eap, NULL);
22709 vim_free(p);
22710 }
22711 }
22712 }
22713#endif
22714 }
22715}
22716
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717#endif /* FEAT_EVAL */
22718
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022720#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721
22722#ifdef WIN3264
22723/*
22724 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22725 */
22726static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22727static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22728static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22729
22730/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022731 * Get the short path (8.3) for the filename in "fnamep".
22732 * Only works for a valid file name.
22733 * When the path gets longer "fnamep" is changed and the allocated buffer
22734 * is put in "bufp".
22735 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22736 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 */
22738 static int
22739get_short_pathname(fnamep, bufp, fnamelen)
22740 char_u **fnamep;
22741 char_u **bufp;
22742 int *fnamelen;
22743{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022744 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 char_u *newbuf;
22746
22747 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 l = GetShortPathName(*fnamep, *fnamep, len);
22749 if (l > len - 1)
22750 {
22751 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022752 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 newbuf = vim_strnsave(*fnamep, l);
22754 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756
22757 vim_free(*bufp);
22758 *fnamep = *bufp = newbuf;
22759
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022760 /* Really should always succeed, as the buffer is big enough. */
22761 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 }
22763
22764 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022765 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766}
22767
22768/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022769 * Get the short path (8.3) for the filename in "fname". The converted
22770 * path is returned in "bufp".
22771 *
22772 * Some of the directories specified in "fname" may not exist. This function
22773 * will shorten the existing directories at the beginning of the path and then
22774 * append the remaining non-existing path.
22775 *
22776 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022777 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022778 * bufp - Pointer to an allocated buffer for the filename.
22779 * fnamelen - Length of the filename pointed to by fname
22780 *
22781 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 */
22783 static int
22784shortpath_for_invalid_fname(fname, bufp, fnamelen)
22785 char_u **fname;
22786 char_u **bufp;
22787 int *fnamelen;
22788{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022789 char_u *short_fname, *save_fname, *pbuf_unused;
22790 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022792 int old_len, len;
22793 int new_len, sfx_len;
22794 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795
22796 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022797 old_len = *fnamelen;
22798 save_fname = vim_strnsave(*fname, old_len);
22799 pbuf_unused = NULL;
22800 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022802 endp = save_fname + old_len - 1; /* Find the end of the copy */
22803 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022805 /*
22806 * Try shortening the supplied path till it succeeds by removing one
22807 * directory at a time from the tail of the path.
22808 */
22809 len = 0;
22810 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022812 /* go back one path-separator */
22813 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22814 --endp;
22815 if (endp <= save_fname)
22816 break; /* processed the complete path */
22817
22818 /*
22819 * Replace the path separator with a NUL and try to shorten the
22820 * resulting path.
22821 */
22822 ch = *endp;
22823 *endp = 0;
22824 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022825 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022826 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22827 {
22828 retval = FAIL;
22829 goto theend;
22830 }
22831 *endp = ch; /* preserve the string */
22832
22833 if (len > 0)
22834 break; /* successfully shortened the path */
22835
22836 /* failed to shorten the path. Skip the path separator */
22837 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 }
22839
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022840 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022842 /*
22843 * Succeeded in shortening the path. Now concatenate the shortened
22844 * path with the remaining path at the tail.
22845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022847 /* Compute the length of the new path. */
22848 sfx_len = (int)(save_endp - endp) + 1;
22849 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022851 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022853 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022855 /* There is not enough space in the currently allocated string,
22856 * copy it to a buffer big enough. */
22857 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022859 {
22860 retval = FAIL;
22861 goto theend;
22862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 }
22864 else
22865 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022866 /* Transfer short_fname to the main buffer (it's big enough),
22867 * unless get_short_pathname() did its work in-place. */
22868 *fname = *bufp = save_fname;
22869 if (short_fname != save_fname)
22870 vim_strncpy(save_fname, short_fname, len);
22871 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022873
22874 /* concat the not-shortened part of the path */
22875 vim_strncpy(*fname + len, endp, sfx_len);
22876 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022878
22879theend:
22880 vim_free(pbuf_unused);
22881 vim_free(save_fname);
22882
22883 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884}
22885
22886/*
22887 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022888 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 */
22890 static int
22891shortpath_for_partial(fnamep, bufp, fnamelen)
22892 char_u **fnamep;
22893 char_u **bufp;
22894 int *fnamelen;
22895{
22896 int sepcount, len, tflen;
22897 char_u *p;
22898 char_u *pbuf, *tfname;
22899 int hasTilde;
22900
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022901 /* Count up the path separators from the RHS.. so we know which part
22902 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022904 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 if (vim_ispathsep(*p))
22906 ++sepcount;
22907
22908 /* Need full path first (use expand_env() to remove a "~/") */
22909 hasTilde = (**fnamep == '~');
22910 if (hasTilde)
22911 pbuf = tfname = expand_env_save(*fnamep);
22912 else
22913 pbuf = tfname = FullName_save(*fnamep, FALSE);
22914
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022915 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022917 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22918 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919
22920 if (len == 0)
22921 {
22922 /* Don't have a valid filename, so shorten the rest of the
22923 * path if we can. This CAN give us invalid 8.3 filenames, but
22924 * there's not a lot of point in guessing what it might be.
22925 */
22926 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022927 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 }
22930
22931 /* Count the paths backward to find the beginning of the desired string. */
22932 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022933 {
22934#ifdef FEAT_MBYTE
22935 if (has_mbyte)
22936 p -= mb_head_off(tfname, p);
22937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 if (vim_ispathsep(*p))
22939 {
22940 if (sepcount == 0 || (hasTilde && sepcount == 1))
22941 break;
22942 else
22943 sepcount --;
22944 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 if (hasTilde)
22947 {
22948 --p;
22949 if (p >= tfname)
22950 *p = '~';
22951 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 }
22954 else
22955 ++p;
22956
22957 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22958 vim_free(*bufp);
22959 *fnamelen = (int)STRLEN(p);
22960 *bufp = pbuf;
22961 *fnamep = p;
22962
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022963 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964}
22965#endif /* WIN3264 */
22966
22967/*
22968 * Adjust a filename, according to a string of modifiers.
22969 * *fnamep must be NUL terminated when called. When returning, the length is
22970 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022971 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 * When there is an error, *fnamep is set to NULL.
22973 */
22974 int
22975modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22976 char_u *src; /* string with modifiers */
22977 int *usedlen; /* characters after src that are used */
22978 char_u **fnamep; /* file name so far */
22979 char_u **bufp; /* buffer for allocated file name or NULL */
22980 int *fnamelen; /* length of fnamep */
22981{
22982 int valid = 0;
22983 char_u *tail;
22984 char_u *s, *p, *pbuf;
22985 char_u dirname[MAXPATHL];
22986 int c;
22987 int has_fullname = 0;
22988#ifdef WIN3264
22989 int has_shortname = 0;
22990#endif
22991
22992repeat:
22993 /* ":p" - full path/file_name */
22994 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22995 {
22996 has_fullname = 1;
22997
22998 valid |= VALID_PATH;
22999 *usedlen += 2;
23000
23001 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23002 if ((*fnamep)[0] == '~'
23003#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23004 && ((*fnamep)[1] == '/'
23005# ifdef BACKSLASH_IN_FILENAME
23006 || (*fnamep)[1] == '\\'
23007# endif
23008 || (*fnamep)[1] == NUL)
23009
23010#endif
23011 )
23012 {
23013 *fnamep = expand_env_save(*fnamep);
23014 vim_free(*bufp); /* free any allocated file name */
23015 *bufp = *fnamep;
23016 if (*fnamep == NULL)
23017 return -1;
23018 }
23019
23020 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023021 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 {
23023 if (vim_ispathsep(*p)
23024 && p[1] == '.'
23025 && (p[2] == NUL
23026 || vim_ispathsep(p[2])
23027 || (p[2] == '.'
23028 && (p[3] == NUL || vim_ispathsep(p[3])))))
23029 break;
23030 }
23031
23032 /* FullName_save() is slow, don't use it when not needed. */
23033 if (*p != NUL || !vim_isAbsName(*fnamep))
23034 {
23035 *fnamep = FullName_save(*fnamep, *p != NUL);
23036 vim_free(*bufp); /* free any allocated file name */
23037 *bufp = *fnamep;
23038 if (*fnamep == NULL)
23039 return -1;
23040 }
23041
23042 /* Append a path separator to a directory. */
23043 if (mch_isdir(*fnamep))
23044 {
23045 /* Make room for one or two extra characters. */
23046 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23047 vim_free(*bufp); /* free any allocated file name */
23048 *bufp = *fnamep;
23049 if (*fnamep == NULL)
23050 return -1;
23051 add_pathsep(*fnamep);
23052 }
23053 }
23054
23055 /* ":." - path relative to the current directory */
23056 /* ":~" - path relative to the home directory */
23057 /* ":8" - shortname path - postponed till after */
23058 while (src[*usedlen] == ':'
23059 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23060 {
23061 *usedlen += 2;
23062 if (c == '8')
23063 {
23064#ifdef WIN3264
23065 has_shortname = 1; /* Postpone this. */
23066#endif
23067 continue;
23068 }
23069 pbuf = NULL;
23070 /* Need full path first (use expand_env() to remove a "~/") */
23071 if (!has_fullname)
23072 {
23073 if (c == '.' && **fnamep == '~')
23074 p = pbuf = expand_env_save(*fnamep);
23075 else
23076 p = pbuf = FullName_save(*fnamep, FALSE);
23077 }
23078 else
23079 p = *fnamep;
23080
23081 has_fullname = 0;
23082
23083 if (p != NULL)
23084 {
23085 if (c == '.')
23086 {
23087 mch_dirname(dirname, MAXPATHL);
23088 s = shorten_fname(p, dirname);
23089 if (s != NULL)
23090 {
23091 *fnamep = s;
23092 if (pbuf != NULL)
23093 {
23094 vim_free(*bufp); /* free any allocated file name */
23095 *bufp = pbuf;
23096 pbuf = NULL;
23097 }
23098 }
23099 }
23100 else
23101 {
23102 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23103 /* Only replace it when it starts with '~' */
23104 if (*dirname == '~')
23105 {
23106 s = vim_strsave(dirname);
23107 if (s != NULL)
23108 {
23109 *fnamep = s;
23110 vim_free(*bufp);
23111 *bufp = s;
23112 }
23113 }
23114 }
23115 vim_free(pbuf);
23116 }
23117 }
23118
23119 tail = gettail(*fnamep);
23120 *fnamelen = (int)STRLEN(*fnamep);
23121
23122 /* ":h" - head, remove "/file_name", can be repeated */
23123 /* Don't remove the first "/" or "c:\" */
23124 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23125 {
23126 valid |= VALID_HEAD;
23127 *usedlen += 2;
23128 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023129 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023130 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 *fnamelen = (int)(tail - *fnamep);
23132#ifdef VMS
23133 if (*fnamelen > 0)
23134 *fnamelen += 1; /* the path separator is part of the path */
23135#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023136 if (*fnamelen == 0)
23137 {
23138 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23139 p = vim_strsave((char_u *)".");
23140 if (p == NULL)
23141 return -1;
23142 vim_free(*bufp);
23143 *bufp = *fnamep = tail = p;
23144 *fnamelen = 1;
23145 }
23146 else
23147 {
23148 while (tail > s && !after_pathsep(s, tail))
23149 mb_ptr_back(*fnamep, tail);
23150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 }
23152
23153 /* ":8" - shortname */
23154 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23155 {
23156 *usedlen += 2;
23157#ifdef WIN3264
23158 has_shortname = 1;
23159#endif
23160 }
23161
23162#ifdef WIN3264
23163 /* Check shortname after we have done 'heads' and before we do 'tails'
23164 */
23165 if (has_shortname)
23166 {
23167 pbuf = NULL;
23168 /* Copy the string if it is shortened by :h */
23169 if (*fnamelen < (int)STRLEN(*fnamep))
23170 {
23171 p = vim_strnsave(*fnamep, *fnamelen);
23172 if (p == 0)
23173 return -1;
23174 vim_free(*bufp);
23175 *bufp = *fnamep = p;
23176 }
23177
23178 /* Split into two implementations - makes it easier. First is where
23179 * there isn't a full name already, second is where there is.
23180 */
23181 if (!has_fullname && !vim_isAbsName(*fnamep))
23182 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023183 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 return -1;
23185 }
23186 else
23187 {
23188 int l;
23189
23190 /* Simple case, already have the full-name
23191 * Nearly always shorter, so try first time. */
23192 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023193 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 return -1;
23195
23196 if (l == 0)
23197 {
23198 /* Couldn't find the filename.. search the paths.
23199 */
23200 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023201 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 return -1;
23203 }
23204 *fnamelen = l;
23205 }
23206 }
23207#endif /* WIN3264 */
23208
23209 /* ":t" - tail, just the basename */
23210 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23211 {
23212 *usedlen += 2;
23213 *fnamelen -= (int)(tail - *fnamep);
23214 *fnamep = tail;
23215 }
23216
23217 /* ":e" - extension, can be repeated */
23218 /* ":r" - root, without extension, can be repeated */
23219 while (src[*usedlen] == ':'
23220 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23221 {
23222 /* find a '.' in the tail:
23223 * - for second :e: before the current fname
23224 * - otherwise: The last '.'
23225 */
23226 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23227 s = *fnamep - 2;
23228 else
23229 s = *fnamep + *fnamelen - 1;
23230 for ( ; s > tail; --s)
23231 if (s[0] == '.')
23232 break;
23233 if (src[*usedlen + 1] == 'e') /* :e */
23234 {
23235 if (s > tail)
23236 {
23237 *fnamelen += (int)(*fnamep - (s + 1));
23238 *fnamep = s + 1;
23239#ifdef VMS
23240 /* cut version from the extension */
23241 s = *fnamep + *fnamelen - 1;
23242 for ( ; s > *fnamep; --s)
23243 if (s[0] == ';')
23244 break;
23245 if (s > *fnamep)
23246 *fnamelen = s - *fnamep;
23247#endif
23248 }
23249 else if (*fnamep <= tail)
23250 *fnamelen = 0;
23251 }
23252 else /* :r */
23253 {
23254 if (s > tail) /* remove one extension */
23255 *fnamelen = (int)(s - *fnamep);
23256 }
23257 *usedlen += 2;
23258 }
23259
23260 /* ":s?pat?foo?" - substitute */
23261 /* ":gs?pat?foo?" - global substitute */
23262 if (src[*usedlen] == ':'
23263 && (src[*usedlen + 1] == 's'
23264 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23265 {
23266 char_u *str;
23267 char_u *pat;
23268 char_u *sub;
23269 int sep;
23270 char_u *flags;
23271 int didit = FALSE;
23272
23273 flags = (char_u *)"";
23274 s = src + *usedlen + 2;
23275 if (src[*usedlen + 1] == 'g')
23276 {
23277 flags = (char_u *)"g";
23278 ++s;
23279 }
23280
23281 sep = *s++;
23282 if (sep)
23283 {
23284 /* find end of pattern */
23285 p = vim_strchr(s, sep);
23286 if (p != NULL)
23287 {
23288 pat = vim_strnsave(s, (int)(p - s));
23289 if (pat != NULL)
23290 {
23291 s = p + 1;
23292 /* find end of substitution */
23293 p = vim_strchr(s, sep);
23294 if (p != NULL)
23295 {
23296 sub = vim_strnsave(s, (int)(p - s));
23297 str = vim_strnsave(*fnamep, *fnamelen);
23298 if (sub != NULL && str != NULL)
23299 {
23300 *usedlen = (int)(p + 1 - src);
23301 s = do_string_sub(str, pat, sub, flags);
23302 if (s != NULL)
23303 {
23304 *fnamep = s;
23305 *fnamelen = (int)STRLEN(s);
23306 vim_free(*bufp);
23307 *bufp = s;
23308 didit = TRUE;
23309 }
23310 }
23311 vim_free(sub);
23312 vim_free(str);
23313 }
23314 vim_free(pat);
23315 }
23316 }
23317 /* after using ":s", repeat all the modifiers */
23318 if (didit)
23319 goto repeat;
23320 }
23321 }
23322
23323 return valid;
23324}
23325
23326/*
23327 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23328 * "flags" can be "g" to do a global substitute.
23329 * Returns an allocated string, NULL for error.
23330 */
23331 char_u *
23332do_string_sub(str, pat, sub, flags)
23333 char_u *str;
23334 char_u *pat;
23335 char_u *sub;
23336 char_u *flags;
23337{
23338 int sublen;
23339 regmatch_T regmatch;
23340 int i;
23341 int do_all;
23342 char_u *tail;
23343 garray_T ga;
23344 char_u *ret;
23345 char_u *save_cpo;
23346
23347 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23348 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023349 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350
23351 ga_init2(&ga, 1, 200);
23352
23353 do_all = (flags[0] == 'g');
23354
23355 regmatch.rm_ic = p_ic;
23356 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23357 if (regmatch.regprog != NULL)
23358 {
23359 tail = str;
23360 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23361 {
23362 /*
23363 * Get some space for a temporary buffer to do the substitution
23364 * into. It will contain:
23365 * - The text up to where the match is.
23366 * - The substituted text.
23367 * - The text after the match.
23368 */
23369 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23370 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23371 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23372 {
23373 ga_clear(&ga);
23374 break;
23375 }
23376
23377 /* copy the text up to where the match is */
23378 i = (int)(regmatch.startp[0] - tail);
23379 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23380 /* add the substituted text */
23381 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23382 + ga.ga_len + i, TRUE, TRUE, FALSE);
23383 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 /* avoid getting stuck on a match with an empty string */
23385 if (tail == regmatch.endp[0])
23386 {
23387 if (*tail == NUL)
23388 break;
23389 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23390 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 }
23392 else
23393 {
23394 tail = regmatch.endp[0];
23395 if (*tail == NUL)
23396 break;
23397 }
23398 if (!do_all)
23399 break;
23400 }
23401
23402 if (ga.ga_data != NULL)
23403 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23404
23405 vim_free(regmatch.regprog);
23406 }
23407
23408 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23409 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023410 if (p_cpo == empty_option)
23411 p_cpo = save_cpo;
23412 else
23413 /* Darn, evaluating {sub} expression changed the value. */
23414 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415
23416 return ret;
23417}
23418
23419#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */