blob: 4f1be3fa2000773b52b8aed6e46efa1982ab3888 [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 {
1059 if (redir_endp != NULL && *redir_endp != NUL)
1060 /* Trailing characters are present after the variable name */
1061 EMSG(_(e_trailing));
1062 else
1063 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001064 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 var_redir_stop();
1066 return FAIL;
1067 }
1068
1069 /* check if we can write to the variable: set it to or append an empty
1070 * string */
1071 save_emsg = did_emsg;
1072 did_emsg = FALSE;
1073 tv.v_type = VAR_STRING;
1074 tv.vval.v_string = (char_u *)"";
1075 if (append)
1076 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1077 else
1078 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1079 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001080 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 if (err)
1082 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001083 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 var_redir_stop();
1085 return FAIL;
1086 }
1087 if (redir_lval->ll_newkey != NULL)
1088 {
1089 /* Dictionary item was created, don't do it again. */
1090 vim_free(redir_lval->ll_newkey);
1091 redir_lval->ll_newkey = NULL;
1092 }
1093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001098 * Append "value[value_len]" to the variable set by var_redir_start().
1099 * The actual appending is postponed until redirection ends, because the value
1100 * appended may in fact be the string we write to, changing it may cause freed
1101 * memory to be used:
1102 * :redir => foo
1103 * :let foo
1104 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 */
1106 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
1113 if (redir_lval == NULL)
1114 return;
1115
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 {
1123 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 }
1126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128}
1129
1130/*
1131 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 */
1134 void
1135var_redir_stop()
1136{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 typval_T tv;
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_lval != NULL)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* If there was no error: assign the text to the variable. */
1142 if (redir_endp != NULL)
1143 {
1144 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1145 tv.v_type = VAR_STRING;
1146 tv.vval.v_string = redir_ga.ga_data;
1147 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1148 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 /* free the collected output */
1151 vim_free(redir_ga.ga_data);
1152 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 clear_lval(redir_lval);
1155 vim_free(redir_lval);
1156 redir_lval = NULL;
1157 }
1158 vim_free(redir_varname);
1159 redir_varname = NULL;
1160}
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162# if defined(FEAT_MBYTE) || defined(PROTO)
1163 int
1164eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1165 char_u *enc_from;
1166 char_u *enc_to;
1167 char_u *fname_from;
1168 char_u *fname_to;
1169{
1170 int err = FALSE;
1171
1172 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1173 set_vim_var_string(VV_CC_TO, enc_to, -1);
1174 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1175 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1176 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1177 err = TRUE;
1178 set_vim_var_string(VV_CC_FROM, NULL, -1);
1179 set_vim_var_string(VV_CC_TO, NULL, -1);
1180 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1181 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1182
1183 if (err)
1184 return FAIL;
1185 return OK;
1186}
1187# endif
1188
1189# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1190 int
1191eval_printexpr(fname, args)
1192 char_u *fname;
1193 char_u *args;
1194{
1195 int err = FALSE;
1196
1197 set_vim_var_string(VV_FNAME_IN, fname, -1);
1198 set_vim_var_string(VV_CMDARG, args, -1);
1199 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1202 set_vim_var_string(VV_CMDARG, NULL, -1);
1203
1204 if (err)
1205 {
1206 mch_remove(fname);
1207 return FAIL;
1208 }
1209 return OK;
1210}
1211# endif
1212
1213# if defined(FEAT_DIFF) || defined(PROTO)
1214 void
1215eval_diff(origfile, newfile, outfile)
1216 char_u *origfile;
1217 char_u *newfile;
1218 char_u *outfile;
1219{
1220 int err = FALSE;
1221
1222 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1223 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1224 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1225 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1226 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1227 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1228 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1229}
1230
1231 void
1232eval_patch(origfile, difffile, outfile)
1233 char_u *origfile;
1234 char_u *difffile;
1235 char_u *outfile;
1236{
1237 int err;
1238
1239 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1240 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1241 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1242 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1243 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1244 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1245 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1246}
1247# endif
1248
1249/*
1250 * Top level evaluation function, returning a boolean.
1251 * Sets "error" to TRUE if there was an error.
1252 * Return TRUE or FALSE.
1253 */
1254 int
1255eval_to_bool(arg, error, nextcmd, skip)
1256 char_u *arg;
1257 int *error;
1258 char_u **nextcmd;
1259 int skip; /* only parse, don't execute */
1260{
Bram Moolenaar33570922005-01-25 22:26:29 +00001261 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int retval = FALSE;
1263
1264 if (skip)
1265 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 else
1269 {
1270 *error = FALSE;
1271 if (!skip)
1272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001273 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 }
1277 if (skip)
1278 --emsg_skip;
1279
1280 return retval;
1281}
1282
1283/*
1284 * Top level evaluation function, returning a string. If "skip" is TRUE,
1285 * only parsing to "nextcmd" is done, without reporting errors. Return
1286 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1287 */
1288 char_u *
1289eval_to_string_skip(arg, nextcmd, skip)
1290 char_u *arg;
1291 char_u **nextcmd;
1292 int skip; /* only parse, don't execute */
1293{
Bram Moolenaar33570922005-01-25 22:26:29 +00001294 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 char_u *retval;
1296
1297 if (skip)
1298 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 retval = NULL;
1301 else
1302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 retval = vim_strsave(get_tv_string(&tv));
1304 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 if (skip)
1307 --emsg_skip;
1308
1309 return retval;
1310}
1311
1312/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001313 * Skip over an expression at "*pp".
1314 * Return FAIL for an error, OK otherwise.
1315 */
1316 int
1317skip_expr(pp)
1318 char_u **pp;
1319{
Bram Moolenaar33570922005-01-25 22:26:29 +00001320 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001321
1322 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001323 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324}
1325
1326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001328 * When "convert" is TRUE convert a List into a sequence of lines and convert
1329 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Return pointer to allocated memory, or NULL for failure.
1331 */
1332 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *arg;
1335 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001340 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001341#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 retval = NULL;
1347 else
1348 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 {
1351 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001352 if (tv.vval.v_list != NULL)
1353 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001354 ga_append(&ga, NUL);
1355 retval = (char_u *)ga.ga_data;
1356 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357#ifdef FEAT_FLOAT
1358 else if (convert && tv.v_type == VAR_FLOAT)
1359 {
1360 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1361 retval = vim_strsave(numbuf);
1362 }
1363#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001364 else
1365 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368
1369 return retval;
1370}
1371
1372/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001373 * Call eval_to_string() without using current local variables and using
1374 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 */
1376 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001377eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *arg;
1379 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 char_u *retval;
1383 void *save_funccalp;
1384
1385 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 if (use_sandbox)
1387 ++sandbox;
1388 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 if (use_sandbox)
1391 --sandbox;
1392 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 restore_funccal(save_funccalp);
1394 return retval;
1395}
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397/*
1398 * Top level evaluation function, returning a number.
1399 * Evaluates "expr" silently.
1400 * Returns -1 for an error.
1401 */
1402 int
1403eval_to_number(expr)
1404 char_u *expr;
1405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001408 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 ++emsg_off;
1411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 retval = -1;
1414 else
1415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001416 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 --emsg_off;
1420
1421 return retval;
1422}
1423
Bram Moolenaara40058a2005-07-11 22:42:07 +00001424/*
1425 * Prepare v: variable "idx" to be used.
1426 * Save the current typeval in "save_tv".
1427 * When not used yet add the variable to the v: hashtable.
1428 */
1429 static void
1430prepare_vimvar(idx, save_tv)
1431 int idx;
1432 typval_T *save_tv;
1433{
1434 *save_tv = vimvars[idx].vv_tv;
1435 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1436 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1437}
1438
1439/*
1440 * Restore v: variable "idx" to typeval "save_tv".
1441 * When no longer defined, remove the variable from the v: hashtable.
1442 */
1443 static void
1444restore_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 hashitem_T *hi;
1449
Bram Moolenaara40058a2005-07-11 22:42:07 +00001450 vimvars[idx].vv_tv = *save_tv;
1451 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1452 {
1453 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1454 if (HASHITEM_EMPTY(hi))
1455 EMSG2(_(e_intern2), "restore_vimvar()");
1456 else
1457 hash_remove(&vimvarht, hi);
1458 }
1459}
1460
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001461#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001462/*
1463 * Evaluate an expression to a list with suggestions.
1464 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001466 */
1467 list_T *
1468eval_spell_expr(badword, expr)
1469 char_u *badword;
1470 char_u *expr;
1471{
1472 typval_T save_val;
1473 typval_T rettv;
1474 list_T *list = NULL;
1475 char_u *p = skipwhite(expr);
1476
1477 /* Set "v:val" to the bad word. */
1478 prepare_vimvar(VV_VAL, &save_val);
1479 vimvars[VV_VAL].vv_type = VAR_STRING;
1480 vimvars[VV_VAL].vv_str = badword;
1481 if (p_verbose == 0)
1482 ++emsg_off;
1483
1484 if (eval1(&p, &rettv, TRUE) == OK)
1485 {
1486 if (rettv.v_type != VAR_LIST)
1487 clear_tv(&rettv);
1488 else
1489 list = rettv.vval.v_list;
1490 }
1491
1492 if (p_verbose == 0)
1493 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001494 restore_vimvar(VV_VAL, &save_val);
1495
1496 return list;
1497}
1498
1499/*
1500 * "list" is supposed to contain two items: a word and a number. Return the
1501 * word in "pp" and the number as the return value.
1502 * Return -1 if anything isn't right.
1503 * Used to get the good word and score from the eval_spell_expr() result.
1504 */
1505 int
1506get_spellword(list, pp)
1507 list_T *list;
1508 char_u **pp;
1509{
1510 listitem_T *li;
1511
1512 li = list->lv_first;
1513 if (li == NULL)
1514 return -1;
1515 *pp = get_tv_string(&li->li_tv);
1516
1517 li = li->li_next;
1518 if (li == NULL)
1519 return -1;
1520 return get_tv_number(&li->li_tv);
1521}
1522#endif
1523
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001524/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001525 * Top level evaluation function.
1526 * Returns an allocated typval_T with the result.
1527 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001528 */
1529 typval_T *
1530eval_expr(arg, nextcmd)
1531 char_u *arg;
1532 char_u **nextcmd;
1533{
1534 typval_T *tv;
1535
1536 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001537 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538 {
1539 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001540 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541 }
1542
1543 return tv;
1544}
1545
1546
Bram Moolenaar4f688582007-07-24 12:34:30 +00001547#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1548 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 * Uses argv[argc] for the function arguments. Only Number and String
1552 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 static int
1556call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 char_u *func;
1558 int argc;
1559 char_u **argv;
1560 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
Bram Moolenaar33570922005-01-25 22:26:29 +00001563 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 long n;
1565 int len;
1566 int i;
1567 int doesrange;
1568 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001571 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 for (i = 0; i < argc; i++)
1576 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001577 /* Pass a NULL or empty argument as an empty string */
1578 if (argv[i] == NULL || *argv[i] == NUL)
1579 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001580 argvars[i].v_type = VAR_STRING;
1581 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001582 continue;
1583 }
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* Recognize a number argument, the others must be strings. */
1586 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1587 if (len != 0 && len == (int)STRLEN(argv[i]))
1588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001589 argvars[i].v_type = VAR_NUMBER;
1590 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 else
1593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001594 argvars[i].v_type = VAR_STRING;
1595 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 }
1598
1599 if (safe)
1600 {
1601 save_funccalp = save_funccal();
1602 ++sandbox;
1603 }
1604
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001605 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1606 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (safe)
1610 {
1611 --sandbox;
1612 restore_funccal(save_funccalp);
1613 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 vim_free(argvars);
1615
1616 if (ret == FAIL)
1617 clear_tv(rettv);
1618
1619 return ret;
1620}
1621
Bram Moolenaar4f688582007-07-24 12:34:30 +00001622# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001624 * Call vimL function "func" and return the result as a string.
1625 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 * Uses argv[argc] for the function arguments.
1627 */
1628 void *
1629call_func_retstr(func, argc, argv, safe)
1630 char_u *func;
1631 int argc;
1632 char_u **argv;
1633 int safe; /* use the sandbox */
1634{
1635 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001636 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637
1638 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1639 return NULL;
1640
1641 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 return retval;
1644}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001645# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646
Bram Moolenaar4f688582007-07-24 12:34:30 +00001647# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001649 * Call vimL function "func" and return the result as a number.
1650 * Returns -1 when calling the function fails.
1651 * Uses argv[argc] for the function arguments.
1652 */
1653 long
1654call_func_retnr(func, argc, argv, safe)
1655 char_u *func;
1656 int argc;
1657 char_u **argv;
1658 int safe; /* use the sandbox */
1659{
1660 typval_T rettv;
1661 long retval;
1662
1663 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1664 return -1;
1665
1666 retval = get_tv_number_chk(&rettv, NULL);
1667 clear_tv(&rettv);
1668 return retval;
1669}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001670# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001671
1672/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001673 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001675 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 */
1677 void *
1678call_func_retlist(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
1685
1686 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1687 return NULL;
1688
1689 if (rettv.v_type != VAR_LIST)
1690 {
1691 clear_tv(&rettv);
1692 return NULL;
1693 }
1694
1695 return rettv.vval.v_list;
1696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698
Bram Moolenaar4f688582007-07-24 12:34:30 +00001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Save the current function call pointer, and set it to NULL.
1702 * Used when executing autocommands and for ":source".
1703 */
1704 void *
1705save_funccal()
1706{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001707 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 current_funccal = NULL;
1710 return (void *)fc;
1711}
1712
1713 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714restore_funccal(vfc)
1715 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717 funccall_T *fc = (funccall_T *)vfc;
1718
1719 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720}
1721
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722#if defined(FEAT_PROFILE) || defined(PROTO)
1723/*
1724 * Prepare profiling for entering a child or something else that is not
1725 * counted for the script/function itself.
1726 * Should always be called in pair with prof_child_exit().
1727 */
1728 void
1729prof_child_enter(tm)
1730 proftime_T *tm; /* place to store waittime */
1731{
1732 funccall_T *fc = current_funccal;
1733
1734 if (fc != NULL && fc->func->uf_profiling)
1735 profile_start(&fc->prof_child);
1736 script_prof_save(tm);
1737}
1738
1739/*
1740 * Take care of time spent in a child.
1741 * Should always be called after prof_child_enter().
1742 */
1743 void
1744prof_child_exit(tm)
1745 proftime_T *tm; /* where waittime was stored */
1746{
1747 funccall_T *fc = current_funccal;
1748
1749 if (fc != NULL && fc->func->uf_profiling)
1750 {
1751 profile_end(&fc->prof_child);
1752 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1753 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1754 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1755 }
1756 script_prof_restore(tm);
1757}
1758#endif
1759
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#ifdef FEAT_FOLDING
1762/*
1763 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1764 * it in "*cp". Doesn't give error messages.
1765 */
1766 int
1767eval_foldexpr(arg, cp)
1768 char_u *arg;
1769 int *cp;
1770{
Bram Moolenaar33570922005-01-25 22:26:29 +00001771 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 int retval;
1773 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001774 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1775 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001778 if (use_sandbox)
1779 ++sandbox;
1780 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 retval = 0;
1784 else
1785 {
1786 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 if (tv.v_type == VAR_NUMBER)
1788 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001789 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a string, check if there is a non-digit before
1794 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (!VIM_ISDIGIT(*s) && *s != '-')
1797 *cp = *s++;
1798 retval = atol((char *)s);
1799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 --sandbox;
1805 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 return retval;
1808}
1809#endif
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 * ":let" list all variable values
1813 * ":let var1 var2" list variable values
1814 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 * ":let var += expr" assignment command.
1816 * ":let var -= expr" assignment command.
1817 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 */
1820 void
1821ex_let(eap)
1822 exarg_T *eap;
1823{
1824 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001826 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 int var_count = 0;
1829 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001831 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001832 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 argend = skip_var_list(arg, &var_count, &semicolon);
1835 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001837 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1838 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001839 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001842 /*
1843 * ":let" without "=": list variables
1844 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 if (*arg == '[')
1846 EMSG(_(e_invarg));
1847 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 list_glob_vars(&first);
1854 list_buf_vars(&first);
1855 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001856#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001858#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_script_vars(&first);
1860 list_func_vars(&first);
1861 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 eap->nextcmd = check_nextcmd(arg);
1864 }
1865 else
1866 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 op[0] = '=';
1868 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001869 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 {
1871 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1872 op[0] = expr[-1]; /* +=, -= or .= */
1873 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (eap->skip)
1877 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (eap->skip)
1880 {
1881 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 --emsg_skip;
1884 }
1885 else if (i != FAIL)
1886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891 }
1892}
1893
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894/*
1895 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1896 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 * When "nextchars" is not NULL it points to a string with characters that
1898 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1899 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 * Returns OK or FAIL;
1901 */
1902 static int
1903ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1904 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 int copy; /* copy values from "tv", don't move */
1907 int semicolon; /* from skip_var_list() */
1908 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910{
1911 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 listitem_T *item;
1915 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916
1917 if (*arg != '[')
1918 {
1919 /*
1920 * ":let var = expr" or ":for var in list"
1921 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 return FAIL;
1924 return OK;
1925 }
1926
1927 /*
1928 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1929 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001930 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 {
1932 EMSG(_(e_listreq));
1933 return FAIL;
1934 }
1935
1936 i = list_len(l);
1937 if (semicolon == 0 && var_count < i)
1938 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001939 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 return FAIL;
1941 }
1942 if (var_count - semicolon > i)
1943 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001944 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return FAIL;
1946 }
1947
1948 item = l->lv_first;
1949 while (*arg != ']')
1950 {
1951 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 item = item->li_next;
1954 if (arg == NULL)
1955 return FAIL;
1956
1957 arg = skipwhite(arg);
1958 if (*arg == ';')
1959 {
1960 /* Put the rest of the list (may be empty) in the var after ';'.
1961 * Create a new list for this. */
1962 l = list_alloc();
1963 if (l == NULL)
1964 return FAIL;
1965 while (item != NULL)
1966 {
1967 list_append_tv(l, &item->li_tv);
1968 item = item->li_next;
1969 }
1970
1971 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001972 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 ltv.vval.v_list = l;
1974 l->lv_refcount = 1;
1975
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1977 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 clear_tv(&ltv);
1979 if (arg == NULL)
1980 return FAIL;
1981 break;
1982 }
1983 else if (*arg != ',' && *arg != ']')
1984 {
1985 EMSG2(_(e_intern2), "ex_let_vars()");
1986 return FAIL;
1987 }
1988 }
1989
1990 return OK;
1991}
1992
1993/*
1994 * Skip over assignable variable "var" or list of variables "[var, var]".
1995 * Used for ":let varvar = expr" and ":for varvar in expr".
1996 * For "[var, var]" increment "*var_count" for each variable.
1997 * for "[var, var; var]" set "semicolon".
1998 * Return NULL for an error.
1999 */
2000 static char_u *
2001skip_var_list(arg, var_count, semicolon)
2002 char_u *arg;
2003 int *var_count;
2004 int *semicolon;
2005{
2006 char_u *p, *s;
2007
2008 if (*arg == '[')
2009 {
2010 /* "[var, var]": find the matching ']'. */
2011 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002012 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 {
2014 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2015 s = skip_var_one(p);
2016 if (s == p)
2017 {
2018 EMSG2(_(e_invarg2), p);
2019 return NULL;
2020 }
2021 ++*var_count;
2022
2023 p = skipwhite(s);
2024 if (*p == ']')
2025 break;
2026 else if (*p == ';')
2027 {
2028 if (*semicolon == 1)
2029 {
2030 EMSG(_("Double ; in list of variables"));
2031 return NULL;
2032 }
2033 *semicolon = 1;
2034 }
2035 else if (*p != ',')
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 }
2041 return p + 1;
2042 }
2043 else
2044 return skip_var_one(arg);
2045}
2046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002047/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002048 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002049 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 static char_u *
2052skip_var_one(arg)
2053 char_u *arg;
2054{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 if (*arg == '@' && arg[1] != NUL)
2056 return arg + 2;
2057 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2058 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059}
2060
Bram Moolenaara7043832005-01-21 11:56:39 +00002061/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002062 * List variables for hashtab "ht" with prefix "prefix".
2063 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002066list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002067 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002068 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002070 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002071{
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 hashitem_T *hi;
2073 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 int todo;
2075
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002076 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2078 {
2079 if (!HASHITEM_EMPTY(hi))
2080 {
2081 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 di = HI2DI(hi);
2083 if (empty || di->di_tv.v_type != VAR_STRING
2084 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
2087 }
2088}
2089
2090/*
2091 * List global variables.
2092 */
2093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_glob_vars(first)
2095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098}
2099
2100/*
2101 * List buffer variables.
2102 */
2103 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104list_buf_vars(first)
2105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 char_u numbuf[NUMBUFLEN];
2108
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2110 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002111
2112 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2114 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002115}
2116
2117/*
2118 * List window variables.
2119 */
2120 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121list_win_vars(first)
2122 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2125 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002128#ifdef FEAT_WINDOWS
2129/*
2130 * List tab page variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_tab_vars(first)
2134 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2137 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138}
2139#endif
2140
Bram Moolenaara7043832005-01-21 11:56:39 +00002141/*
2142 * List Vim variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_vim_vars(first)
2146 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149}
2150
2151/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002152 * List script-local variables, if there is a script.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_script_vars(first)
2156 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157{
2158 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2160 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002161}
2162
2163/*
2164 * List function variables, if there is a function.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_func_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_funccal != NULL)
2171 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 * List variables in "arg".
2177 */
2178 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 exarg_T *eap;
2181 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183{
2184 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002185 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187 char_u *name_start;
2188 char_u *arg_subsc;
2189 char_u *tofree;
2190 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191
2192 while (!ends_excmd(*arg) && !got_int)
2193 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002196 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2198 {
2199 emsg_severe = TRUE;
2200 EMSG(_(e_trailing));
2201 break;
2202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 /* get_name_len() takes care of expanding curly braces */
2207 name_start = name = arg;
2208 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2209 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 /* This is mainly to keep test 49 working: when expanding
2212 * curly braces fails overrule the exception error message. */
2213 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 emsg_severe = TRUE;
2216 EMSG2(_(e_invarg2), arg);
2217 break;
2218 }
2219 error = TRUE;
2220 }
2221 else
2222 {
2223 if (tofree != NULL)
2224 name = tofree;
2225 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 else
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 /* handle d.key, l[idx], f(expr) */
2230 arg_subsc = arg;
2231 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002239 case 'g': list_glob_vars(first); break;
2240 case 'b': list_buf_vars(first); break;
2241 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002243 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'v': list_vim_vars(first); break;
2246 case 's': list_script_vars(first); break;
2247 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 default:
2249 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 }
2252 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 {
2254 char_u numbuf[NUMBUFLEN];
2255 char_u *tf;
2256 int c;
2257 char_u *s;
2258
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002259 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 c = *arg;
2261 *arg = NUL;
2262 list_one_var_a((char_u *)"",
2263 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 tv.v_type,
2265 s == NULL ? (char_u *)"" : s,
2266 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 *arg = c;
2268 vim_free(tf);
2269 }
2270 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
2273 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274
2275 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277
2278 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280
2281 return arg;
2282}
2283
2284/*
2285 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2286 * Returns a pointer to the char just after the var name.
2287 * Returns NULL if there is an error.
2288 */
2289 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002290ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002292 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002294 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296{
2297 int c1;
2298 char_u *name;
2299 char_u *p;
2300 char_u *arg_end = NULL;
2301 int len;
2302 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304
2305 /*
2306 * ":let $VAR = expr": Set environment variable.
2307 */
2308 if (*arg == '$')
2309 {
2310 /* Find the end of the name. */
2311 ++arg;
2312 name = arg;
2313 len = get_env_len(&arg);
2314 if (len == 0)
2315 EMSG2(_(e_invarg2), name - 1);
2316 else
2317 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 if (op != NULL && (*op == '+' || *op == '-'))
2319 EMSG2(_(e_letwrong), op);
2320 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002321 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 EMSG(_(e_letunexp));
2323 else
2324 {
2325 c1 = name[len];
2326 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002327 p = get_tv_string_chk(tv);
2328 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 {
2330 int mustfree = FALSE;
2331 char_u *s = vim_getenv(name, &mustfree);
2332
2333 if (s != NULL)
2334 {
2335 p = tofree = concat_str(s, p);
2336 if (mustfree)
2337 vim_free(s);
2338 }
2339 }
2340 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002341 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 if (STRICMP(name, "HOME") == 0)
2344 init_homedir();
2345 else if (didset_vim && STRICMP(name, "VIM") == 0)
2346 didset_vim = FALSE;
2347 else if (didset_vimruntime
2348 && STRICMP(name, "VIMRUNTIME") == 0)
2349 didset_vimruntime = FALSE;
2350 arg_end = arg;
2351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 }
2355 }
2356 }
2357
2358 /*
2359 * ":let &option = expr": Set option value.
2360 * ":let &l:option = expr": Set local option value.
2361 * ":let &g:option = expr": Set global option value.
2362 */
2363 else if (*arg == '&')
2364 {
2365 /* Find the end of the name. */
2366 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002367 if (p == NULL || (endchars != NULL
2368 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 EMSG(_(e_letunexp));
2370 else
2371 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 long n;
2373 int opt_type;
2374 long numval;
2375 char_u *stringval = NULL;
2376 char_u *s;
2377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 c1 = *p;
2379 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380
2381 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 s = get_tv_string_chk(tv); /* != NULL if number or string */
2383 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 {
2385 opt_type = get_option_value(arg, &numval,
2386 &stringval, opt_flags);
2387 if ((opt_type == 1 && *op == '.')
2388 || (opt_type == 0 && *op != '.'))
2389 EMSG2(_(e_letwrong), op);
2390 else
2391 {
2392 if (opt_type == 1) /* number */
2393 {
2394 if (*op == '+')
2395 n = numval + n;
2396 else
2397 n = numval - n;
2398 }
2399 else if (opt_type == 0 && stringval != NULL) /* string */
2400 {
2401 s = concat_str(stringval, s);
2402 vim_free(stringval);
2403 stringval = s;
2404 }
2405 }
2406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 if (s != NULL)
2408 {
2409 set_option_value(arg, n, s, opt_flags);
2410 arg_end = p;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 }
2415 }
2416
2417 /*
2418 * ":let @r = expr": Set register contents.
2419 */
2420 else if (*arg == '@')
2421 {
2422 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 if (op != NULL && (*op == '+' || *op == '-'))
2424 EMSG2(_(e_letwrong), op);
2425 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 EMSG(_(e_letunexp));
2428 else
2429 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002430 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 char_u *s;
2432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 p = get_tv_string_chk(tv);
2434 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002436 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 if (s != NULL)
2438 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002439 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 vim_free(s);
2441 }
2442 }
2443 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 arg_end = arg + 1;
2447 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002448 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002456 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002458 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002460 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2464 EMSG(_(e_letunexp));
2465 else
2466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 arg_end = p;
2469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 }
2473
2474 else
2475 EMSG2(_(e_invarg2), arg);
2476
2477 return arg_end;
2478}
2479
2480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2482 */
2483 static int
2484check_changedtick(arg)
2485 char_u *arg;
2486{
2487 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2488 {
2489 EMSG2(_(e_readonlyvar), arg);
2490 return TRUE;
2491 }
2492 return FALSE;
2493}
2494
2495/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 * Get an lval: variable, Dict item or List item that can be assigned a value
2497 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2498 * "name.key", "name.key[expr]" etc.
2499 * Indexing only works if "name" is an existing List or Dictionary.
2500 * "name" points to the start of the name.
2501 * If "rettv" is not NULL it points to the value to be assigned.
2502 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2503 * wrong; must end in space or cmd separator.
2504 *
2505 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002506 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 */
2509 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002510get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002512 typval_T *rettv;
2513 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 int unlet;
2515 int skip;
2516 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 char_u *expr_start, *expr_end;
2521 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 dictitem_T *v;
2523 typval_T var1;
2524 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533
2534 if (skip)
2535 {
2536 /* When skipping just find the end of the name. */
2537 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 }
2540
2541 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (expr_start != NULL)
2544 {
2545 /* Don't expand the name when we already know there is an error. */
2546 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2547 && *p != '[' && *p != '.')
2548 {
2549 EMSG(_(e_trailing));
2550 return NULL;
2551 }
2552
2553 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2554 if (lp->ll_exp_name == NULL)
2555 {
2556 /* Report an invalid expression in braces, unless the
2557 * expression evaluation has been cancelled due to an
2558 * aborting error, an interrupt, or an exception. */
2559 if (!aborting() && !quiet)
2560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002561 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 EMSG2(_(e_invarg2), name);
2563 return NULL;
2564 }
2565 }
2566 lp->ll_name = lp->ll_exp_name;
2567 }
2568 else
2569 lp->ll_name = name;
2570
2571 /* Without [idx] or .key we are done. */
2572 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2573 return p;
2574
2575 cc = *p;
2576 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002577 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (v == NULL && !quiet)
2579 EMSG2(_(e_undefvar), lp->ll_name);
2580 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 if (v == NULL)
2582 return NULL;
2583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /*
2585 * Loop until no more [idx] or .key is following.
2586 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2591 && !(lp->ll_tv->v_type == VAR_DICT
2592 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (!quiet)
2595 EMSG(_("E689: Can only index a List or Dictionary"));
2596 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E708: [:] must come last"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002604
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 len = -1;
2606 if (*p == '.')
2607 {
2608 key = p + 1;
2609 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2610 ;
2611 if (len == 0)
2612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSG(_(e_emptykey));
2615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 }
2617 p = key + len;
2618 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619 else
2620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 if (*p == ':')
2624 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 empty1 = FALSE;
2628 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002630 if (get_tv_string_chk(&var1) == NULL)
2631 {
2632 /* not a number or string */
2633 clear_tv(&var1);
2634 return NULL;
2635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 }
2637
2638 /* Optionally get the second index [ :expr]. */
2639 if (*p == ':')
2640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002645 if (!empty1)
2646 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (rettv != NULL && (rettv->v_type != VAR_LIST
2650 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 if (!empty1)
2655 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658 p = skipwhite(p + 1);
2659 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 else
2662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var2) == NULL)
2671 {
2672 /* not a number or string */
2673 if (!empty1)
2674 clear_tv(&var1);
2675 clear_tv(&var2);
2676 return NULL;
2677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (!empty1)
2689 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
2694
2695 /* Skip to past ']'. */
2696 ++p;
2697 }
2698
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 {
2701 if (len == -1)
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (*key == NUL)
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
2708 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
2712 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 lp->ll_list = NULL;
2714 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002715 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002718 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (len == -1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (len == -1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 p = NULL;
2735 break;
2736 }
2737 if (len == -1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
2741 else
2742 {
2743 /*
2744 * Get the number and item for the only or first index of the List.
2745 */
2746 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 else
2749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002750 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 clear_tv(&var1);
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_list = lp->ll_tv->vval.v_list;
2755 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2756 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002758 if (lp->ll_n1 < 0)
2759 {
2760 lp->ll_n1 = 0;
2761 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2762 }
2763 }
2764 if (lp->ll_li == NULL)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 }
2770
2771 /*
2772 * May need to find the item or absolute index for the second
2773 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 * When no index given: "lp->ll_empty2" is TRUE.
2775 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 }
2788
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2790 if (lp->ll_n1 < 0)
2791 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2792 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002794 }
2795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return p;
2801}
2802
2803/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 */
2806 static void
2807clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002808 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809{
2810 vim_free(lp->ll_exp_name);
2811 vim_free(lp->ll_newkey);
2812}
2813
2814/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002815 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 */
2819 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002823 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826{
2827 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 listitem_T *ri;
2829 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830
2831 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002834 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 cc = *endp;
2836 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 if (op != NULL && *op != '=')
2838 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002839 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002841 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002842 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002843 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 {
2845 if (tv_op(&tv, rettv, op) == OK)
2846 set_var(lp->ll_name, &tv, FALSE);
2847 clear_tv(&tv);
2848 }
2849 }
2850 else
2851 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002853 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002855 else if (tv_check_lock(lp->ll_newkey == NULL
2856 ? lp->ll_tv->v_lock
2857 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2858 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 else if (lp->ll_range)
2860 {
2861 /*
2862 * Assign the List values to the list items.
2863 */
2864 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002866 if (op != NULL && *op != '=')
2867 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2868 else
2869 {
2870 clear_tv(&lp->ll_li->li_tv);
2871 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2872 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 ri = ri->li_next;
2874 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2875 break;
2876 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002879 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 ri = NULL;
2882 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_li = lp->ll_li->li_next;
2886 ++lp->ll_n1;
2887 }
2888 if (ri != NULL)
2889 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890 else if (lp->ll_empty2
2891 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 : lp->ll_n1 != lp->ll_n2)
2893 EMSG(_("E711: List value has not enough items"));
2894 }
2895 else
2896 {
2897 /*
2898 * Assign to a List or Dictionary item.
2899 */
2900 if (lp->ll_newkey != NULL)
2901 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 if (op != NULL && *op != '=')
2903 {
2904 EMSG2(_(e_letwrong), op);
2905 return;
2906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002909 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (di == NULL)
2911 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002912 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2913 {
2914 vim_free(di);
2915 return;
2916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002918 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 else if (op != NULL && *op != '=')
2920 {
2921 tv_op(lp->ll_tv, rettv, op);
2922 return;
2923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002926
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 /*
2928 * Assign the value to the variable or list item.
2929 */
2930 if (copy)
2931 copy_tv(rettv, lp->ll_tv);
2932 else
2933 {
2934 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002935 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 }
2938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939}
2940
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2943 * Returns OK or FAIL.
2944 */
2945 static int
2946tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 typval_T *tv1;
2948 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 char_u *op;
2950{
2951 long n;
2952 char_u numbuf[NUMBUFLEN];
2953 char_u *s;
2954
2955 /* Can't do anything with a Funcref or a Dict on the right. */
2956 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2957 {
2958 switch (tv1->v_type)
2959 {
2960 case VAR_DICT:
2961 case VAR_FUNC:
2962 break;
2963
2964 case VAR_LIST:
2965 if (*op != '+' || tv2->v_type != VAR_LIST)
2966 break;
2967 /* List += List */
2968 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2969 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2970 return OK;
2971
2972 case VAR_NUMBER:
2973 case VAR_STRING:
2974 if (tv2->v_type == VAR_LIST)
2975 break;
2976 if (*op == '+' || *op == '-')
2977 {
2978 /* nr += nr or nr -= nr*/
2979 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980#ifdef FEAT_FLOAT
2981 if (tv2->v_type == VAR_FLOAT)
2982 {
2983 float_T f = n;
2984
2985 if (*op == '+')
2986 f += tv2->vval.v_float;
2987 else
2988 f -= tv2->vval.v_float;
2989 clear_tv(tv1);
2990 tv1->v_type = VAR_FLOAT;
2991 tv1->vval.v_float = f;
2992 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994#endif
2995 {
2996 if (*op == '+')
2997 n += get_tv_number(tv2);
2998 else
2999 n -= get_tv_number(tv2);
3000 clear_tv(tv1);
3001 tv1->v_type = VAR_NUMBER;
3002 tv1->vval.v_number = n;
3003 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 }
3005 else
3006 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007 if (tv2->v_type == VAR_FLOAT)
3008 break;
3009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 /* str .= str */
3011 s = get_tv_string(tv1);
3012 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3013 clear_tv(tv1);
3014 tv1->v_type = VAR_STRING;
3015 tv1->vval.v_string = s;
3016 }
3017 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018
3019#ifdef FEAT_FLOAT
3020 case VAR_FLOAT:
3021 {
3022 float_T f;
3023
3024 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3025 && tv2->v_type != VAR_NUMBER
3026 && tv2->v_type != VAR_STRING))
3027 break;
3028 if (tv2->v_type == VAR_FLOAT)
3029 f = tv2->vval.v_float;
3030 else
3031 f = get_tv_number(tv2);
3032 if (*op == '+')
3033 tv1->vval.v_float += f;
3034 else
3035 tv1->vval.v_float -= f;
3036 }
3037 return OK;
3038#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 }
3040 }
3041
3042 EMSG2(_(e_letwrong), op);
3043 return FAIL;
3044}
3045
3046/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003047 * Add a watcher to a list.
3048 */
3049 static void
3050list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 list_T *l;
3052 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053{
3054 lw->lw_next = l->lv_watch;
3055 l->lv_watch = lw;
3056}
3057
3058/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003059 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003060 * No warning when it isn't found...
3061 */
3062 static void
3063list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 list_T *l;
3065 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066{
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068
3069 lwp = &l->lv_watch;
3070 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3071 {
3072 if (lw == lwrem)
3073 {
3074 *lwp = lw->lw_next;
3075 break;
3076 }
3077 lwp = &lw->lw_next;
3078 }
3079}
3080
3081/*
3082 * Just before removing an item from a list: advance watchers to the next
3083 * item.
3084 */
3085 static void
3086list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 list_T *l;
3088 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089{
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091
3092 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3093 if (lw->lw_item == item)
3094 lw->lw_item = item->li_next;
3095}
3096
3097/*
3098 * Evaluate the expression used in a ":for var in expr" command.
3099 * "arg" points to "var".
3100 * Set "*errp" to TRUE for an error, FALSE otherwise;
3101 * Return a pointer that holds the info. Null when there is an error.
3102 */
3103 void *
3104eval_for_line(arg, errp, nextcmdp, skip)
3105 char_u *arg;
3106 int *errp;
3107 char_u **nextcmdp;
3108 int skip;
3109{
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 typval_T tv;
3113 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114
3115 *errp = TRUE; /* default: there is an error */
3116
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 if (fi == NULL)
3119 return NULL;
3120
3121 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3122 if (expr == NULL)
3123 return fi;
3124
3125 expr = skipwhite(expr);
3126 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3127 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003128 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129 return fi;
3130 }
3131
3132 if (skip)
3133 ++emsg_skip;
3134 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3135 {
3136 *errp = FALSE;
3137 if (!skip)
3138 {
3139 l = tv.vval.v_list;
3140 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003141 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003143 clear_tv(&tv);
3144 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 else
3146 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003147 /* No need to increment the refcount, it's already set for the
3148 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149 fi->fi_list = l;
3150 list_add_watch(l, &fi->fi_lw);
3151 fi->fi_lw.lw_item = l->lv_first;
3152 }
3153 }
3154 }
3155 if (skip)
3156 --emsg_skip;
3157
3158 return fi;
3159}
3160
3161/*
3162 * Use the first item in a ":for" list. Advance to the next.
3163 * Assign the values to the variable (list). "arg" points to the first one.
3164 * Return TRUE when a valid item was found, FALSE when at end of list or
3165 * something wrong.
3166 */
3167 int
3168next_for_item(fi_void, arg)
3169 void *fi_void;
3170 char_u *arg;
3171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175
3176 item = fi->fi_lw.lw_item;
3177 if (item == NULL)
3178 result = FALSE;
3179 else
3180 {
3181 fi->fi_lw.lw_item = item->li_next;
3182 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3183 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3184 }
3185 return result;
3186}
3187
3188/*
3189 * Free the structure used to store info used by ":for".
3190 */
3191 void
3192free_for_info(fi_void)
3193 void *fi_void;
3194{
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003197 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 list_unref(fi->fi_list);
3201 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 vim_free(fi);
3203}
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3206
3207 void
3208set_context_for_expression(xp, arg, cmdidx)
3209 expand_T *xp;
3210 char_u *arg;
3211 cmdidx_T cmdidx;
3212{
3213 int got_eq = FALSE;
3214 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 if (cmdidx == CMD_let)
3218 {
3219 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003220 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 {
3222 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003223 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 {
3225 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 if (vim_iswhite(*p))
3228 break;
3229 }
3230 return;
3231 }
3232 }
3233 else
3234 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3235 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 while ((xp->xp_pattern = vim_strpbrk(arg,
3237 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3238 {
3239 c = *xp->xp_pattern;
3240 if (c == '&')
3241 {
3242 c = xp->xp_pattern[1];
3243 if (c == '&')
3244 {
3245 ++xp->xp_pattern;
3246 xp->xp_context = cmdidx != CMD_let || got_eq
3247 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3248 }
3249 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003252 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3253 xp->xp_pattern += 2;
3254
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 else if (c == '$')
3258 {
3259 /* environment variable */
3260 xp->xp_context = EXPAND_ENV_VARS;
3261 }
3262 else if (c == '=')
3263 {
3264 got_eq = TRUE;
3265 xp->xp_context = EXPAND_EXPRESSION;
3266 }
3267 else if (c == '<'
3268 && xp->xp_context == EXPAND_FUNCTIONS
3269 && vim_strchr(xp->xp_pattern, '(') == NULL)
3270 {
3271 /* Function name can start with "<SNR>" */
3272 break;
3273 }
3274 else if (cmdidx != CMD_let || got_eq)
3275 {
3276 if (c == '"') /* string */
3277 {
3278 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3279 if (c == '\\' && xp->xp_pattern[1] != NUL)
3280 ++xp->xp_pattern;
3281 xp->xp_context = EXPAND_NOTHING;
3282 }
3283 else if (c == '\'') /* literal string */
3284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003285 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3287 /* skip */ ;
3288 xp->xp_context = EXPAND_NOTHING;
3289 }
3290 else if (c == '|')
3291 {
3292 if (xp->xp_pattern[1] == '|')
3293 {
3294 ++xp->xp_pattern;
3295 xp->xp_context = EXPAND_EXPRESSION;
3296 }
3297 else
3298 xp->xp_context = EXPAND_COMMANDS;
3299 }
3300 else
3301 xp->xp_context = EXPAND_EXPRESSION;
3302 }
3303 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 /* Doesn't look like something valid, expand as an expression
3305 * anyway. */
3306 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 arg = xp->xp_pattern;
3308 if (*arg != NUL)
3309 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3310 /* skip */ ;
3311 }
3312 xp->xp_pattern = arg;
3313}
3314
3315#endif /* FEAT_CMDL_COMPL */
3316
3317/*
3318 * ":1,25call func(arg1, arg2)" function call.
3319 */
3320 void
3321ex_call(eap)
3322 exarg_T *eap;
3323{
3324 char_u *arg = eap->arg;
3325 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 linenr_T lnum;
3331 int doesrange;
3332 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003333 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003335 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003336 if (fudi.fd_newkey != NULL)
3337 {
3338 /* Still need to give an error message for missing key. */
3339 EMSG2(_(e_dictkey), fudi.fd_newkey);
3340 vim_free(fudi.fd_newkey);
3341 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003342 if (tofree == NULL)
3343 return;
3344
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003345 /* Increase refcount on dictionary, it could get deleted when evaluating
3346 * the arguments. */
3347 if (fudi.fd_dict != NULL)
3348 ++fudi.fd_dict->dv_refcount;
3349
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003350 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003351 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003352 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar532c7802005-01-27 14:44:31 +00003354 /* Skip white space to allow ":call func ()". Not good, but required for
3355 * backward compatibility. */
3356 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 if (*startarg != '(')
3360 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003361 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 goto end;
3363 }
3364
3365 /*
3366 * When skipping, evaluate the function once, to find the end of the
3367 * arguments.
3368 * When the function takes a range, this is discovered after the first
3369 * call, and the loop is broken.
3370 */
3371 if (eap->skip)
3372 {
3373 ++emsg_skip;
3374 lnum = eap->line2; /* do it once, also with an invalid range */
3375 }
3376 else
3377 lnum = eap->line1;
3378 for ( ; lnum <= eap->line2; ++lnum)
3379 {
3380 if (!eap->skip && eap->addr_count > 0)
3381 {
3382 curwin->w_cursor.lnum = lnum;
3383 curwin->w_cursor.col = 0;
3384 }
3385 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003386 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003387 eap->line1, eap->line2, &doesrange,
3388 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
3390 failed = TRUE;
3391 break;
3392 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003393
3394 /* Handle a function returning a Funcref, Dictionary or List. */
3395 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3396 {
3397 failed = TRUE;
3398 break;
3399 }
3400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (doesrange || eap->skip)
3403 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003406 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003408 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (aborting())
3410 break;
3411 }
3412 if (eap->skip)
3413 --emsg_skip;
3414
3415 if (!failed)
3416 {
3417 /* Check for trailing illegal characters and a following command. */
3418 if (!ends_excmd(*arg))
3419 {
3420 emsg_severe = TRUE;
3421 EMSG(_(e_trailing));
3422 }
3423 else
3424 eap->nextcmd = check_nextcmd(arg);
3425 }
3426
3427end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003429 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430}
3431
3432/*
3433 * ":unlet[!] var1 ... " command.
3434 */
3435 void
3436ex_unlet(eap)
3437 exarg_T *eap;
3438{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003439 ex_unletlock(eap, eap->arg, 0);
3440}
3441
3442/*
3443 * ":lockvar" and ":unlockvar" commands
3444 */
3445 void
3446ex_lockvar(eap)
3447 exarg_T *eap;
3448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003450 int deep = 2;
3451
3452 if (eap->forceit)
3453 deep = -1;
3454 else if (vim_isdigit(*arg))
3455 {
3456 deep = getdigits(&arg);
3457 arg = skipwhite(arg);
3458 }
3459
3460 ex_unletlock(eap, arg, deep);
3461}
3462
3463/*
3464 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3465 */
3466 static void
3467ex_unletlock(eap, argstart, deep)
3468 exarg_T *eap;
3469 char_u *argstart;
3470 int deep;
3471{
3472 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003475 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477 do
3478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003480 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3481 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003482 if (lv.ll_name == NULL)
3483 error = TRUE; /* error but continue parsing */
3484 if (name_end == NULL || (!vim_iswhite(*name_end)
3485 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 if (name_end != NULL)
3488 {
3489 emsg_severe = TRUE;
3490 EMSG(_(e_trailing));
3491 }
3492 if (!(eap->skip || error))
3493 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
3495 }
3496
3497 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003498 {
3499 if (eap->cmdidx == CMD_unlet)
3500 {
3501 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3502 error = TRUE;
3503 }
3504 else
3505 {
3506 if (do_lock_var(&lv, name_end, deep,
3507 eap->cmdidx == CMD_lockvar) == FAIL)
3508 error = TRUE;
3509 }
3510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003512 if (!eap->skip)
3513 clear_lval(&lv);
3514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 arg = skipwhite(name_end);
3516 } while (!ends_excmd(*arg));
3517
3518 eap->nextcmd = check_nextcmd(arg);
3519}
3520
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003522do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003525 int forceit;
3526{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003527 int ret = OK;
3528 int cc;
3529
3530 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 cc = *name_end;
3533 *name_end = NUL;
3534
3535 /* Normal name or expanded name. */
3536 if (check_changedtick(lp->ll_name))
3537 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003538 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003541 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003542 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3543 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 else if (lp->ll_range)
3545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003547
3548 /* Delete a range of List items. */
3549 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3550 {
3551 li = lp->ll_li->li_next;
3552 listitem_remove(lp->ll_list, lp->ll_li);
3553 lp->ll_li = li;
3554 ++lp->ll_n1;
3555 }
3556 }
3557 else
3558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 /* unlet a List item. */
3561 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 }
3566
3567 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568}
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570/*
3571 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 */
3574 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 hashtab_T *ht;
3580 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003582 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaar33570922005-01-25 22:26:29 +00003584 ht = find_var_ht(name, &varname);
3585 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003587 hi = hash_find(ht, varname);
3588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003589 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003590 di = HI2DI(hi);
3591 if (var_check_fixed(di->di_flags, name)
3592 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 return FAIL;
3594 delete_var(ht, hi);
3595 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 if (forceit)
3599 return OK;
3600 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return FAIL;
3602}
3603
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604/*
3605 * Lock or unlock variable indicated by "lp".
3606 * "deep" is the levels to go (-1 for unlimited);
3607 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3608 */
3609 static int
3610do_lock_var(lp, name_end, deep, lock)
3611 lval_T *lp;
3612 char_u *name_end;
3613 int deep;
3614 int lock;
3615{
3616 int ret = OK;
3617 int cc;
3618 dictitem_T *di;
3619
3620 if (deep == 0) /* nothing to do */
3621 return OK;
3622
3623 if (lp->ll_tv == NULL)
3624 {
3625 cc = *name_end;
3626 *name_end = NUL;
3627
3628 /* Normal name or expanded name. */
3629 if (check_changedtick(lp->ll_name))
3630 ret = FAIL;
3631 else
3632 {
3633 di = find_var(lp->ll_name, NULL);
3634 if (di == NULL)
3635 ret = FAIL;
3636 else
3637 {
3638 if (lock)
3639 di->di_flags |= DI_FLAGS_LOCK;
3640 else
3641 di->di_flags &= ~DI_FLAGS_LOCK;
3642 item_lock(&di->di_tv, deep, lock);
3643 }
3644 }
3645 *name_end = cc;
3646 }
3647 else if (lp->ll_range)
3648 {
3649 listitem_T *li = lp->ll_li;
3650
3651 /* (un)lock a range of List items. */
3652 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3653 {
3654 item_lock(&li->li_tv, deep, lock);
3655 li = li->li_next;
3656 ++lp->ll_n1;
3657 }
3658 }
3659 else if (lp->ll_list != NULL)
3660 /* (un)lock a List item. */
3661 item_lock(&lp->ll_li->li_tv, deep, lock);
3662 else
3663 /* un(lock) a Dictionary item. */
3664 item_lock(&lp->ll_di->di_tv, deep, lock);
3665
3666 return ret;
3667}
3668
3669/*
3670 * Lock or unlock an item. "deep" is nr of levels to go.
3671 */
3672 static void
3673item_lock(tv, deep, lock)
3674 typval_T *tv;
3675 int deep;
3676 int lock;
3677{
3678 static int recurse = 0;
3679 list_T *l;
3680 listitem_T *li;
3681 dict_T *d;
3682 hashitem_T *hi;
3683 int todo;
3684
3685 if (recurse >= DICT_MAXNEST)
3686 {
3687 EMSG(_("E743: variable nested too deep for (un)lock"));
3688 return;
3689 }
3690 if (deep == 0)
3691 return;
3692 ++recurse;
3693
3694 /* lock/unlock the item itself */
3695 if (lock)
3696 tv->v_lock |= VAR_LOCKED;
3697 else
3698 tv->v_lock &= ~VAR_LOCKED;
3699
3700 switch (tv->v_type)
3701 {
3702 case VAR_LIST:
3703 if ((l = tv->vval.v_list) != NULL)
3704 {
3705 if (lock)
3706 l->lv_lock |= VAR_LOCKED;
3707 else
3708 l->lv_lock &= ~VAR_LOCKED;
3709 if (deep < 0 || deep > 1)
3710 /* recursive: lock/unlock the items the List contains */
3711 for (li = l->lv_first; li != NULL; li = li->li_next)
3712 item_lock(&li->li_tv, deep - 1, lock);
3713 }
3714 break;
3715 case VAR_DICT:
3716 if ((d = tv->vval.v_dict) != NULL)
3717 {
3718 if (lock)
3719 d->dv_lock |= VAR_LOCKED;
3720 else
3721 d->dv_lock &= ~VAR_LOCKED;
3722 if (deep < 0 || deep > 1)
3723 {
3724 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003725 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3727 {
3728 if (!HASHITEM_EMPTY(hi))
3729 {
3730 --todo;
3731 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3732 }
3733 }
3734 }
3735 }
3736 }
3737 --recurse;
3738}
3739
Bram Moolenaara40058a2005-07-11 22:42:07 +00003740/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003741 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3742 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003743 */
3744 static int
3745tv_islocked(tv)
3746 typval_T *tv;
3747{
3748 return (tv->v_lock & VAR_LOCKED)
3749 || (tv->v_type == VAR_LIST
3750 && tv->vval.v_list != NULL
3751 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3752 || (tv->v_type == VAR_DICT
3753 && tv->vval.v_dict != NULL
3754 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3755}
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3758/*
3759 * Delete all "menutrans_" variables.
3760 */
3761 void
3762del_menutrans_vars()
3763{
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003765 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003768 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 {
3771 if (!HASHITEM_EMPTY(hi))
3772 {
3773 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3775 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 }
3777 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003778 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779}
3780#endif
3781
3782#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3783
3784/*
3785 * Local string buffer for the next two functions to store a variable name
3786 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3787 * get_user_var_name().
3788 */
3789
3790static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3791
3792static char_u *varnamebuf = NULL;
3793static int varnamebuflen = 0;
3794
3795/*
3796 * Function to concatenate a prefix and a variable name.
3797 */
3798 static char_u *
3799cat_prefix_varname(prefix, name)
3800 int prefix;
3801 char_u *name;
3802{
3803 int len;
3804
3805 len = (int)STRLEN(name) + 3;
3806 if (len > varnamebuflen)
3807 {
3808 vim_free(varnamebuf);
3809 len += 10; /* some additional space */
3810 varnamebuf = alloc(len);
3811 if (varnamebuf == NULL)
3812 {
3813 varnamebuflen = 0;
3814 return NULL;
3815 }
3816 varnamebuflen = len;
3817 }
3818 *varnamebuf = prefix;
3819 varnamebuf[1] = ':';
3820 STRCPY(varnamebuf + 2, name);
3821 return varnamebuf;
3822}
3823
3824/*
3825 * Function given to ExpandGeneric() to obtain the list of user defined
3826 * (global/buffer/window/built-in) variable names.
3827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 char_u *
3829get_user_var_name(xp, idx)
3830 expand_T *xp;
3831 int idx;
3832{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003833 static long_u gdone;
3834 static long_u bdone;
3835 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003836#ifdef FEAT_WINDOWS
3837 static long_u tdone;
3838#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003839 static int vidx;
3840 static hashitem_T *hi;
3841 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003844 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003846#ifdef FEAT_WINDOWS
3847 tdone = 0;
3848#endif
3849 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003850
3851 /* Global variables */
3852 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003856 else
3857 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003858 while (HASHITEM_EMPTY(hi))
3859 ++hi;
3860 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3861 return cat_prefix_varname('g', hi->hi_key);
3862 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864
3865 /* b: variables */
3866 ht = &curbuf->b_vars.dv_hashtab;
3867 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003869 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003871 else
3872 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003873 while (HASHITEM_EMPTY(hi))
3874 ++hi;
3875 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003877 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003879 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return (char_u *)"b:changedtick";
3881 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003882
3883 /* w: variables */
3884 ht = &curwin->w_vars.dv_hashtab;
3885 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003887 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003888 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003889 else
3890 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003891 while (HASHITEM_EMPTY(hi))
3892 ++hi;
3893 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003895
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003896#ifdef FEAT_WINDOWS
3897 /* t: variables */
3898 ht = &curtab->tp_vars.dv_hashtab;
3899 if (tdone < ht->ht_used)
3900 {
3901 if (tdone++ == 0)
3902 hi = ht->ht_array;
3903 else
3904 ++hi;
3905 while (HASHITEM_EMPTY(hi))
3906 ++hi;
3907 return cat_prefix_varname('t', hi->hi_key);
3908 }
3909#endif
3910
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 /* v: variables */
3912 if (vidx < VV_LEN)
3913 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 vim_free(varnamebuf);
3916 varnamebuf = NULL;
3917 varnamebuflen = 0;
3918 return NULL;
3919}
3920
3921#endif /* FEAT_CMDL_COMPL */
3922
3923/*
3924 * types for expressions.
3925 */
3926typedef enum
3927{
3928 TYPE_UNKNOWN = 0
3929 , TYPE_EQUAL /* == */
3930 , TYPE_NEQUAL /* != */
3931 , TYPE_GREATER /* > */
3932 , TYPE_GEQUAL /* >= */
3933 , TYPE_SMALLER /* < */
3934 , TYPE_SEQUAL /* <= */
3935 , TYPE_MATCH /* =~ */
3936 , TYPE_NOMATCH /* !~ */
3937} exptype_T;
3938
3939/*
3940 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3943 */
3944
3945/*
3946 * Handle zero level expression.
3947 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003949 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 * Return OK or FAIL.
3951 */
3952 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 char_u **nextcmd;
3957 int evaluate;
3958{
3959 int ret;
3960 char_u *p;
3961
3962 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if (ret == FAIL || !ends_excmd(*p))
3965 {
3966 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 /*
3969 * Report the invalid expression unless the expression evaluation has
3970 * been cancelled due to an aborting error, an interrupt, or an
3971 * exception.
3972 */
3973 if (!aborting())
3974 EMSG2(_(e_invexpr2), arg);
3975 ret = FAIL;
3976 }
3977 if (nextcmd != NULL)
3978 *nextcmd = check_nextcmd(p);
3979
3980 return ret;
3981}
3982
3983/*
3984 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003985 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 *
3987 * "arg" must point to the first non-white of the expression.
3988 * "arg" is advanced to the next non-white after the recognized expression.
3989 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003990 * Note: "rettv.v_lock" is not set.
3991 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * Return OK or FAIL.
3993 */
3994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int evaluate;
3999{
4000 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /*
4004 * Get the first variable.
4005 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FAIL;
4008
4009 if ((*arg)[0] == '?')
4010 {
4011 result = FALSE;
4012 if (evaluate)
4013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 int error = FALSE;
4015
4016 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004019 if (error)
4020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022
4023 /*
4024 * Get the second variable.
4025 */
4026 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return FAIL;
4029
4030 /*
4031 * Check for the ":".
4032 */
4033 if ((*arg)[0] != ':')
4034 {
4035 EMSG(_("E109: Missing ':' after '?'"));
4036 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return FAIL;
4039 }
4040
4041 /*
4042 * Get the third variable.
4043 */
4044 *arg = skipwhite(*arg + 1);
4045 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4046 {
4047 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return FAIL;
4050 }
4051 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054
4055 return OK;
4056}
4057
4058/*
4059 * Handle first level expression:
4060 * expr2 || expr2 || expr2 logical OR
4061 *
4062 * "arg" must point to the first non-white of the expression.
4063 * "arg" is advanced to the next non-white after the recognized expression.
4064 *
4065 * Return OK or FAIL.
4066 */
4067 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 int evaluate;
4072{
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 long result;
4075 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 /*
4079 * Get the first variable.
4080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return FAIL;
4083
4084 /*
4085 * Repeat until there is no following "||".
4086 */
4087 first = TRUE;
4088 result = FALSE;
4089 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4090 {
4091 if (evaluate && first)
4092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004096 if (error)
4097 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 first = FALSE;
4099 }
4100
4101 /*
4102 * Get the second variable.
4103 */
4104 *arg = skipwhite(*arg + 2);
4105 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4106 return FAIL;
4107
4108 /*
4109 * Compute the result.
4110 */
4111 if (evaluate && !result)
4112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004116 if (error)
4117 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119 if (evaluate)
4120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 rettv->v_type = VAR_NUMBER;
4122 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle second level expression:
4131 * expr3 && expr3 && expr3 logical AND
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 long result;
4146 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Get the first variable.
4151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 return FAIL;
4154
4155 /*
4156 * Repeat until there is no following "&&".
4157 */
4158 first = TRUE;
4159 result = TRUE;
4160 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4161 {
4162 if (evaluate && first)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (error)
4168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 first = FALSE;
4170 }
4171
4172 /*
4173 * Get the second variable.
4174 */
4175 *arg = skipwhite(*arg + 2);
4176 if (eval4(arg, &var2, evaluate && result) == FAIL)
4177 return FAIL;
4178
4179 /*
4180 * Compute the result.
4181 */
4182 if (evaluate && result)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 if (evaluate)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 rettv->v_type = VAR_NUMBER;
4193 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197 return OK;
4198}
4199
4200/*
4201 * Handle third level expression:
4202 * var1 == var2
4203 * var1 =~ var2
4204 * var1 != var2
4205 * var1 !~ var2
4206 * var1 > var2
4207 * var1 >= var2
4208 * var1 < var2
4209 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004210 * var1 is var2
4211 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 *
4213 * "arg" must point to the first non-white of the expression.
4214 * "arg" is advanced to the next non-white after the recognized expression.
4215 *
4216 * Return OK or FAIL.
4217 */
4218 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 int evaluate;
4223{
Bram Moolenaar33570922005-01-25 22:26:29 +00004224 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u *p;
4226 int i;
4227 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004228 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int len = 2;
4230 long n1, n2;
4231 char_u *s1, *s2;
4232 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4233 regmatch_T regmatch;
4234 int ic;
4235 char_u *save_cpo;
4236
4237 /*
4238 * Get the first variable.
4239 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 return FAIL;
4242
4243 p = *arg;
4244 switch (p[0])
4245 {
4246 case '=': if (p[1] == '=')
4247 type = TYPE_EQUAL;
4248 else if (p[1] == '~')
4249 type = TYPE_MATCH;
4250 break;
4251 case '!': if (p[1] == '=')
4252 type = TYPE_NEQUAL;
4253 else if (p[1] == '~')
4254 type = TYPE_NOMATCH;
4255 break;
4256 case '>': if (p[1] != '=')
4257 {
4258 type = TYPE_GREATER;
4259 len = 1;
4260 }
4261 else
4262 type = TYPE_GEQUAL;
4263 break;
4264 case '<': if (p[1] != '=')
4265 {
4266 type = TYPE_SMALLER;
4267 len = 1;
4268 }
4269 else
4270 type = TYPE_SEQUAL;
4271 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004272 case 'i': if (p[1] == 's')
4273 {
4274 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4275 len = 5;
4276 if (!vim_isIDc(p[len]))
4277 {
4278 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4279 type_is = TRUE;
4280 }
4281 }
4282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284
4285 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004286 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
4288 if (type != TYPE_UNKNOWN)
4289 {
4290 /* extra question mark appended: ignore case */
4291 if (p[len] == '?')
4292 {
4293 ic = TRUE;
4294 ++len;
4295 }
4296 /* extra '#' appended: match case */
4297 else if (p[len] == '#')
4298 {
4299 ic = FALSE;
4300 ++len;
4301 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004302 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 else
4304 ic = p_ic;
4305
4306 /*
4307 * Get the second variable.
4308 */
4309 *arg = skipwhite(p + len);
4310 if (eval5(arg, &var2, evaluate) == FAIL)
4311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315
4316 if (evaluate)
4317 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004318 if (type_is && rettv->v_type != var2.v_type)
4319 {
4320 /* For "is" a different type always means FALSE, for "notis"
4321 * it means TRUE. */
4322 n1 = (type == TYPE_NEQUAL);
4323 }
4324 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4325 {
4326 if (type_is)
4327 {
4328 n1 = (rettv->v_type == var2.v_type
4329 && rettv->vval.v_list == var2.vval.v_list);
4330 if (type == TYPE_NEQUAL)
4331 n1 = !n1;
4332 }
4333 else if (rettv->v_type != var2.v_type
4334 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4335 {
4336 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004337 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004338 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004339 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004340 clear_tv(rettv);
4341 clear_tv(&var2);
4342 return FAIL;
4343 }
4344 else
4345 {
4346 /* Compare two Lists for being equal or unequal. */
4347 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4348 if (type == TYPE_NEQUAL)
4349 n1 = !n1;
4350 }
4351 }
4352
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004353 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4354 {
4355 if (type_is)
4356 {
4357 n1 = (rettv->v_type == var2.v_type
4358 && rettv->vval.v_dict == var2.vval.v_dict);
4359 if (type == TYPE_NEQUAL)
4360 n1 = !n1;
4361 }
4362 else if (rettv->v_type != var2.v_type
4363 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4364 {
4365 if (rettv->v_type != var2.v_type)
4366 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4367 else
4368 EMSG(_("E736: Invalid operation for Dictionary"));
4369 clear_tv(rettv);
4370 clear_tv(&var2);
4371 return FAIL;
4372 }
4373 else
4374 {
4375 /* Compare two Dictionaries for being equal or unequal. */
4376 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4377 if (type == TYPE_NEQUAL)
4378 n1 = !n1;
4379 }
4380 }
4381
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4383 {
4384 if (rettv->v_type != var2.v_type
4385 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4386 {
4387 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004388 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004390 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391 clear_tv(rettv);
4392 clear_tv(&var2);
4393 return FAIL;
4394 }
4395 else
4396 {
4397 /* Compare two Funcrefs for being equal or unequal. */
4398 if (rettv->vval.v_string == NULL
4399 || var2.vval.v_string == NULL)
4400 n1 = FALSE;
4401 else
4402 n1 = STRCMP(rettv->vval.v_string,
4403 var2.vval.v_string) == 0;
4404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 }
4408
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004409#ifdef FEAT_FLOAT
4410 /*
4411 * If one of the two variables is a float, compare as a float.
4412 * When using "=~" or "!~", always compare as string.
4413 */
4414 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4415 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4416 {
4417 float_T f1, f2;
4418
4419 if (rettv->v_type == VAR_FLOAT)
4420 f1 = rettv->vval.v_float;
4421 else
4422 f1 = get_tv_number(rettv);
4423 if (var2.v_type == VAR_FLOAT)
4424 f2 = var2.vval.v_float;
4425 else
4426 f2 = get_tv_number(&var2);
4427 n1 = FALSE;
4428 switch (type)
4429 {
4430 case TYPE_EQUAL: n1 = (f1 == f2); break;
4431 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4432 case TYPE_GREATER: n1 = (f1 > f2); break;
4433 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4434 case TYPE_SMALLER: n1 = (f1 < f2); break;
4435 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4436 case TYPE_UNKNOWN:
4437 case TYPE_MATCH:
4438 case TYPE_NOMATCH: break; /* avoid gcc warning */
4439 }
4440 }
4441#endif
4442
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 /*
4444 * If one of the two variables is a number, compare as a number.
4445 * When using "=~" or "!~", always compare as string.
4446 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 n1 = get_tv_number(rettv);
4451 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 switch (type)
4453 {
4454 case TYPE_EQUAL: n1 = (n1 == n2); break;
4455 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4456 case TYPE_GREATER: n1 = (n1 > n2); break;
4457 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4458 case TYPE_SMALLER: n1 = (n1 < n2); break;
4459 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4460 case TYPE_UNKNOWN:
4461 case TYPE_MATCH:
4462 case TYPE_NOMATCH: break; /* avoid gcc warning */
4463 }
4464 }
4465 else
4466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 s1 = get_tv_string_buf(rettv, buf1);
4468 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4470 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4471 else
4472 i = 0;
4473 n1 = FALSE;
4474 switch (type)
4475 {
4476 case TYPE_EQUAL: n1 = (i == 0); break;
4477 case TYPE_NEQUAL: n1 = (i != 0); break;
4478 case TYPE_GREATER: n1 = (i > 0); break;
4479 case TYPE_GEQUAL: n1 = (i >= 0); break;
4480 case TYPE_SMALLER: n1 = (i < 0); break;
4481 case TYPE_SEQUAL: n1 = (i <= 0); break;
4482
4483 case TYPE_MATCH:
4484 case TYPE_NOMATCH:
4485 /* avoid 'l' flag in 'cpoptions' */
4486 save_cpo = p_cpo;
4487 p_cpo = (char_u *)"";
4488 regmatch.regprog = vim_regcomp(s2,
4489 RE_MAGIC + RE_STRING);
4490 regmatch.rm_ic = ic;
4491 if (regmatch.regprog != NULL)
4492 {
4493 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4494 vim_free(regmatch.regprog);
4495 if (type == TYPE_NOMATCH)
4496 n1 = !n1;
4497 }
4498 p_cpo = save_cpo;
4499 break;
4500
4501 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4502 }
4503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 clear_tv(rettv);
4505 clear_tv(&var2);
4506 rettv->v_type = VAR_NUMBER;
4507 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 }
4510
4511 return OK;
4512}
4513
4514/*
4515 * Handle fourth level expression:
4516 * + number addition
4517 * - number subtraction
4518 * . string concatenation
4519 *
4520 * "arg" must point to the first non-white of the expression.
4521 * "arg" is advanced to the next non-white after the recognized expression.
4522 *
4523 * Return OK or FAIL.
4524 */
4525 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 int evaluate;
4530{
Bram Moolenaar33570922005-01-25 22:26:29 +00004531 typval_T var2;
4532 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 int op;
4534 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004535#ifdef FEAT_FLOAT
4536 float_T f1 = 0, f2 = 0;
4537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 char_u *s1, *s2;
4539 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4540 char_u *p;
4541
4542 /*
4543 * Get the first variable.
4544 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004545 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 return FAIL;
4547
4548 /*
4549 * Repeat computing, until no '+', '-' or '.' is following.
4550 */
4551 for (;;)
4552 {
4553 op = **arg;
4554 if (op != '+' && op != '-' && op != '.')
4555 break;
4556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004557 if ((op != '+' || rettv->v_type != VAR_LIST)
4558#ifdef FEAT_FLOAT
4559 && (op == '.' || rettv->v_type != VAR_FLOAT)
4560#endif
4561 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 {
4563 /* For "list + ...", an illegal use of the first operand as
4564 * a number cannot be determined before evaluating the 2nd
4565 * operand: if this is also a list, all is ok.
4566 * For "something . ...", "something - ..." or "non-list + ...",
4567 * we know that the first operand needs to be a string or number
4568 * without evaluating the 2nd operand. So check before to avoid
4569 * side effects after an error. */
4570 if (evaluate && get_tv_string_chk(rettv) == NULL)
4571 {
4572 clear_tv(rettv);
4573 return FAIL;
4574 }
4575 }
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 /*
4578 * Get the second variable.
4579 */
4580 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004581 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 return FAIL;
4585 }
4586
4587 if (evaluate)
4588 {
4589 /*
4590 * Compute the result.
4591 */
4592 if (op == '.')
4593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4595 s2 = get_tv_string_buf_chk(&var2, buf2);
4596 if (s2 == NULL) /* type error ? */
4597 {
4598 clear_tv(rettv);
4599 clear_tv(&var2);
4600 return FAIL;
4601 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004602 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 clear_tv(rettv);
4604 rettv->v_type = VAR_STRING;
4605 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004607 else if (op == '+' && rettv->v_type == VAR_LIST
4608 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 {
4610 /* concatenate Lists */
4611 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4612 &var3) == FAIL)
4613 {
4614 clear_tv(rettv);
4615 clear_tv(&var2);
4616 return FAIL;
4617 }
4618 clear_tv(rettv);
4619 *rettv = var3;
4620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else
4622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 int error = FALSE;
4624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004625#ifdef FEAT_FLOAT
4626 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004628 f1 = rettv->vval.v_float;
4629 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 else
4632#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 n1 = get_tv_number_chk(rettv, &error);
4635 if (error)
4636 {
4637 /* This can only happen for "list + non-list". For
4638 * "non-list + ..." or "something - ...", we returned
4639 * before evaluating the 2nd operand. */
4640 clear_tv(rettv);
4641 return FAIL;
4642 }
4643#ifdef FEAT_FLOAT
4644 if (var2.v_type == VAR_FLOAT)
4645 f1 = n1;
4646#endif
4647 }
4648#ifdef FEAT_FLOAT
4649 if (var2.v_type == VAR_FLOAT)
4650 {
4651 f2 = var2.vval.v_float;
4652 n2 = 0;
4653 }
4654 else
4655#endif
4656 {
4657 n2 = get_tv_number_chk(&var2, &error);
4658 if (error)
4659 {
4660 clear_tv(rettv);
4661 clear_tv(&var2);
4662 return FAIL;
4663 }
4664#ifdef FEAT_FLOAT
4665 if (rettv->v_type == VAR_FLOAT)
4666 f2 = n2;
4667#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004670
4671#ifdef FEAT_FLOAT
4672 /* If there is a float on either side the result is a float. */
4673 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4674 {
4675 if (op == '+')
4676 f1 = f1 + f2;
4677 else
4678 f1 = f1 - f2;
4679 rettv->v_type = VAR_FLOAT;
4680 rettv->vval.v_float = f1;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004683#endif
4684 {
4685 if (op == '+')
4686 n1 = n1 + n2;
4687 else
4688 n1 = n1 - n2;
4689 rettv->v_type = VAR_NUMBER;
4690 rettv->vval.v_number = n1;
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
4696 return OK;
4697}
4698
4699/*
4700 * Handle fifth level expression:
4701 * * number multiplication
4702 * / number division
4703 * % number modulo
4704 *
4705 * "arg" must point to the first non-white of the expression.
4706 * "arg" is advanced to the next non-white after the recognized expression.
4707 *
4708 * Return OK or FAIL.
4709 */
4710 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004715 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 int op;
4719 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720#ifdef FEAT_FLOAT
4721 int use_float = FALSE;
4722 float_T f1 = 0, f2;
4723#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 /*
4727 * Get the first variable.
4728 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return FAIL;
4731
4732 /*
4733 * Repeat computing, until no '*', '/' or '%' is following.
4734 */
4735 for (;;)
4736 {
4737 op = **arg;
4738 if (op != '*' && op != '/' && op != '%')
4739 break;
4740
4741 if (evaluate)
4742 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743#ifdef FEAT_FLOAT
4744 if (rettv->v_type == VAR_FLOAT)
4745 {
4746 f1 = rettv->vval.v_float;
4747 use_float = TRUE;
4748 n1 = 0;
4749 }
4750 else
4751#endif
4752 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 if (error)
4755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 else
4758 n1 = 0;
4759
4760 /*
4761 * Get the second variable.
4762 */
4763 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004764 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return FAIL;
4766
4767 if (evaluate)
4768 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004769#ifdef FEAT_FLOAT
4770 if (var2.v_type == VAR_FLOAT)
4771 {
4772 if (!use_float)
4773 {
4774 f1 = n1;
4775 use_float = TRUE;
4776 }
4777 f2 = var2.vval.v_float;
4778 n2 = 0;
4779 }
4780 else
4781#endif
4782 {
4783 n2 = get_tv_number_chk(&var2, &error);
4784 clear_tv(&var2);
4785 if (error)
4786 return FAIL;
4787#ifdef FEAT_FLOAT
4788 if (use_float)
4789 f2 = n2;
4790#endif
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
4793 /*
4794 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797#ifdef FEAT_FLOAT
4798 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 if (op == '*')
4801 f1 = f1 * f2;
4802 else if (op == '/')
4803 {
4804 /* We rely on the floating point library to handle divide
4805 * by zero to result in "inf" and not a crash. */
4806 f1 = f1 / f2;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004810 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 return FAIL;
4812 }
4813 rettv->v_type = VAR_FLOAT;
4814 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819 if (op == '*')
4820 n1 = n1 * n2;
4821 else if (op == '/')
4822 {
4823 if (n2 == 0) /* give an error message? */
4824 {
4825 if (n1 == 0)
4826 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4827 else if (n1 < 0)
4828 n1 = -0x7fffffffL;
4829 else
4830 n1 = 0x7fffffffL;
4831 }
4832 else
4833 n1 = n1 / n2;
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 {
4837 if (n2 == 0) /* give an error message? */
4838 n1 = 0;
4839 else
4840 n1 = n1 % n2;
4841 }
4842 rettv->v_type = VAR_NUMBER;
4843 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 }
4847
4848 return OK;
4849}
4850
4851/*
4852 * Handle sixth level expression:
4853 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004854 * "string" string constant
4855 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 * &option-name option value
4857 * @r register contents
4858 * identifier variable value
4859 * function() function call
4860 * $VAR environment variable
4861 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004862 * [expr, expr] List
4863 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 *
4865 * Also handle:
4866 * ! in front logical NOT
4867 * - in front unary minus
4868 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869 * trailing [] subscript in String or List
4870 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 *
4872 * "arg" must point to the first non-white of the expression.
4873 * "arg" is advanced to the next non-white after the recognized expression.
4874 *
4875 * Return OK or FAIL.
4876 */
4877 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004878eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004882 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 long n;
4885 int len;
4886 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 char_u *start_leader, *end_leader;
4888 int ret = OK;
4889 char_u *alias;
4890
4891 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 /*
4898 * Skip '!' and '-' characters. They are handled later.
4899 */
4900 start_leader = *arg;
4901 while (**arg == '!' || **arg == '-' || **arg == '+')
4902 *arg = skipwhite(*arg + 1);
4903 end_leader = *arg;
4904
4905 switch (**arg)
4906 {
4907 /*
4908 * Number constant.
4909 */
4910 case '0':
4911 case '1':
4912 case '2':
4913 case '3':
4914 case '4':
4915 case '5':
4916 case '6':
4917 case '7':
4918 case '8':
4919 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 {
4921#ifdef FEAT_FLOAT
4922 char_u *p = skipdigits(*arg + 1);
4923 int get_float = FALSE;
4924
4925 /* We accept a float when the format matches
4926 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004927 * strict to avoid backwards compatibility problems.
4928 * Don't look for a float after the "." operator, so that
4929 * ":let vers = 1.2.3" doesn't fail. */
4930 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 get_float = TRUE;
4933 p = skipdigits(p + 2);
4934 if (*p == 'e' || *p == 'E')
4935 {
4936 ++p;
4937 if (*p == '-' || *p == '+')
4938 ++p;
4939 if (!vim_isdigit(*p))
4940 get_float = FALSE;
4941 else
4942 p = skipdigits(p + 1);
4943 }
4944 if (ASCII_ISALPHA(*p) || *p == '.')
4945 get_float = FALSE;
4946 }
4947 if (get_float)
4948 {
4949 float_T f;
4950
4951 *arg += string2float(*arg, &f);
4952 if (evaluate)
4953 {
4954 rettv->v_type = VAR_FLOAT;
4955 rettv->vval.v_float = f;
4956 }
4957 }
4958 else
4959#endif
4960 {
4961 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4962 *arg += len;
4963 if (evaluate)
4964 {
4965 rettv->v_type = VAR_NUMBER;
4966 rettv->vval.v_number = n;
4967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971
4972 /*
4973 * String constant: "string".
4974 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004975 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 break;
4977
4978 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 break;
4983
4984 /*
4985 * List: [expr, expr]
4986 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989
4990 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 * Dictionary: {key: val, key: val}
4992 */
4993 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4994 break;
4995
4996 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004997 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004999 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 break;
5001
5002 /*
5003 * Environment variable: $VAR.
5004 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007
5008 /*
5009 * Register contents: @r.
5010 */
5011 case '@': ++*arg;
5012 if (evaluate)
5013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005015 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 if (**arg != NUL)
5018 ++*arg;
5019 break;
5020
5021 /*
5022 * nested expression: (expression).
5023 */
5024 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (**arg == ')')
5027 ++*arg;
5028 else if (ret == OK)
5029 {
5030 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 ret = FAIL;
5033 }
5034 break;
5035
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039
5040 if (ret == NOTDONE)
5041 {
5042 /*
5043 * Must be a variable or function name.
5044 * Can also be a curly-braces kind of name: {expr}.
5045 */
5046 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005047 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 if (alias != NULL)
5049 s = alias;
5050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005051 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 ret = FAIL;
5053 else
5054 {
5055 if (**arg == '(') /* recursive! */
5056 {
5057 /* If "s" is the name of a variable of type VAR_FUNC
5058 * use its contents. */
5059 s = deref_func_name(s, &len);
5060
5061 /* Invoke the function. */
5062 ret = get_func_tv(s, len, rettv, arg,
5063 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005064 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 /* Stop the expression evaluation when immediately
5066 * aborting on error, or when an interrupt occurred or
5067 * an exception was thrown but not caught. */
5068 if (aborting())
5069 {
5070 if (ret == OK)
5071 clear_tv(rettv);
5072 ret = FAIL;
5073 }
5074 }
5075 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005077 else
5078 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 }
5080
5081 if (alias != NULL)
5082 vim_free(alias);
5083 }
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 *arg = skipwhite(*arg);
5086
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005087 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5088 * expr(expr). */
5089 if (ret == OK)
5090 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 /*
5093 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5094 */
5095 if (ret == OK && evaluate && end_leader > start_leader)
5096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 int val = 0;
5099#ifdef FEAT_FLOAT
5100 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005101
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 if (rettv->v_type == VAR_FLOAT)
5103 f = rettv->vval.v_float;
5104 else
5105#endif
5106 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005107 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005109 clear_tv(rettv);
5110 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112 else
5113 {
5114 while (end_leader > start_leader)
5115 {
5116 --end_leader;
5117 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 {
5119#ifdef FEAT_FLOAT
5120 if (rettv->v_type == VAR_FLOAT)
5121 f = !f;
5122 else
5123#endif
5124 val = !val;
5125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 {
5128#ifdef FEAT_FLOAT
5129 if (rettv->v_type == VAR_FLOAT)
5130 f = -f;
5131 else
5132#endif
5133 val = -val;
5134 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005136#ifdef FEAT_FLOAT
5137 if (rettv->v_type == VAR_FLOAT)
5138 {
5139 clear_tv(rettv);
5140 rettv->vval.v_float = f;
5141 }
5142 else
5143#endif
5144 {
5145 clear_tv(rettv);
5146 rettv->v_type = VAR_NUMBER;
5147 rettv->vval.v_number = val;
5148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151
5152 return ret;
5153}
5154
5155/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005156 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5157 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5159 */
5160 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166{
5167 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005168 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 long len = -1;
5171 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005175 if (rettv->v_type == VAR_FUNC
5176#ifdef FEAT_FLOAT
5177 || rettv->v_type == VAR_FLOAT
5178#endif
5179 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 if (verbose)
5182 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 return FAIL;
5184 }
5185
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /*
5189 * dict.name
5190 */
5191 key = *arg + 1;
5192 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5193 ;
5194 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 }
5198 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 /*
5201 * something[idx]
5202 *
5203 * Get the (first) variable from inside the [].
5204 */
5205 *arg = skipwhite(*arg + 1);
5206 if (**arg == ':')
5207 empty1 = TRUE;
5208 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5209 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5211 {
5212 /* not a number or string */
5213 clear_tv(&var1);
5214 return FAIL;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 /*
5218 * Get the second variable from inside the [:].
5219 */
5220 if (**arg == ':')
5221 {
5222 range = TRUE;
5223 *arg = skipwhite(*arg + 1);
5224 if (**arg == ']')
5225 empty2 = TRUE;
5226 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (!empty1)
5229 clear_tv(&var1);
5230 return FAIL;
5231 }
5232 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5233 {
5234 /* not a number or string */
5235 if (!empty1)
5236 clear_tv(&var1);
5237 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 return FAIL;
5239 }
5240 }
5241
5242 /* Check for the ']'. */
5243 if (**arg != ']')
5244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005245 if (verbose)
5246 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 clear_tv(&var1);
5248 if (range)
5249 clear_tv(&var2);
5250 return FAIL;
5251 }
5252 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 }
5254
5255 if (evaluate)
5256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 n1 = 0;
5258 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 n1 = get_tv_number(&var1);
5261 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 }
5263 if (range)
5264 {
5265 if (empty2)
5266 n2 = -1;
5267 else
5268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 n2 = get_tv_number(&var2);
5270 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 }
5272 }
5273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
5276 case VAR_NUMBER:
5277 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 len = (long)STRLEN(s);
5280 if (range)
5281 {
5282 /* The resulting variable is a substring. If the indexes
5283 * are out of range the result is empty. */
5284 if (n1 < 0)
5285 {
5286 n1 = len + n1;
5287 if (n1 < 0)
5288 n1 = 0;
5289 }
5290 if (n2 < 0)
5291 n2 = len + n2;
5292 else if (n2 >= len)
5293 n2 = len;
5294 if (n1 >= len || n2 < 0 || n1 > n2)
5295 s = NULL;
5296 else
5297 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5298 }
5299 else
5300 {
5301 /* The resulting variable is a string of a single
5302 * character. If the index is too big or negative the
5303 * result is empty. */
5304 if (n1 >= len || n1 < 0)
5305 s = NULL;
5306 else
5307 s = vim_strnsave(s + n1, 1);
5308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005309 clear_tv(rettv);
5310 rettv->v_type = VAR_STRING;
5311 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 break;
5313
5314 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005315 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 if (n1 < 0)
5317 n1 = len + n1;
5318 if (!empty1 && (n1 < 0 || n1 >= len))
5319 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005320 /* For a range we allow invalid values and return an empty
5321 * list. A list index out of range is an error. */
5322 if (!range)
5323 {
5324 if (verbose)
5325 EMSGN(_(e_listidx), n1);
5326 return FAIL;
5327 }
5328 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 }
5330 if (range)
5331 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005332 list_T *l;
5333 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334
5335 if (n2 < 0)
5336 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005337 else if (n2 >= len)
5338 n2 = len - 1;
5339 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005340 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 l = list_alloc();
5342 if (l == NULL)
5343 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 n1 <= n2; ++n1)
5346 {
5347 if (list_append_tv(l, &item->li_tv) == FAIL)
5348 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005349 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
5352 item = item->li_next;
5353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 clear_tv(rettv);
5355 rettv->v_type = VAR_LIST;
5356 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005357 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 else
5360 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005361 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005362 clear_tv(rettv);
5363 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 }
5365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366
5367 case VAR_DICT:
5368 if (range)
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (len == -1)
5373 clear_tv(&var1);
5374 return FAIL;
5375 }
5376 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378
5379 if (len == -1)
5380 {
5381 key = get_tv_string(&var1);
5382 if (*key == NUL)
5383 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005384 if (verbose)
5385 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 clear_tv(&var1);
5387 return FAIL;
5388 }
5389 }
5390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005391 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005393 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005394 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 if (len == -1)
5396 clear_tv(&var1);
5397 if (item == NULL)
5398 return FAIL;
5399
5400 copy_tv(&item->di_tv, &var1);
5401 clear_tv(rettv);
5402 *rettv = var1;
5403 }
5404 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 }
5406 }
5407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 return OK;
5409}
5410
5411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 * Get an option value.
5413 * "arg" points to the '&' or '+' before the option name.
5414 * "arg" is advanced to character after the option name.
5415 * Return OK or FAIL.
5416 */
5417 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 int evaluate;
5422{
5423 char_u *option_end;
5424 long numval;
5425 char_u *stringval;
5426 int opt_type;
5427 int c;
5428 int working = (**arg == '+'); /* has("+option") */
5429 int ret = OK;
5430 int opt_flags;
5431
5432 /*
5433 * Isolate the option name and find its value.
5434 */
5435 option_end = find_option_end(arg, &opt_flags);
5436 if (option_end == NULL)
5437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 EMSG2(_("E112: Option name missing: %s"), *arg);
5440 return FAIL;
5441 }
5442
5443 if (!evaluate)
5444 {
5445 *arg = option_end;
5446 return OK;
5447 }
5448
5449 c = *option_end;
5450 *option_end = NUL;
5451 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454 if (opt_type == -3) /* invalid name */
5455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 EMSG2(_("E113: Unknown option: %s"), *arg);
5458 ret = FAIL;
5459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
5462 if (opt_type == -2) /* hidden string option */
5463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 rettv->v_type = VAR_STRING;
5465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467 else if (opt_type == -1) /* hidden number option */
5468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 rettv->v_type = VAR_NUMBER;
5470 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else if (opt_type == 1) /* number option */
5473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 rettv->v_type = VAR_NUMBER;
5475 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else /* string option */
5478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 rettv->v_type = VAR_STRING;
5480 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 }
5483 else if (working && (opt_type == -2 || opt_type == -1))
5484 ret = FAIL;
5485
5486 *option_end = c; /* put back for error messages */
5487 *arg = option_end;
5488
5489 return ret;
5490}
5491
5492/*
5493 * Allocate a variable for a string constant.
5494 * Return OK or FAIL.
5495 */
5496 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int evaluate;
5501{
5502 char_u *p;
5503 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 int extra = 0;
5505
5506 /*
5507 * Find the end of the string, skipping backslashed characters.
5508 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005509 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 if (*p == '\\' && p[1] != NUL)
5512 {
5513 ++p;
5514 /* A "\<x>" form occupies at least 4 characters, and produces up
5515 * to 6 characters: reserve space for 2 extra */
5516 if (*p == '<')
5517 extra += 2;
5518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
5520
5521 if (*p != '"')
5522 {
5523 EMSG2(_("E114: Missing quote: %s"), *arg);
5524 return FAIL;
5525 }
5526
5527 /* If only parsing, set *arg and return here */
5528 if (!evaluate)
5529 {
5530 *arg = p + 1;
5531 return OK;
5532 }
5533
5534 /*
5535 * Copy the string into allocated memory, handling backslashed
5536 * characters.
5537 */
5538 name = alloc((unsigned)(p - *arg + extra));
5539 if (name == NULL)
5540 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 rettv->v_type = VAR_STRING;
5542 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
5546 if (*p == '\\')
5547 {
5548 switch (*++p)
5549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 case 'b': *name++ = BS; ++p; break;
5551 case 'e': *name++ = ESC; ++p; break;
5552 case 'f': *name++ = FF; ++p; break;
5553 case 'n': *name++ = NL; ++p; break;
5554 case 'r': *name++ = CAR; ++p; break;
5555 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 case 'X': /* hex: "\x1", "\x12" */
5558 case 'x':
5559 case 'u': /* Unicode: "\u0023" */
5560 case 'U':
5561 if (vim_isxdigit(p[1]))
5562 {
5563 int n, nr;
5564 int c = toupper(*p);
5565
5566 if (c == 'X')
5567 n = 2;
5568 else
5569 n = 4;
5570 nr = 0;
5571 while (--n >= 0 && vim_isxdigit(p[1]))
5572 {
5573 ++p;
5574 nr = (nr << 4) + hex2nr(*p);
5575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577#ifdef FEAT_MBYTE
5578 /* For "\u" store the number according to
5579 * 'encoding'. */
5580 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 else
5583#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 break;
5587
5588 /* octal: "\1", "\12", "\123" */
5589 case '0':
5590 case '1':
5591 case '2':
5592 case '3':
5593 case '4':
5594 case '5':
5595 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 case '7': *name = *p++ - '0';
5597 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 *name = (*name << 3) + *p++ - '0';
5600 if (*p >= '0' && *p <= '7')
5601 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 break;
5605
5606 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (extra != 0)
5609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 break;
5612 }
5613 /* FALLTHROUGH */
5614
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 break;
5617 }
5618 }
5619 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 *arg = p + 1;
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 return OK;
5627}
5628
5629/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 * Return OK or FAIL.
5632 */
5633 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 int evaluate;
5638{
5639 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 int reduce = 0;
5642
5643 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005645 */
5646 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005651 break;
5652 ++reduce;
5653 ++p;
5654 }
5655 }
5656
Bram Moolenaar8c711452005-01-14 21:53:12 +00005657 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 return FAIL;
5661 }
5662
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 if (!evaluate)
5665 {
5666 *arg = p + 1;
5667 return OK;
5668 }
5669
5670 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 */
5673 str = alloc((unsigned)((p - *arg) - reduce));
5674 if (str == NULL)
5675 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 rettv->v_type = VAR_STRING;
5677 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 break;
5685 ++p;
5686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005690 *arg = p + 1;
5691
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 return OK;
5693}
5694
5695/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 * Allocate a variable for a List and fill it from "*arg".
5697 * Return OK or FAIL.
5698 */
5699 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 int evaluate;
5704{
Bram Moolenaar33570922005-01-25 22:26:29 +00005705 list_T *l = NULL;
5706 typval_T tv;
5707 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708
5709 if (evaluate)
5710 {
5711 l = list_alloc();
5712 if (l == NULL)
5713 return FAIL;
5714 }
5715
5716 *arg = skipwhite(*arg + 1);
5717 while (**arg != ']' && **arg != NUL)
5718 {
5719 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5720 goto failret;
5721 if (evaluate)
5722 {
5723 item = listitem_alloc();
5724 if (item != NULL)
5725 {
5726 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005727 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 list_append(l, item);
5729 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005730 else
5731 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 }
5733
5734 if (**arg == ']')
5735 break;
5736 if (**arg != ',')
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 goto failret;
5740 }
5741 *arg = skipwhite(*arg + 1);
5742 }
5743
5744 if (**arg != ']')
5745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747failret:
5748 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005749 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 return FAIL;
5751 }
5752
5753 *arg = skipwhite(*arg + 1);
5754 if (evaluate)
5755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 rettv->v_type = VAR_LIST;
5757 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758 ++l->lv_refcount;
5759 }
5760
5761 return OK;
5762}
5763
5764/*
5765 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005766 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005768 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769list_alloc()
5770{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005771 list_T *l;
5772
5773 l = (list_T *)alloc_clear(sizeof(list_T));
5774 if (l != NULL)
5775 {
5776 /* Prepend the list to the list of lists for garbage collection. */
5777 if (first_list != NULL)
5778 first_list->lv_used_prev = l;
5779 l->lv_used_prev = NULL;
5780 l->lv_used_next = first_list;
5781 first_list = l;
5782 }
5783 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784}
5785
5786/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005787 * Allocate an empty list for a return value.
5788 * Returns OK or FAIL.
5789 */
5790 static int
5791rettv_list_alloc(rettv)
5792 typval_T *rettv;
5793{
5794 list_T *l = list_alloc();
5795
5796 if (l == NULL)
5797 return FAIL;
5798
5799 rettv->vval.v_list = l;
5800 rettv->v_type = VAR_LIST;
5801 ++l->lv_refcount;
5802 return OK;
5803}
5804
5805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * Unreference a list: decrement the reference count and free it when it
5807 * becomes zero.
5808 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005809 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005813 if (l != NULL && --l->lv_refcount <= 0)
5814 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815}
5816
5817/*
5818 * Free a list, including all items it points to.
5819 * Ignores the reference count.
5820 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005821 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005822list_free(l, recurse)
5823 list_T *l;
5824 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825{
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005828 /* Remove the list from the list of lists for garbage collection. */
5829 if (l->lv_used_prev == NULL)
5830 first_list = l->lv_used_next;
5831 else
5832 l->lv_used_prev->lv_used_next = l->lv_used_next;
5833 if (l->lv_used_next != NULL)
5834 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5835
Bram Moolenaard9fba312005-06-26 22:34:35 +00005836 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005838 /* Remove the item before deleting it. */
5839 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005840 if (recurse || (item->li_tv.v_type != VAR_LIST
5841 && item->li_tv.v_type != VAR_DICT))
5842 clear_tv(&item->li_tv);
5843 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 }
5845 vim_free(l);
5846}
5847
5848/*
5849 * Allocate a list item.
5850 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852listitem_alloc()
5853{
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855}
5856
5857/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005858 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 */
5860 static void
5861listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005864 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 vim_free(item);
5866}
5867
5868/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005869 * Remove a list item from a List and free it. Also clears the value.
5870 */
5871 static void
5872listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 list_T *l;
5874 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005875{
5876 list_remove(l, item, item);
5877 listitem_free(item);
5878}
5879
5880/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 * Get the number of items in a list.
5882 */
5883 static long
5884list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 if (l == NULL)
5888 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005889 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890}
5891
5892/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893 * Return TRUE when two lists have exactly the same values.
5894 */
5895 static int
5896list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 list_T *l1;
5898 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005899 int ic; /* ignore case for strings */
5900{
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005902
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005903 if (l1 == NULL || l2 == NULL)
5904 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005905 if (l1 == l2)
5906 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005907 if (list_len(l1) != list_len(l2))
5908 return FALSE;
5909
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005910 for (item1 = l1->lv_first, item2 = l2->lv_first;
5911 item1 != NULL && item2 != NULL;
5912 item1 = item1->li_next, item2 = item2->li_next)
5913 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5914 return FALSE;
5915 return item1 == NULL && item2 == NULL;
5916}
5917
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005918#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5919 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005920/*
5921 * Return the dictitem that an entry in a hashtable points to.
5922 */
5923 dictitem_T *
5924dict_lookup(hi)
5925 hashitem_T *hi;
5926{
5927 return HI2DI(hi);
5928}
5929#endif
5930
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005932 * Return TRUE when two dictionaries have exactly the same key/values.
5933 */
5934 static int
5935dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 dict_T *d1;
5937 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005938 int ic; /* ignore case for strings */
5939{
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 hashitem_T *hi;
5941 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005942 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005943
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005944 if (d1 == NULL || d2 == NULL)
5945 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005946 if (d1 == d2)
5947 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005948 if (dict_len(d1) != dict_len(d2))
5949 return FALSE;
5950
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005951 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005954 if (!HASHITEM_EMPTY(hi))
5955 {
5956 item2 = dict_find(d2, hi->hi_key, -1);
5957 if (item2 == NULL)
5958 return FALSE;
5959 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5960 return FALSE;
5961 --todo;
5962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005963 }
5964 return TRUE;
5965}
5966
5967/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005969 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 */
5972 static int
5973tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005974 typval_T *tv1;
5975 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005976 int ic; /* ignore case */
5977{
5978 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005979 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005980 static int recursive = 0; /* cach recursive loops */
5981 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005983 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005984 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005985 /* Catch lists and dicts that have an endless loop by limiting
5986 * recursiveness to 1000. We guess they are equal then. */
5987 if (recursive >= 1000)
5988 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005989
5990 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005991 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005992 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005993 ++recursive;
5994 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5995 --recursive;
5996 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005997
5998 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005999 ++recursive;
6000 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6001 --recursive;
6002 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006003
6004 case VAR_FUNC:
6005 return (tv1->vval.v_string != NULL
6006 && tv2->vval.v_string != NULL
6007 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6008
6009 case VAR_NUMBER:
6010 return tv1->vval.v_number == tv2->vval.v_number;
6011
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006012#ifdef FEAT_FLOAT
6013 case VAR_FLOAT:
6014 return tv1->vval.v_float == tv2->vval.v_float;
6015#endif
6016
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006017 case VAR_STRING:
6018 s1 = get_tv_string_buf(tv1, buf1);
6019 s2 = get_tv_string_buf(tv2, buf2);
6020 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006022
6023 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 return TRUE;
6025}
6026
6027/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Locate item with index "n" in list "l" and return it.
6029 * A negative index is counted from the end; -1 is the last item.
6030 * Returns NULL when "n" is out of range.
6031 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 long n;
6036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 long idx;
6039
6040 if (l == NULL)
6041 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006042
6043 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006045 n = l->lv_len + n;
6046
6047 /* Check for index out of range. */
6048 if (n < 0 || n >= l->lv_len)
6049 return NULL;
6050
6051 /* When there is a cached index may start search from there. */
6052 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006054 if (n < l->lv_idx / 2)
6055 {
6056 /* closest to the start of the list */
6057 item = l->lv_first;
6058 idx = 0;
6059 }
6060 else if (n > (l->lv_idx + l->lv_len) / 2)
6061 {
6062 /* closest to the end of the list */
6063 item = l->lv_last;
6064 idx = l->lv_len - 1;
6065 }
6066 else
6067 {
6068 /* closest to the cached index */
6069 item = l->lv_idx_item;
6070 idx = l->lv_idx;
6071 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 }
6073 else
6074 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006075 if (n < l->lv_len / 2)
6076 {
6077 /* closest to the start of the list */
6078 item = l->lv_first;
6079 idx = 0;
6080 }
6081 else
6082 {
6083 /* closest to the end of the list */
6084 item = l->lv_last;
6085 idx = l->lv_len - 1;
6086 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088
6089 while (n > idx)
6090 {
6091 /* search forward */
6092 item = item->li_next;
6093 ++idx;
6094 }
6095 while (n < idx)
6096 {
6097 /* search backward */
6098 item = item->li_prev;
6099 --idx;
6100 }
6101
6102 /* cache the used index */
6103 l->lv_idx = idx;
6104 l->lv_idx_item = item;
6105
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 return item;
6107}
6108
6109/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006110 * Get list item "l[idx]" as a number.
6111 */
6112 static long
6113list_find_nr(l, idx, errorp)
6114 list_T *l;
6115 long idx;
6116 int *errorp; /* set to TRUE when something wrong */
6117{
6118 listitem_T *li;
6119
6120 li = list_find(l, idx);
6121 if (li == NULL)
6122 {
6123 if (errorp != NULL)
6124 *errorp = TRUE;
6125 return -1L;
6126 }
6127 return get_tv_number_chk(&li->li_tv, errorp);
6128}
6129
6130/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006131 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6132 */
6133 char_u *
6134list_find_str(l, idx)
6135 list_T *l;
6136 long idx;
6137{
6138 listitem_T *li;
6139
6140 li = list_find(l, idx - 1);
6141 if (li == NULL)
6142 {
6143 EMSGN(_(e_listidx), idx);
6144 return NULL;
6145 }
6146 return get_tv_string(&li->li_tv);
6147}
6148
6149/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006150 * Locate "item" list "l" and return its index.
6151 * Returns -1 when "item" is not in the list.
6152 */
6153 static long
6154list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 list_T *l;
6156 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006157{
6158 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006160
6161 if (l == NULL)
6162 return -1;
6163 idx = 0;
6164 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6165 ++idx;
6166 if (li == NULL)
6167 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006168 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006169}
6170
6171/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 * Append item "item" to the end of list "l".
6173 */
6174 static void
6175list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 list_T *l;
6177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178{
6179 if (l->lv_last == NULL)
6180 {
6181 /* empty list */
6182 l->lv_first = item;
6183 l->lv_last = item;
6184 item->li_prev = NULL;
6185 }
6186 else
6187 {
6188 l->lv_last->li_next = item;
6189 item->li_prev = l->lv_last;
6190 l->lv_last = item;
6191 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006192 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 item->li_next = NULL;
6194}
6195
6196/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006200 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006202 list_T *l;
6203 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006205 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206
Bram Moolenaar05159a02005-02-26 23:04:13 +00006207 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 copy_tv(tv, &li->li_tv);
6210 list_append(l, li);
6211 return OK;
6212}
6213
6214/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006215 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006216 * Return FAIL when out of memory.
6217 */
6218 int
6219list_append_dict(list, dict)
6220 list_T *list;
6221 dict_T *dict;
6222{
6223 listitem_T *li = listitem_alloc();
6224
6225 if (li == NULL)
6226 return FAIL;
6227 li->li_tv.v_type = VAR_DICT;
6228 li->li_tv.v_lock = 0;
6229 li->li_tv.vval.v_dict = dict;
6230 list_append(list, li);
6231 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232 return OK;
6233}
6234
6235/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006236 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006237 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006238 * Returns FAIL when out of memory.
6239 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006240 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006241list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006242 list_T *l;
6243 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006244 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006245{
6246 listitem_T *li = listitem_alloc();
6247
6248 if (li == NULL)
6249 return FAIL;
6250 list_append(l, li);
6251 li->li_tv.v_type = VAR_STRING;
6252 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006253 if (str == NULL)
6254 li->li_tv.vval.v_string = NULL;
6255 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006256 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006257 return FAIL;
6258 return OK;
6259}
6260
6261/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006262 * Append "n" to list "l".
6263 * Returns FAIL when out of memory.
6264 */
6265 static int
6266list_append_number(l, n)
6267 list_T *l;
6268 varnumber_T n;
6269{
6270 listitem_T *li;
6271
6272 li = listitem_alloc();
6273 if (li == NULL)
6274 return FAIL;
6275 li->li_tv.v_type = VAR_NUMBER;
6276 li->li_tv.v_lock = 0;
6277 li->li_tv.vval.v_number = n;
6278 list_append(l, li);
6279 return OK;
6280}
6281
6282/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284 * If "item" is NULL append at the end.
6285 * Return FAIL when out of memory.
6286 */
6287 static int
6288list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 list_T *l;
6290 typval_T *tv;
6291 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006292{
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006294
6295 if (ni == NULL)
6296 return FAIL;
6297 copy_tv(tv, &ni->li_tv);
6298 if (item == NULL)
6299 /* Append new item at end of list. */
6300 list_append(l, ni);
6301 else
6302 {
6303 /* Insert new item before existing item. */
6304 ni->li_prev = item->li_prev;
6305 ni->li_next = item;
6306 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309 ++l->lv_idx;
6310 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314 l->lv_idx_item = NULL;
6315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006318 }
6319 return OK;
6320}
6321
6322/*
6323 * Extend "l1" with "l2".
6324 * If "bef" is NULL append at the end, otherwise insert before this item.
6325 * Returns FAIL when out of memory.
6326 */
6327 static int
6328list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 list_T *l1;
6330 list_T *l2;
6331 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332{
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006334 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006336 /* We also quit the loop when we have inserted the original item count of
6337 * the list, avoid a hang when we extend a list with itself. */
6338 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6340 return FAIL;
6341 return OK;
6342}
6343
6344/*
6345 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6346 * Return FAIL when out of memory.
6347 */
6348 static int
6349list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 list_T *l1;
6351 list_T *l2;
6352 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353{
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006356 if (l1 == NULL || l2 == NULL)
6357 return FAIL;
6358
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006360 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006361 if (l == NULL)
6362 return FAIL;
6363 tv->v_type = VAR_LIST;
6364 tv->vval.v_list = l;
6365
6366 /* append all items from the second list */
6367 return list_extend(l, l2, NULL);
6368}
6369
6370/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006371 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374 * Returns NULL when out of memory.
6375 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381{
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 list_T *copy;
6383 listitem_T *item;
6384 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385
6386 if (orig == NULL)
6387 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388
6389 copy = list_alloc();
6390 if (copy != NULL)
6391 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006392 if (copyID != 0)
6393 {
6394 /* Do this before adding the items, because one of the items may
6395 * refer back to this list. */
6396 orig->lv_copyID = copyID;
6397 orig->lv_copylist = copy;
6398 }
6399 for (item = orig->lv_first; item != NULL && !got_int;
6400 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401 {
6402 ni = listitem_alloc();
6403 if (ni == NULL)
6404 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006405 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 {
6407 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6408 {
6409 vim_free(ni);
6410 break;
6411 }
6412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 list_append(copy, ni);
6416 }
6417 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006418 if (item != NULL)
6419 {
6420 list_unref(copy);
6421 copy = NULL;
6422 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 }
6424
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return copy;
6426}
6427
6428/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006430 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006433list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 list_T *l;
6435 listitem_T *item;
6436 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437{
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 /* notify watchers */
6441 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006443 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444 list_fix_watch(l, ip);
6445 if (ip == item2)
6446 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448
6449 if (item2->li_next == NULL)
6450 l->lv_last = item->li_prev;
6451 else
6452 item2->li_next->li_prev = item->li_prev;
6453 if (item->li_prev == NULL)
6454 l->lv_first = item2->li_next;
6455 else
6456 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458}
6459
6460/*
6461 * Return an allocated string with the string representation of a list.
6462 * May return NULL.
6463 */
6464 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006465list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006467 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468{
6469 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470
6471 if (tv->vval.v_list == NULL)
6472 return NULL;
6473 ga_init2(&ga, (int)sizeof(char), 80);
6474 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006475 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006476 {
6477 vim_free(ga.ga_data);
6478 return NULL;
6479 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 ga_append(&ga, ']');
6481 ga_append(&ga, NUL);
6482 return (char_u *)ga.ga_data;
6483}
6484
6485/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006486 * Join list "l" into a string in "*gap", using separator "sep".
6487 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006489 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006491list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006492 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006494 char_u *sep;
6495 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006496 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006497{
6498 int first = TRUE;
6499 char_u *tofree;
6500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006502 char_u *s;
6503
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006505 {
6506 if (first)
6507 first = FALSE;
6508 else
6509 ga_concat(gap, sep);
6510
6511 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006512 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006514 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515 if (s != NULL)
6516 ga_concat(gap, s);
6517 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 if (s == NULL)
6519 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006520 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006521 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523}
6524
6525/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006526 * Garbage collection for lists and dictionaries.
6527 *
6528 * We use reference counts to be able to free most items right away when they
6529 * are no longer used. But for composite items it's possible that it becomes
6530 * unused while the reference count is > 0: When there is a recursive
6531 * reference. Example:
6532 * :let l = [1, 2, 3]
6533 * :let d = {9: l}
6534 * :let l[1] = d
6535 *
6536 * Since this is quite unusual we handle this with garbage collection: every
6537 * once in a while find out which lists and dicts are not referenced from any
6538 * variable.
6539 *
6540 * Here is a good reference text about garbage collection (refers to Python
6541 * but it applies to all reference-counting mechanisms):
6542 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006543 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006544
6545/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006546 * Do garbage collection for lists and dicts.
6547 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006548 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006549 int
6550garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006551{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006552 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006553 buf_T *buf;
6554 win_T *wp;
6555 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006556 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006557 int did_free;
6558 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006559#ifdef FEAT_WINDOWS
6560 tabpage_T *tp;
6561#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006562
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006563 /* Only do this once. */
6564 want_garbage_collect = FALSE;
6565 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006566 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006567
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006568 /* We advance by two because we add one for items referenced through
6569 * previous_funccal. */
6570 current_copyID += COPYID_INC;
6571 copyID = current_copyID;
6572
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006573 /*
6574 * 1. Go through all accessible variables and mark all lists and dicts
6575 * with copyID.
6576 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006577
6578 /* Don't free variables in the previous_funccal list unless they are only
6579 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006580 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006581 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6582 {
6583 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6584 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6585 }
6586
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006587 /* script-local variables */
6588 for (i = 1; i <= ga_scripts.ga_len; ++i)
6589 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6590
6591 /* buffer-local variables */
6592 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6593 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6594
6595 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006596 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006597 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6598
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006599#ifdef FEAT_WINDOWS
6600 /* tabpage-local variables */
6601 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6602 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6603#endif
6604
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006605 /* global variables */
6606 set_ref_in_ht(&globvarht, copyID);
6607
6608 /* function-local variables */
6609 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6610 {
6611 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6612 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6613 }
6614
Bram Moolenaard812df62008-11-09 12:46:09 +00006615 /* v: vars */
6616 set_ref_in_ht(&vimvarht, copyID);
6617
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006618 /*
6619 * 2. Free lists and dictionaries that are not referenced.
6620 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006621 did_free = free_unref_items(copyID);
6622
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006623 /*
6624 * 3. Check if any funccal can be freed now.
6625 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006626 for (pfc = &previous_funccal; *pfc != NULL; )
6627 {
6628 if (can_free_funccal(*pfc, copyID))
6629 {
6630 fc = *pfc;
6631 *pfc = fc->caller;
6632 free_funccal(fc, TRUE);
6633 did_free = TRUE;
6634 did_free_funccal = TRUE;
6635 }
6636 else
6637 pfc = &(*pfc)->caller;
6638 }
6639 if (did_free_funccal)
6640 /* When a funccal was freed some more items might be garbage
6641 * collected, so run again. */
6642 (void)garbage_collect();
6643
6644 return did_free;
6645}
6646
6647/*
6648 * Free lists and dictionaries that are no longer referenced.
6649 */
6650 static int
6651free_unref_items(copyID)
6652 int copyID;
6653{
6654 dict_T *dd;
6655 list_T *ll;
6656 int did_free = FALSE;
6657
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006658 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006660 */
6661 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006662 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006663 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006664 /* Free the Dictionary and ordinary items it contains, but don't
6665 * recurse into Lists and Dictionaries, they will be in the list
6666 * of dicts or list of lists. */
6667 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006668 did_free = TRUE;
6669
6670 /* restart, next dict may also have been freed */
6671 dd = first_dict;
6672 }
6673 else
6674 dd = dd->dv_used_next;
6675
6676 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006677 * Go through the list of lists and free items without the copyID.
6678 * But don't free a list that has a watcher (used in a for loop), these
6679 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 */
6681 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006682 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6683 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006685 /* Free the List and ordinary items it contains, but don't recurse
6686 * into Lists and Dictionaries, they will be in the list of dicts
6687 * or list of lists. */
6688 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006689 did_free = TRUE;
6690
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006691 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006692 ll = first_list;
6693 }
6694 else
6695 ll = ll->lv_used_next;
6696
6697 return did_free;
6698}
6699
6700/*
6701 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6702 */
6703 static void
6704set_ref_in_ht(ht, copyID)
6705 hashtab_T *ht;
6706 int copyID;
6707{
6708 int todo;
6709 hashitem_T *hi;
6710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006711 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 for (hi = ht->ht_array; todo > 0; ++hi)
6713 if (!HASHITEM_EMPTY(hi))
6714 {
6715 --todo;
6716 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6717 }
6718}
6719
6720/*
6721 * Mark all lists and dicts referenced through list "l" with "copyID".
6722 */
6723 static void
6724set_ref_in_list(l, copyID)
6725 list_T *l;
6726 int copyID;
6727{
6728 listitem_T *li;
6729
6730 for (li = l->lv_first; li != NULL; li = li->li_next)
6731 set_ref_in_item(&li->li_tv, copyID);
6732}
6733
6734/*
6735 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6736 */
6737 static void
6738set_ref_in_item(tv, copyID)
6739 typval_T *tv;
6740 int copyID;
6741{
6742 dict_T *dd;
6743 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006744
6745 switch (tv->v_type)
6746 {
6747 case VAR_DICT:
6748 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006749 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006750 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006751 /* Didn't see this dict yet. */
6752 dd->dv_copyID = copyID;
6753 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006755 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006756
6757 case VAR_LIST:
6758 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006759 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006761 /* Didn't see this list yet. */
6762 ll->lv_copyID = copyID;
6763 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006768}
6769
6770/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 * Allocate an empty header for a dictionary.
6772 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006773 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774dict_alloc()
6775{
Bram Moolenaar33570922005-01-25 22:26:29 +00006776 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777
Bram Moolenaar33570922005-01-25 22:26:29 +00006778 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006780 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006781 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006782 if (first_dict != NULL)
6783 first_dict->dv_used_prev = d;
6784 d->dv_used_next = first_dict;
6785 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006786 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006789 d->dv_lock = 0;
6790 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006792 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006793 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794}
6795
6796/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006797 * Allocate an empty dict for a return value.
6798 * Returns OK or FAIL.
6799 */
6800 static int
6801rettv_dict_alloc(rettv)
6802 typval_T *rettv;
6803{
6804 dict_T *d = dict_alloc();
6805
6806 if (d == NULL)
6807 return FAIL;
6808
6809 rettv->vval.v_dict = d;
6810 rettv->v_type = VAR_DICT;
6811 ++d->dv_refcount;
6812 return OK;
6813}
6814
6815
6816/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 * Unreference a Dictionary: decrement the reference count and free it when it
6818 * becomes zero.
6819 */
6820 static void
6821dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006824 if (d != NULL && --d->dv_refcount <= 0)
6825 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006826}
6827
6828/*
6829 * Free a Dictionary, including all items it contains.
6830 * Ignores the reference count.
6831 */
6832 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006833dict_free(d, recurse)
6834 dict_T *d;
6835 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006837 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006840
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 /* Remove the dict from the list of dicts for garbage collection. */
6842 if (d->dv_used_prev == NULL)
6843 first_dict = d->dv_used_next;
6844 else
6845 d->dv_used_prev->dv_used_next = d->dv_used_next;
6846 if (d->dv_used_next != NULL)
6847 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6848
6849 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006851 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006854 if (!HASHITEM_EMPTY(hi))
6855 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856 /* Remove the item before deleting it, just in case there is
6857 * something recursive causing trouble. */
6858 di = HI2DI(hi);
6859 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006860 if (recurse || (di->di_tv.v_type != VAR_LIST
6861 && di->di_tv.v_type != VAR_DICT))
6862 clear_tv(&di->di_tv);
6863 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006864 --todo;
6865 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006866 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006868 vim_free(d);
6869}
6870
6871/*
6872 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006873 * The "key" is copied to the new item.
6874 * Note that the value of the item "di_tv" still needs to be initialized!
6875 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006877 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878dictitem_alloc(key)
6879 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880{
Bram Moolenaar33570922005-01-25 22:26:29 +00006881 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006883 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006886 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006887 di->di_flags = 0;
6888 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006889 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006890}
6891
6892/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006893 * Make a copy of a Dictionary item.
6894 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898{
Bram Moolenaar33570922005-01-25 22:26:29 +00006899 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006901 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6902 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 if (di != NULL)
6904 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006906 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006907 copy_tv(&org->di_tv, &di->di_tv);
6908 }
6909 return di;
6910}
6911
6912/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006913 * Remove item "item" from Dictionary "dict" and free it.
6914 */
6915 static void
6916dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 dict_T *dict;
6918 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006919{
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006921
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006923 if (HASHITEM_EMPTY(hi))
6924 EMSG2(_(e_intern2), "dictitem_remove()");
6925 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006926 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006927 dictitem_free(item);
6928}
6929
6930/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006931 * Free a dict item. Also clears the value.
6932 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006933 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006937 clear_tv(&item->di_tv);
6938 vim_free(item);
6939}
6940
6941/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6943 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 * Returns NULL when out of memory.
6946 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006948dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006951 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952{
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 dict_T *copy;
6954 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006957
6958 if (orig == NULL)
6959 return NULL;
6960
6961 copy = dict_alloc();
6962 if (copy != NULL)
6963 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964 if (copyID != 0)
6965 {
6966 orig->dv_copyID = copyID;
6967 orig->dv_copydict = copy;
6968 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006969 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006970 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 --todo;
6975
6976 di = dictitem_alloc(hi->hi_key);
6977 if (di == NULL)
6978 break;
6979 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006980 {
6981 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6982 copyID) == FAIL)
6983 {
6984 vim_free(di);
6985 break;
6986 }
6987 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988 else
6989 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6990 if (dict_add(copy, di) == FAIL)
6991 {
6992 dictitem_free(di);
6993 break;
6994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997
Bram Moolenaare9a41262005-01-15 22:18:47 +00006998 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006999 if (todo > 0)
7000 {
7001 dict_unref(copy);
7002 copy = NULL;
7003 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007004 }
7005
7006 return copy;
7007}
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007011 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007012 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007013 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 dict_T *d;
7016 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017{
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019}
7020
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007022 * Add a number or string entry to dictionary "d".
7023 * When "str" is NULL use number "nr", otherwise use "str".
7024 * Returns FAIL when out of memory and when key already exists.
7025 */
7026 int
7027dict_add_nr_str(d, key, nr, str)
7028 dict_T *d;
7029 char *key;
7030 long nr;
7031 char_u *str;
7032{
7033 dictitem_T *item;
7034
7035 item = dictitem_alloc((char_u *)key);
7036 if (item == NULL)
7037 return FAIL;
7038 item->di_tv.v_lock = 0;
7039 if (str == NULL)
7040 {
7041 item->di_tv.v_type = VAR_NUMBER;
7042 item->di_tv.vval.v_number = nr;
7043 }
7044 else
7045 {
7046 item->di_tv.v_type = VAR_STRING;
7047 item->di_tv.vval.v_string = vim_strsave(str);
7048 }
7049 if (dict_add(d, item) == FAIL)
7050 {
7051 dictitem_free(item);
7052 return FAIL;
7053 }
7054 return OK;
7055}
7056
7057/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007058 * Add a list entry to dictionary "d".
7059 * Returns FAIL when out of memory and when key already exists.
7060 */
7061 int
7062dict_add_list(d, key, list)
7063 dict_T *d;
7064 char *key;
7065 list_T *list;
7066{
7067 dictitem_T *item;
7068
7069 item = dictitem_alloc((char_u *)key);
7070 if (item == NULL)
7071 return FAIL;
7072 item->di_tv.v_lock = 0;
7073 item->di_tv.v_type = VAR_LIST;
7074 item->di_tv.vval.v_list = list;
7075 if (dict_add(d, item) == FAIL)
7076 {
7077 dictitem_free(item);
7078 return FAIL;
7079 }
7080 return OK;
7081}
7082
7083/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084 * Get the number of items in a Dictionary.
7085 */
7086 static long
7087dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090 if (d == NULL)
7091 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007092 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093}
7094
7095/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 * Find item "key[len]" in Dictionary "d".
7097 * If "len" is negative use strlen(key).
7098 * Returns NULL when not found.
7099 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007100 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 char_u *key;
7104 int len;
7105{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106#define AKEYLEN 200
7107 char_u buf[AKEYLEN];
7108 char_u *akey;
7109 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (len < 0)
7113 akey = key;
7114 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 tofree = akey = vim_strnsave(key, len);
7117 if (akey == NULL)
7118 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007119 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 else
7121 {
7122 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007123 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 akey = buf;
7125 }
7126
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 vim_free(tofree);
7129 if (HASHITEM_EMPTY(hi))
7130 return NULL;
7131 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132}
7133
7134/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007135 * Get a string item from a dictionary.
7136 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007137 * Returns NULL if the entry doesn't exist or out of memory.
7138 */
7139 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007140get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007141 dict_T *d;
7142 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007143 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007144{
7145 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007146 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007147
7148 di = dict_find(d, key, -1);
7149 if (di == NULL)
7150 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007151 s = get_tv_string(&di->di_tv);
7152 if (save && s != NULL)
7153 s = vim_strsave(s);
7154 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007155}
7156
7157/*
7158 * Get a number item from a dictionary.
7159 * Returns 0 if the entry doesn't exist or out of memory.
7160 */
7161 long
7162get_dict_number(d, key)
7163 dict_T *d;
7164 char_u *key;
7165{
7166 dictitem_T *di;
7167
7168 di = dict_find(d, key, -1);
7169 if (di == NULL)
7170 return 0;
7171 return get_tv_number(&di->di_tv);
7172}
7173
7174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 * Return an allocated string with the string representation of a Dictionary.
7176 * May return NULL.
7177 */
7178 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007179dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007181 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182{
7183 garray_T ga;
7184 int first = TRUE;
7185 char_u *tofree;
7186 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 return NULL;
7194 ga_init2(&ga, (int)sizeof(char), 80);
7195 ga_append(&ga, '{');
7196
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 --todo;
7203
7204 if (first)
7205 first = FALSE;
7206 else
7207 ga_concat(&ga, (char_u *)", ");
7208
7209 tofree = string_quote(hi->hi_key, FALSE);
7210 if (tofree != NULL)
7211 {
7212 ga_concat(&ga, tofree);
7213 vim_free(tofree);
7214 }
7215 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007216 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 if (s != NULL)
7218 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 if (s == NULL)
7221 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007224 if (todo > 0)
7225 {
7226 vim_free(ga.ga_data);
7227 return NULL;
7228 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229
7230 ga_append(&ga, '}');
7231 ga_append(&ga, NUL);
7232 return (char_u *)ga.ga_data;
7233}
7234
7235/*
7236 * Allocate a variable for a Dictionary and fill it from "*arg".
7237 * Return OK or FAIL. Returns NOTDONE for {expr}.
7238 */
7239 static int
7240get_dict_tv(arg, rettv, evaluate)
7241 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 int evaluate;
7244{
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 dict_T *d = NULL;
7246 typval_T tvkey;
7247 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007248 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252
7253 /*
7254 * First check if it's not a curly-braces thing: {expr}.
7255 * Must do this without evaluating, otherwise a function may be called
7256 * twice. Unfortunately this means we need to call eval1() twice for the
7257 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260 if (*start != '}')
7261 {
7262 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7263 return FAIL;
7264 if (*start == '}')
7265 return NOTDONE;
7266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267
7268 if (evaluate)
7269 {
7270 d = dict_alloc();
7271 if (d == NULL)
7272 return FAIL;
7273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 tvkey.v_type = VAR_UNKNOWN;
7275 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276
7277 *arg = skipwhite(*arg + 1);
7278 while (**arg != '}' && **arg != NUL)
7279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 goto failret;
7282 if (**arg != ':')
7283 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007284 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 goto failret;
7287 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007288 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007290 key = get_tv_string_buf_chk(&tvkey, buf);
7291 if (key == NULL || *key == NUL)
7292 {
7293 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7294 if (key != NULL)
7295 EMSG(_(e_emptykey));
7296 clear_tv(&tvkey);
7297 goto failret;
7298 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300
7301 *arg = skipwhite(*arg + 1);
7302 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7303 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007304 if (evaluate)
7305 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 goto failret;
7307 }
7308 if (evaluate)
7309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 if (item != NULL)
7312 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007313 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 clear_tv(&tv);
7316 goto failret;
7317 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 item = dictitem_alloc(key);
7319 clear_tv(&tvkey);
7320 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007323 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 if (dict_add(d, item) == FAIL)
7325 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 }
7327 }
7328
7329 if (**arg == '}')
7330 break;
7331 if (**arg != ',')
7332 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007333 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334 goto failret;
7335 }
7336 *arg = skipwhite(*arg + 1);
7337 }
7338
7339 if (**arg != '}')
7340 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007341 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342failret:
7343 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007344 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 return FAIL;
7346 }
7347
7348 *arg = skipwhite(*arg + 1);
7349 if (evaluate)
7350 {
7351 rettv->v_type = VAR_DICT;
7352 rettv->vval.v_dict = d;
7353 ++d->dv_refcount;
7354 }
7355
7356 return OK;
7357}
7358
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 * Return a string with the string representation of a variable.
7361 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007362 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007363 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007365 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 */
7367 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007368echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007371 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007372 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 static int recurse = 0;
7375 char_u *r = NULL;
7376
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007379 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 *tofree = NULL;
7381 return NULL;
7382 }
7383 ++recurse;
7384
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007385 switch (tv->v_type)
7386 {
7387 case VAR_FUNC:
7388 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 r = tv->vval.v_string;
7390 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007391
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007392 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007393 if (tv->vval.v_list == NULL)
7394 {
7395 *tofree = NULL;
7396 r = NULL;
7397 }
7398 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7399 {
7400 *tofree = NULL;
7401 r = (char_u *)"[...]";
7402 }
7403 else
7404 {
7405 tv->vval.v_list->lv_copyID = copyID;
7406 *tofree = list2string(tv, copyID);
7407 r = *tofree;
7408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007412 if (tv->vval.v_dict == NULL)
7413 {
7414 *tofree = NULL;
7415 r = NULL;
7416 }
7417 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7418 {
7419 *tofree = NULL;
7420 r = (char_u *)"{...}";
7421 }
7422 else
7423 {
7424 tv->vval.v_dict->dv_copyID = copyID;
7425 *tofree = dict2string(tv, copyID);
7426 r = *tofree;
7427 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007430 case VAR_STRING:
7431 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 *tofree = NULL;
7433 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007434 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007436#ifdef FEAT_FLOAT
7437 case VAR_FLOAT:
7438 *tofree = NULL;
7439 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7440 r = numbuf;
7441 break;
7442#endif
7443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007444 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007445 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007446 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448
7449 --recurse;
7450 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007451}
7452
7453/*
7454 * Return a string with the string representation of a variable.
7455 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7456 * "numbuf" is used for a number.
7457 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007458 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007459 */
7460 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007461tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007463 char_u **tofree;
7464 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007465 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007466{
7467 switch (tv->v_type)
7468 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007469 case VAR_FUNC:
7470 *tofree = string_quote(tv->vval.v_string, TRUE);
7471 return *tofree;
7472 case VAR_STRING:
7473 *tofree = string_quote(tv->vval.v_string, FALSE);
7474 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007475#ifdef FEAT_FLOAT
7476 case VAR_FLOAT:
7477 *tofree = NULL;
7478 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7479 return numbuf;
7480#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007482 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007484 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007485 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007487 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007488 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007489}
7490
7491/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 * Return string "str" in ' quotes, doubling ' characters.
7493 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007495 */
7496 static char_u *
7497string_quote(str, function)
7498 char_u *str;
7499 int function;
7500{
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007502 char_u *p, *r, *s;
7503
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 len = (function ? 13 : 3);
7505 if (str != NULL)
7506 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007507 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 for (p = str; *p != NUL; mb_ptr_adv(p))
7509 if (*p == '\'')
7510 ++len;
7511 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007512 s = r = alloc(len);
7513 if (r != NULL)
7514 {
7515 if (function)
7516 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007518 r += 10;
7519 }
7520 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 if (str != NULL)
7523 for (p = str; *p != NUL; )
7524 {
7525 if (*p == '\'')
7526 *r++ = '\'';
7527 MB_COPY_CHAR(p, r);
7528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530 if (function)
7531 *r++ = ')';
7532 *r++ = NUL;
7533 }
7534 return s;
7535}
7536
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007537#ifdef FEAT_FLOAT
7538/*
7539 * Convert the string "text" to a floating point number.
7540 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7541 * this always uses a decimal point.
7542 * Returns the length of the text that was consumed.
7543 */
7544 static int
7545string2float(text, value)
7546 char_u *text;
7547 float_T *value; /* result stored here */
7548{
7549 char *s = (char *)text;
7550 float_T f;
7551
7552 f = strtod(s, &s);
7553 *value = f;
7554 return (int)((char_u *)s - text);
7555}
7556#endif
7557
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 * Get the value of an environment variable.
7560 * "arg" is pointing to the '$'. It is advanced to after the name.
7561 * If the environment variable was not set, silently assume it is empty.
7562 * Always return OK.
7563 */
7564 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 int evaluate;
7569{
7570 char_u *string = NULL;
7571 int len;
7572 int cc;
7573 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007574 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575
7576 ++*arg;
7577 name = *arg;
7578 len = get_env_len(arg);
7579 if (evaluate)
7580 {
7581 if (len != 0)
7582 {
7583 cc = name[len];
7584 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007585 /* first try vim_getenv(), fast for normal environment vars */
7586 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007588 {
7589 if (!mustfree)
7590 string = vim_strsave(string);
7591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 else
7593 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007594 if (mustfree)
7595 vim_free(string);
7596
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 /* next try expanding things like $VIM and ${HOME} */
7598 string = expand_env_save(name - 1);
7599 if (string != NULL && *string == '$')
7600 {
7601 vim_free(string);
7602 string = NULL;
7603 }
7604 }
7605 name[len] = cc;
7606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 rettv->v_type = VAR_STRING;
7608 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610
7611 return OK;
7612}
7613
7614/*
7615 * Array with names and number of arguments of all internal functions
7616 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7617 */
7618static struct fst
7619{
7620 char *f_name; /* function name */
7621 char f_min_argc; /* minimal number of arguments */
7622 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007624 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625} functions[] =
7626{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007627#ifdef FEAT_FLOAT
7628 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007629 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007630#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007631 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {"append", 2, 2, f_append},
7633 {"argc", 0, 0, f_argc},
7634 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007635 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007637 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007638 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007639 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007642 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {"bufexists", 1, 1, f_bufexists},
7644 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7645 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7646 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7647 {"buflisted", 1, 1, f_buflisted},
7648 {"bufloaded", 1, 1, f_bufloaded},
7649 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007650 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"bufwinnr", 1, 1, f_bufwinnr},
7652 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007653 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007655#ifdef FEAT_FLOAT
7656 {"ceil", 1, 1, f_ceil},
7657#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007658 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {"char2nr", 1, 1, f_char2nr},
7660 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007661 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007663#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007664 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007665 {"complete_add", 1, 1, f_complete_add},
7666 {"complete_check", 0, 0, f_complete_check},
7667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007672 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007673#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007676 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007677 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {"delete", 1, 1, f_delete},
7679 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007680 {"diff_filler", 1, 1, f_diff_filler},
7681 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007682 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {"eventhandler", 0, 0, f_eventhandler},
7686 {"executable", 1, 1, f_executable},
7687 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007688#ifdef FEAT_FLOAT
7689 {"exp", 1, 1, f_exp},
7690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007692 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007693 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7695 {"filereadable", 1, 1, f_filereadable},
7696 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007697 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007698 {"finddir", 1, 3, f_finddir},
7699 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701 {"float2nr", 1, 1, f_float2nr},
7702 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007703 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007705 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {"fnamemodify", 2, 2, f_fnamemodify},
7707 {"foldclosed", 1, 1, f_foldclosed},
7708 {"foldclosedend", 1, 1, f_foldclosedend},
7709 {"foldlevel", 1, 1, f_foldlevel},
7710 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007711 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007714 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007715 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007716 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {"getbufvar", 2, 2, f_getbufvar},
7718 {"getchar", 0, 1, f_getchar},
7719 {"getcharmod", 0, 0, f_getcharmod},
7720 {"getcmdline", 0, 0, f_getcmdline},
7721 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007722 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007724 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007725 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {"getfsize", 1, 1, f_getfsize},
7727 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007728 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007729 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007730 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007731 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007732 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007733 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007734 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007735 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007737 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007738 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"getwinposx", 0, 0, f_getwinposx},
7740 {"getwinposy", 0, 0, f_getwinposy},
7741 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007742 {"glob", 1, 2, f_glob},
7743 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007745 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007746 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007747 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7749 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7750 {"histadd", 2, 2, f_histadd},
7751 {"histdel", 1, 2, f_histdel},
7752 {"histget", 1, 2, f_histget},
7753 {"histnr", 1, 1, f_histnr},
7754 {"hlID", 1, 1, f_hlID},
7755 {"hlexists", 1, 1, f_hlexists},
7756 {"hostname", 0, 0, f_hostname},
7757 {"iconv", 3, 3, f_iconv},
7758 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007760 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007762 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {"inputrestore", 0, 0, f_inputrestore},
7764 {"inputsave", 0, 0, f_inputsave},
7765 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007768 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"libcall", 3, 3, f_libcall},
7775 {"libcallnr", 3, 3, f_libcallnr},
7776 {"line", 1, 1, f_line},
7777 {"line2byte", 1, 1, f_line2byte},
7778 {"lispindent", 1, 1, f_lispindent},
7779 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007780#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007781 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007782 {"log10", 1, 1, f_log10},
7783#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007785 {"maparg", 1, 3, f_maparg},
7786 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007787 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007788 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007789 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007790 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007791 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007792 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007793 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007794 {"max", 1, 1, f_max},
7795 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007796#ifdef vim_mkdir
7797 {"mkdir", 1, 3, f_mkdir},
7798#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007799 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007800#ifdef FEAT_MZSCHEME
7801 {"mzeval", 1, 1, f_mzeval},
7802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"nextnonblank", 1, 1, f_nextnonblank},
7804 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007805 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007806#ifdef FEAT_FLOAT
7807 {"pow", 2, 2, f_pow},
7808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007810 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007811 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007813 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007814 {"reltime", 0, 2, f_reltime},
7815 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {"remote_expr", 2, 3, f_remote_expr},
7817 {"remote_foreground", 1, 1, f_remote_foreground},
7818 {"remote_peek", 1, 2, f_remote_peek},
7819 {"remote_read", 1, 1, f_remote_read},
7820 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007821 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007823 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007825 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827 {"round", 1, 1, f_round},
7828#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007829 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007830 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007831 {"searchpair", 3, 7, f_searchpair},
7832 {"searchpairpos", 3, 7, f_searchpairpos},
7833 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"server2client", 2, 2, f_server2client},
7835 {"serverlist", 0, 0, f_serverlist},
7836 {"setbufvar", 3, 3, f_setbufvar},
7837 {"setcmdpos", 1, 1, f_setcmdpos},
7838 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007839 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007840 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007841 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007842 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007844 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007845 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007847 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#ifdef FEAT_FLOAT
7850 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007851 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007853 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007854 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007855 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007856 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007857 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007858#ifdef FEAT_FLOAT
7859 {"sqrt", 1, 1, f_sqrt},
7860 {"str2float", 1, 1, f_str2float},
7861#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007862 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007863 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007864 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865#ifdef HAVE_STRFTIME
7866 {"strftime", 1, 2, f_strftime},
7867#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"strlen", 1, 1, f_strlen},
7871 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007872 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007874 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"submatch", 1, 1, f_submatch},
7876 {"substitute", 4, 4, f_substitute},
7877 {"synID", 3, 3, f_synID},
7878 {"synIDattr", 2, 3, f_synIDattr},
7879 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007880 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007881 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007882 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007883 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007884 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007885 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007886 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007887 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007888#ifdef FEAT_FLOAT
7889 {"tan", 1, 1, f_tan},
7890 {"tanh", 1, 1, f_tanh},
7891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007893 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"tolower", 1, 1, f_tolower},
7895 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007896 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#ifdef FEAT_FLOAT
7898 {"trunc", 1, 1, f_trunc},
7899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007901 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007902 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007903 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"virtcol", 1, 1, f_virtcol},
7905 {"visualmode", 0, 1, f_visualmode},
7906 {"winbufnr", 1, 1, f_winbufnr},
7907 {"wincol", 0, 0, f_wincol},
7908 {"winheight", 1, 1, f_winheight},
7909 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007910 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007912 {"winrestview", 1, 1, f_winrestview},
7913 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007915 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916};
7917
7918#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7919
7920/*
7921 * Function given to ExpandGeneric() to obtain the list of internal
7922 * or user defined function names.
7923 */
7924 char_u *
7925get_function_name(xp, idx)
7926 expand_T *xp;
7927 int idx;
7928{
7929 static int intidx = -1;
7930 char_u *name;
7931
7932 if (idx == 0)
7933 intidx = -1;
7934 if (intidx < 0)
7935 {
7936 name = get_user_func_name(xp, idx);
7937 if (name != NULL)
7938 return name;
7939 }
7940 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7941 {
7942 STRCPY(IObuff, functions[intidx].f_name);
7943 STRCAT(IObuff, "(");
7944 if (functions[intidx].f_max_argc == 0)
7945 STRCAT(IObuff, ")");
7946 return IObuff;
7947 }
7948
7949 return NULL;
7950}
7951
7952/*
7953 * Function given to ExpandGeneric() to obtain the list of internal or
7954 * user defined variable or function names.
7955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 char_u *
7957get_expr_name(xp, idx)
7958 expand_T *xp;
7959 int idx;
7960{
7961 static int intidx = -1;
7962 char_u *name;
7963
7964 if (idx == 0)
7965 intidx = -1;
7966 if (intidx < 0)
7967 {
7968 name = get_function_name(xp, idx);
7969 if (name != NULL)
7970 return name;
7971 }
7972 return get_user_var_name(xp, ++intidx);
7973}
7974
7975#endif /* FEAT_CMDL_COMPL */
7976
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007977#if defined(EBCDIC) || defined(PROTO)
7978/*
7979 * Compare struct fst by function name.
7980 */
7981 static int
7982compare_func_name(s1, s2)
7983 const void *s1;
7984 const void *s2;
7985{
7986 struct fst *p1 = (struct fst *)s1;
7987 struct fst *p2 = (struct fst *)s2;
7988
7989 return STRCMP(p1->f_name, p2->f_name);
7990}
7991
7992/*
7993 * Sort the function table by function name.
7994 * The sorting of the table above is ASCII dependant.
7995 * On machines using EBCDIC we have to sort it.
7996 */
7997 static void
7998sortFunctions()
7999{
8000 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8001
8002 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8003}
8004#endif
8005
8006
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007/*
8008 * Find internal function in table above.
8009 * Return index, or -1 if not found
8010 */
8011 static int
8012find_internal_func(name)
8013 char_u *name; /* name of the function */
8014{
8015 int first = 0;
8016 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8017 int cmp;
8018 int x;
8019
8020 /*
8021 * Find the function name in the table. Binary search.
8022 */
8023 while (first <= last)
8024 {
8025 x = first + ((unsigned)(last - first) >> 1);
8026 cmp = STRCMP(name, functions[x].f_name);
8027 if (cmp < 0)
8028 last = x - 1;
8029 else if (cmp > 0)
8030 first = x + 1;
8031 else
8032 return x;
8033 }
8034 return -1;
8035}
8036
8037/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008038 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8039 * name it contains, otherwise return "name".
8040 */
8041 static char_u *
8042deref_func_name(name, lenp)
8043 char_u *name;
8044 int *lenp;
8045{
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008047 int cc;
8048
8049 cc = name[*lenp];
8050 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008051 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008056 {
8057 *lenp = 0;
8058 return (char_u *)""; /* just in case */
8059 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008060 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008062 }
8063
8064 return name;
8065}
8066
8067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 * Allocate a variable for the result of a function.
8069 * Return OK or FAIL.
8070 */
8071 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008072get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8073 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 char_u *name; /* name of the function */
8075 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 char_u **arg; /* argument, pointing to the '(' */
8078 linenr_T firstline; /* first line of range */
8079 linenr_T lastline; /* last line of range */
8080 int *doesrange; /* return: function handled range */
8081 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 char_u *argp;
8085 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008086 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 int argcount = 0; /* number of arguments found */
8088
8089 /*
8090 * Get the arguments.
8091 */
8092 argp = *arg;
8093 while (argcount < MAX_FUNC_ARGS)
8094 {
8095 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8096 if (*argp == ')' || *argp == ',' || *argp == NUL)
8097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8099 {
8100 ret = FAIL;
8101 break;
8102 }
8103 ++argcount;
8104 if (*argp != ',')
8105 break;
8106 }
8107 if (*argp == ')')
8108 ++argp;
8109 else
8110 ret = FAIL;
8111
8112 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008114 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 {
8117 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008118 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008120 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122
8123 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125
8126 *arg = skipwhite(argp);
8127 return ret;
8128}
8129
8130
8131/*
8132 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008133 * Return OK when the function can't be called, FAIL otherwise.
8134 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 */
8136 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008137call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008138 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008139 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008143 typval_T *argvars; /* vars for arguments, must have "argcount"
8144 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 linenr_T firstline; /* first line of range */
8146 linenr_T lastline; /* last line of range */
8147 int *doesrange; /* return: function handled range */
8148 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
8151 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#define ERROR_UNKNOWN 0
8153#define ERROR_TOOMANY 1
8154#define ERROR_TOOFEW 2
8155#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156#define ERROR_DICT 4
8157#define ERROR_NONE 5
8158#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 int error = ERROR_NONE;
8160 int i;
8161 int llen;
8162 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163#define FLEN_FIXED 40
8164 char_u fname_buf[FLEN_FIXED + 1];
8165 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008166 char_u *name;
8167
8168 /* Make a copy of the name, if it comes from a funcref variable it could
8169 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008170 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008171 if (name == NULL)
8172 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173
8174 /*
8175 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8176 * Change <SNR>123_name() to K_SNR 123_name().
8177 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 llen = eval_fname_script(name);
8180 if (llen > 0)
8181 {
8182 fname_buf[0] = K_SPECIAL;
8183 fname_buf[1] = KS_EXTRA;
8184 fname_buf[2] = (int)KE_SNR;
8185 i = 3;
8186 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8187 {
8188 if (current_SID <= 0)
8189 error = ERROR_SCRIPT;
8190 else
8191 {
8192 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8193 i = (int)STRLEN(fname_buf);
8194 }
8195 }
8196 if (i + STRLEN(name + llen) < FLEN_FIXED)
8197 {
8198 STRCPY(fname_buf + i, name + llen);
8199 fname = fname_buf;
8200 }
8201 else
8202 {
8203 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8204 if (fname == NULL)
8205 error = ERROR_OTHER;
8206 else
8207 {
8208 mch_memmove(fname, fname_buf, (size_t)i);
8209 STRCPY(fname + i, name + llen);
8210 }
8211 }
8212 }
8213 else
8214 fname = name;
8215
8216 *doesrange = FALSE;
8217
8218
8219 /* execute the function if no errors detected and executing */
8220 if (evaluate && error == ERROR_NONE)
8221 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008222 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8223 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 error = ERROR_UNKNOWN;
8225
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008226 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
8228 /*
8229 * User defined function.
8230 */
8231 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008234 /* Trigger FuncUndefined event, may load the function. */
8235 if (fp == NULL
8236 && apply_autocmds(EVENT_FUNCUNDEFINED,
8237 fname, fname, TRUE, NULL)
8238 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008240 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 fp = find_func(fname);
8242 }
8243#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008244 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008245 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008246 {
8247 /* loaded a package, search for the function again */
8248 fp = find_func(fname);
8249 }
8250
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (fp != NULL)
8252 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008253 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008255 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008257 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008259 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 else
8262 {
8263 /*
8264 * Call the user function.
8265 * Save and restore search patterns, script variables and
8266 * redo buffer.
8267 */
8268 save_search_patterns();
8269 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008270 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008272 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008273 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8274 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8275 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008276 /* Function was unreferenced while being used, free it
8277 * now. */
8278 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 restoreRedobuff();
8280 restore_search_patterns();
8281 error = ERROR_NONE;
8282 }
8283 }
8284 }
8285 else
8286 {
8287 /*
8288 * Find the function name in the table, call its implementation.
8289 */
8290 i = find_internal_func(fname);
8291 if (i >= 0)
8292 {
8293 if (argcount < functions[i].f_min_argc)
8294 error = ERROR_TOOFEW;
8295 else if (argcount > functions[i].f_max_argc)
8296 error = ERROR_TOOMANY;
8297 else
8298 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 error = ERROR_NONE;
8302 }
8303 }
8304 }
8305 /*
8306 * The function call (or "FuncUndefined" autocommand sequence) might
8307 * have been aborted by an error, an interrupt, or an explicitly thrown
8308 * exception that has not been caught so far. This situation can be
8309 * tested for by calling aborting(). For an error in an internal
8310 * function or for the "E132" error in call_user_func(), however, the
8311 * throw point at which the "force_abort" flag (temporarily reset by
8312 * emsg()) is normally updated has not been reached yet. We need to
8313 * update that flag first to make aborting() reliable.
8314 */
8315 update_force_abort();
8316 }
8317 if (error == ERROR_NONE)
8318 ret = OK;
8319
8320 /*
8321 * Report an error unless the argument evaluation or function call has been
8322 * cancelled due to an aborting error, an interrupt, or an exception.
8323 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008324 if (!aborting())
8325 {
8326 switch (error)
8327 {
8328 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008329 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008330 break;
8331 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008332 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008333 break;
8334 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008335 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008336 name);
8337 break;
8338 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008339 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008340 name);
8341 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008342 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008343 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 name);
8345 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 }
8347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 if (fname != name && fname != fname_buf)
8350 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008351 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 return ret;
8354}
8355
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008356/*
8357 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008358 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008359 */
8360 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008361emsg_funcname(ermsg, name)
8362 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008363 char_u *name;
8364{
8365 char_u *p;
8366
8367 if (*name == K_SPECIAL)
8368 p = concat_str((char_u *)"<SNR>", name + 3);
8369 else
8370 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008371 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008372 if (p != name)
8373 vim_free(p);
8374}
8375
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008376/*
8377 * Return TRUE for a non-zero Number and a non-empty String.
8378 */
8379 static int
8380non_zero_arg(argvars)
8381 typval_T *argvars;
8382{
8383 return ((argvars[0].v_type == VAR_NUMBER
8384 && argvars[0].vval.v_number != 0)
8385 || (argvars[0].v_type == VAR_STRING
8386 && argvars[0].vval.v_string != NULL
8387 && *argvars[0].vval.v_string != NUL));
8388}
8389
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390/*********************************************
8391 * Implementation of the built-in functions
8392 */
8393
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008394#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008395static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8396
8397/*
8398 * Get the float value of "argvars[0]" into "f".
8399 * Returns FAIL when the argument is not a Number or Float.
8400 */
8401 static int
8402get_float_arg(argvars, f)
8403 typval_T *argvars;
8404 float_T *f;
8405{
8406 if (argvars[0].v_type == VAR_FLOAT)
8407 {
8408 *f = argvars[0].vval.v_float;
8409 return OK;
8410 }
8411 if (argvars[0].v_type == VAR_NUMBER)
8412 {
8413 *f = (float_T)argvars[0].vval.v_number;
8414 return OK;
8415 }
8416 EMSG(_("E808: Number or Float required"));
8417 return FAIL;
8418}
8419
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008420/*
8421 * "abs(expr)" function
8422 */
8423 static void
8424f_abs(argvars, rettv)
8425 typval_T *argvars;
8426 typval_T *rettv;
8427{
8428 if (argvars[0].v_type == VAR_FLOAT)
8429 {
8430 rettv->v_type = VAR_FLOAT;
8431 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8432 }
8433 else
8434 {
8435 varnumber_T n;
8436 int error = FALSE;
8437
8438 n = get_tv_number_chk(&argvars[0], &error);
8439 if (error)
8440 rettv->vval.v_number = -1;
8441 else if (n > 0)
8442 rettv->vval.v_number = n;
8443 else
8444 rettv->vval.v_number = -n;
8445 }
8446}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008447
8448/*
8449 * "acos()" function
8450 */
8451 static void
8452f_acos(argvars, rettv)
8453 typval_T *argvars;
8454 typval_T *rettv;
8455{
8456 float_T f;
8457
8458 rettv->v_type = VAR_FLOAT;
8459 if (get_float_arg(argvars, &f) == OK)
8460 rettv->vval.v_float = acos(f);
8461 else
8462 rettv->vval.v_float = 0.0;
8463}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008464#endif
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008467 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 */
8469 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008470f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 typval_T *argvars;
8472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008479 if ((l = argvars[0].vval.v_list) != NULL
8480 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8481 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008483 }
8484 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008485 EMSG(_(e_listreq));
8486}
8487
8488/*
8489 * "append(lnum, string/list)" function
8490 */
8491 static void
8492f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 typval_T *argvars;
8494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008495{
8496 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008497 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 list_T *l = NULL;
8499 listitem_T *li = NULL;
8500 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008501 long added = 0;
8502
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503 lnum = get_tv_lnum(argvars);
8504 if (lnum >= 0
8505 && lnum <= curbuf->b_ml.ml_line_count
8506 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008508 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008510 l = argvars[1].vval.v_list;
8511 if (l == NULL)
8512 return;
8513 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008515 for (;;)
8516 {
8517 if (l == NULL)
8518 tv = &argvars[1]; /* append a string */
8519 else if (li == NULL)
8520 break; /* end of list */
8521 else
8522 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 line = get_tv_string_chk(tv);
8524 if (line == NULL) /* type error */
8525 {
8526 rettv->vval.v_number = 1; /* Failed */
8527 break;
8528 }
8529 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 ++added;
8531 if (l == NULL)
8532 break;
8533 li = li->li_next;
8534 }
8535
8536 appended_lines_mark(lnum, added);
8537 if (curwin->w_cursor.lnum > lnum)
8538 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 else
8541 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544/*
8545 * "argc()" function
8546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008548f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
8556 * "argidx()" function
8557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008560 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565
8566/*
8567 * "argv(nr)" function
8568 */
8569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *argvars;
8572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 int idx;
8575
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008576 if (argvars[0].v_type != VAR_UNKNOWN)
8577 {
8578 idx = get_tv_number_chk(&argvars[0], NULL);
8579 if (idx >= 0 && idx < ARGCOUNT)
8580 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8581 else
8582 rettv->vval.v_string = NULL;
8583 rettv->v_type = VAR_STRING;
8584 }
8585 else if (rettv_list_alloc(rettv) == OK)
8586 for (idx = 0; idx < ARGCOUNT; ++idx)
8587 list_append_string(rettv->vval.v_list,
8588 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008593 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008594 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008595 static void
8596f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008597 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008598 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008599{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008600 float_T f;
8601
8602 rettv->v_type = VAR_FLOAT;
8603 if (get_float_arg(argvars, &f) == OK)
8604 rettv->vval.v_float = asin(f);
8605 else
8606 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607}
8608
8609/*
8610 * "atan()" function
8611 */
8612 static void
8613f_atan(argvars, rettv)
8614 typval_T *argvars;
8615 typval_T *rettv;
8616{
8617 float_T f;
8618
8619 rettv->v_type = VAR_FLOAT;
8620 if (get_float_arg(argvars, &f) == OK)
8621 rettv->vval.v_float = atan(f);
8622 else
8623 rettv->vval.v_float = 0.0;
8624}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625
8626/*
8627 * "atan2()" function
8628 */
8629 static void
8630f_atan2(argvars, rettv)
8631 typval_T *argvars;
8632 typval_T *rettv;
8633{
8634 float_T fx, fy;
8635
8636 rettv->v_type = VAR_FLOAT;
8637 if (get_float_arg(argvars, &fx) == OK
8638 && get_float_arg(&argvars[1], &fy) == OK)
8639 rettv->vval.v_float = atan2(fx, fy);
8640 else
8641 rettv->vval.v_float = 0.0;
8642}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008643#endif
8644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645/*
8646 * "browse(save, title, initdir, default)" function
8647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653#ifdef FEAT_BROWSE
8654 int save;
8655 char_u *title;
8656 char_u *initdir;
8657 char_u *defname;
8658 char_u buf[NUMBUFLEN];
8659 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008660 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008662 save = get_tv_number_chk(&argvars[0], &error);
8663 title = get_tv_string_chk(&argvars[1]);
8664 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8665 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 if (error || title == NULL || initdir == NULL || defname == NULL)
8668 rettv->vval.v_string = NULL;
8669 else
8670 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008671 do_browse(save ? BROWSE_SAVE : 0,
8672 title, defname, NULL, initdir, NULL, curbuf);
8673#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008675#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008677}
8678
8679/*
8680 * "browsedir(title, initdir)" function
8681 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008686{
8687#ifdef FEAT_BROWSE
8688 char_u *title;
8689 char_u *initdir;
8690 char_u buf[NUMBUFLEN];
8691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008692 title = get_tv_string_chk(&argvars[0]);
8693 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 if (title == NULL || initdir == NULL)
8696 rettv->vval.v_string = NULL;
8697 else
8698 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008699 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
Bram Moolenaar33570922005-01-25 22:26:29 +00008706static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708/*
8709 * Find a buffer by number or exact name.
8710 */
8711 static buf_T *
8712find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008713 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008717 if (avar->v_type == VAR_NUMBER)
8718 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008719 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008721 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008722 if (buf == NULL)
8723 {
8724 /* No full path name match, try a match with a URL or a "nofile"
8725 * buffer, these don't use the full path. */
8726 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8727 if (buf->b_fname != NULL
8728 && (path_with_url(buf->b_fname)
8729#ifdef FEAT_QUICKFIX
8730 || bt_nofile(buf)
8731#endif
8732 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008733 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008734 break;
8735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
8737 return buf;
8738}
8739
8740/*
8741 * "bufexists(expr)" function
8742 */
8743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008745 typval_T *argvars;
8746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749}
8750
8751/*
8752 * "buflisted(expr)" function
8753 */
8754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 typval_T *argvars;
8757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 buf_T *buf;
8760
8761 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
8766 * "bufloaded(expr)" function
8767 */
8768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008770 typval_T *argvars;
8771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 buf_T *buf;
8774
8775 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
Bram Moolenaar33570922005-01-25 22:26:29 +00008779static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781/*
8782 * Get buffer by number or pattern.
8783 */
8784 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 int save_magic;
8790 char_u *save_cpo;
8791 buf_T *buf;
8792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 if (tv->v_type == VAR_NUMBER)
8794 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008795 if (tv->v_type != VAR_STRING)
8796 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 if (name == NULL || *name == NUL)
8798 return curbuf;
8799 if (name[0] == '$' && name[1] == NUL)
8800 return lastbuf;
8801
8802 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8803 save_magic = p_magic;
8804 p_magic = TRUE;
8805 save_cpo = p_cpo;
8806 p_cpo = (char_u *)"";
8807
8808 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8809 TRUE, FALSE));
8810
8811 p_magic = save_magic;
8812 p_cpo = save_cpo;
8813
8814 /* If not found, try expanding the name, like done for bufexists(). */
8815 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
8818 return buf;
8819}
8820
8821/*
8822 * "bufname(expr)" function
8823 */
8824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008826 typval_T *argvars;
8827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 buf_T *buf;
8830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 buf = get_buf_tv(&argvars[0]);
8834 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 --emsg_off;
8840}
8841
8842/*
8843 * "bufnr(expr)" function
8844 */
8845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *argvars;
8848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008851 int error = FALSE;
8852 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008857 --emsg_off;
8858
8859 /* If the buffer isn't found and the second argument is not zero create a
8860 * new buffer. */
8861 if (buf == NULL
8862 && argvars[1].v_type != VAR_UNKNOWN
8863 && get_tv_number_chk(&argvars[1], &error) != 0
8864 && !error
8865 && (name = get_tv_string_chk(&argvars[0])) != NULL
8866 && !error)
8867 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873}
8874
8875/*
8876 * "bufwinnr(nr)" function
8877 */
8878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *argvars;
8881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883#ifdef FEAT_WINDOWS
8884 win_T *wp;
8885 int winnr = 0;
8886#endif
8887 buf_T *buf;
8888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#ifdef FEAT_WINDOWS
8893 for (wp = firstwin; wp; wp = wp->w_next)
8894 {
8895 ++winnr;
8896 if (wp->w_buffer == buf)
8897 break;
8898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902#endif
8903 --emsg_off;
8904}
8905
8906/*
8907 * "byte2line(byte)" function
8908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008911 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
8914#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916#else
8917 long boff = 0;
8918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008919 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 (linenr_T)0, &boff);
8925#endif
8926}
8927
8928/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008929 * "byteidx()" function
8930 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *argvars;
8934 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008935{
8936#ifdef FEAT_MBYTE
8937 char_u *t;
8938#endif
8939 char_u *str;
8940 long idx;
8941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 str = get_tv_string_chk(&argvars[0]);
8943 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008946 return;
8947
8948#ifdef FEAT_MBYTE
8949 t = str;
8950 for ( ; idx > 0; idx--)
8951 {
8952 if (*t == NUL) /* EOL reached */
8953 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008954 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008955 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008957#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008958 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008960#endif
8961}
8962
8963/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008964 * "call(func, arglist)" function
8965 */
8966 static void
8967f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008970{
8971 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008972 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008973 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008975 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008977
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008978 if (argvars[1].v_type != VAR_LIST)
8979 {
8980 EMSG(_(e_listreq));
8981 return;
8982 }
8983 if (argvars[1].vval.v_list == NULL)
8984 return;
8985
8986 if (argvars[0].v_type == VAR_FUNC)
8987 func = argvars[0].vval.v_string;
8988 else
8989 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (*func == NUL)
8991 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008992
Bram Moolenaare9a41262005-01-15 22:18:47 +00008993 if (argvars[2].v_type != VAR_UNKNOWN)
8994 {
8995 if (argvars[2].v_type != VAR_DICT)
8996 {
8997 EMSG(_(e_dictreq));
8998 return;
8999 }
9000 selfdict = argvars[2].vval.v_dict;
9001 }
9002
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009003 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9004 item = item->li_next)
9005 {
9006 if (argc == MAX_FUNC_ARGS)
9007 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009008 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 break;
9010 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009011 /* Make a copy of each argument. This is needed to be able to set
9012 * v_lock to VAR_FIXED in the copy without changing the original list.
9013 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014 copy_tv(&item->li_tv, &argv[argc++]);
9015 }
9016
9017 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009018 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9020 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009021
9022 /* Free the arguments. */
9023 while (argc > 0)
9024 clear_tv(&argv[--argc]);
9025}
9026
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009027#ifdef FEAT_FLOAT
9028/*
9029 * "ceil({float})" function
9030 */
9031 static void
9032f_ceil(argvars, rettv)
9033 typval_T *argvars;
9034 typval_T *rettv;
9035{
9036 float_T f;
9037
9038 rettv->v_type = VAR_FLOAT;
9039 if (get_float_arg(argvars, &f) == OK)
9040 rettv->vval.v_float = ceil(f);
9041 else
9042 rettv->vval.v_float = 0.0;
9043}
9044#endif
9045
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009046/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009047 * "changenr()" function
9048 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009049 static void
9050f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009051 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009052 typval_T *rettv;
9053{
9054 rettv->vval.v_number = curbuf->b_u_seq_cur;
9055}
9056
9057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 * "char2nr(string)" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065#ifdef FEAT_MBYTE
9066 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 else
9069#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071}
9072
9073/*
9074 * "cindent(lnum)" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081#ifdef FEAT_CINDENT
9082 pos_T pos;
9083 linenr_T lnum;
9084
9085 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9088 {
9089 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 curwin->w_cursor = pos;
9092 }
9093 else
9094#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096}
9097
9098/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009099 * "clearmatches()" function
9100 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009101 static void
9102f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009103 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009104 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009105{
9106#ifdef FEAT_SEARCH_EXTRA
9107 clear_matches(curwin);
9108#endif
9109}
9110
9111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 * "col(string)" function
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 typval_T *argvars;
9117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 colnr_T col = 0;
9120 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009121 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009123 fp = var2fpos(&argvars[0], FALSE, &fnum);
9124 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
9126 if (fp->col == MAXCOL)
9127 {
9128 /* '> can be MAXCOL, get the length of the line then */
9129 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009130 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 else
9132 col = MAXCOL;
9133 }
9134 else
9135 {
9136 col = fp->col + 1;
9137#ifdef FEAT_VIRTUALEDIT
9138 /* col(".") when the cursor is on the NUL at the end of the line
9139 * because of "coladd" can be seen as an extra column. */
9140 if (virtual_active() && fp == &curwin->w_cursor)
9141 {
9142 char_u *p = ml_get_cursor();
9143
9144 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9145 curwin->w_virtcol - curwin->w_cursor.coladd))
9146 {
9147# ifdef FEAT_MBYTE
9148 int l;
9149
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009150 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 col += l;
9152# else
9153 if (*p != NUL && p[1] == NUL)
9154 ++col;
9155# endif
9156 }
9157 }
9158#endif
9159 }
9160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162}
9163
Bram Moolenaar572cb562005-08-05 21:35:02 +00009164#if defined(FEAT_INS_EXPAND)
9165/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009166 * "complete()" function
9167 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009168 static void
9169f_complete(argvars, rettv)
9170 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009171 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009172{
9173 int startcol;
9174
9175 if ((State & INSERT) == 0)
9176 {
9177 EMSG(_("E785: complete() can only be used in Insert mode"));
9178 return;
9179 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009180
9181 /* Check for undo allowed here, because if something was already inserted
9182 * the line was already saved for undo and this check isn't done. */
9183 if (!undo_allowed())
9184 return;
9185
Bram Moolenaarade00832006-03-10 21:46:58 +00009186 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9187 {
9188 EMSG(_(e_invarg));
9189 return;
9190 }
9191
9192 startcol = get_tv_number_chk(&argvars[0], NULL);
9193 if (startcol <= 0)
9194 return;
9195
9196 set_completion(startcol - 1, argvars[1].vval.v_list);
9197}
9198
9199/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009200 * "complete_add()" function
9201 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009202 static void
9203f_complete_add(argvars, rettv)
9204 typval_T *argvars;
9205 typval_T *rettv;
9206{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009207 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009208}
9209
9210/*
9211 * "complete_check()" function
9212 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009213 static void
9214f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009215 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009216 typval_T *rettv;
9217{
9218 int saved = RedrawingDisabled;
9219
9220 RedrawingDisabled = 0;
9221 ins_compl_check_keys(0);
9222 rettv->vval.v_number = compl_interrupted;
9223 RedrawingDisabled = saved;
9224}
9225#endif
9226
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227/*
9228 * "confirm(message, buttons[, default [, type]])" function
9229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009232 typval_T *argvars UNUSED;
9233 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9236 char_u *message;
9237 char_u *buttons = NULL;
9238 char_u buf[NUMBUFLEN];
9239 char_u buf2[NUMBUFLEN];
9240 int def = 1;
9241 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 char_u *typestr;
9243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 message = get_tv_string_chk(&argvars[0]);
9246 if (message == NULL)
9247 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009248 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9251 if (buttons == NULL)
9252 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009256 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9259 if (typestr == NULL)
9260 error = TRUE;
9261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 switch (TOUPPER_ASC(*typestr))
9264 {
9265 case 'E': type = VIM_ERROR; break;
9266 case 'Q': type = VIM_QUESTION; break;
9267 case 'I': type = VIM_INFO; break;
9268 case 'W': type = VIM_WARNING; break;
9269 case 'G': type = VIM_GENERIC; break;
9270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 }
9272 }
9273 }
9274 }
9275
9276 if (buttons == NULL || *buttons == NUL)
9277 buttons = (char_u *)_("&Ok");
9278
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009279 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009280 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282#endif
9283}
9284
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009285/*
9286 * "copy()" function
9287 */
9288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009290 typval_T *argvars;
9291 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009292{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009293 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009294}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#ifdef FEAT_FLOAT
9297/*
9298 * "cos()" function
9299 */
9300 static void
9301f_cos(argvars, rettv)
9302 typval_T *argvars;
9303 typval_T *rettv;
9304{
9305 float_T f;
9306
9307 rettv->v_type = VAR_FLOAT;
9308 if (get_float_arg(argvars, &f) == OK)
9309 rettv->vval.v_float = cos(f);
9310 else
9311 rettv->vval.v_float = 0.0;
9312}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009313
9314/*
9315 * "cosh()" function
9316 */
9317 static void
9318f_cosh(argvars, rettv)
9319 typval_T *argvars;
9320 typval_T *rettv;
9321{
9322 float_T f;
9323
9324 rettv->v_type = VAR_FLOAT;
9325 if (get_float_arg(argvars, &f) == OK)
9326 rettv->vval.v_float = cosh(f);
9327 else
9328 rettv->vval.v_float = 0.0;
9329}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009330#endif
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009333 * "count()" function
9334 */
9335 static void
9336f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 typval_T *argvars;
9338 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009339{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009340 long n = 0;
9341 int ic = FALSE;
9342
Bram Moolenaare9a41262005-01-15 22:18:47 +00009343 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009344 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009345 listitem_T *li;
9346 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009348
Bram Moolenaare9a41262005-01-15 22:18:47 +00009349 if ((l = argvars[0].vval.v_list) != NULL)
9350 {
9351 li = l->lv_first;
9352 if (argvars[2].v_type != VAR_UNKNOWN)
9353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 int error = FALSE;
9355
9356 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009357 if (argvars[3].v_type != VAR_UNKNOWN)
9358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 idx = get_tv_number_chk(&argvars[3], &error);
9360 if (!error)
9361 {
9362 li = list_find(l, idx);
9363 if (li == NULL)
9364 EMSGN(_(e_listidx), idx);
9365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 if (error)
9368 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009369 }
9370
9371 for ( ; li != NULL; li = li->li_next)
9372 if (tv_equal(&li->li_tv, &argvars[1], ic))
9373 ++n;
9374 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009376 else if (argvars[0].v_type == VAR_DICT)
9377 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 int todo;
9379 dict_T *d;
9380 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009382 if ((d = argvars[0].vval.v_dict) != NULL)
9383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 int error = FALSE;
9385
Bram Moolenaare9a41262005-01-15 22:18:47 +00009386 if (argvars[2].v_type != VAR_UNKNOWN)
9387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389 if (argvars[3].v_type != VAR_UNKNOWN)
9390 EMSG(_(e_invarg));
9391 }
9392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009393 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009395 {
9396 if (!HASHITEM_EMPTY(hi))
9397 {
9398 --todo;
9399 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9400 ++n;
9401 }
9402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 }
9404 }
9405 else
9406 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009407 rettv->vval.v_number = n;
9408}
9409
9410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9412 *
9413 * Checks the existence of a cscope connection.
9414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009417 typval_T *argvars UNUSED;
9418 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_CSCOPE
9421 int num = 0;
9422 char_u *dbpath = NULL;
9423 char_u *prepend = NULL;
9424 char_u buf[NUMBUFLEN];
9425
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009426 if (argvars[0].v_type != VAR_UNKNOWN
9427 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 num = (int)get_tv_number(&argvars[0]);
9430 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009431 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 }
9434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436#endif
9437}
9438
9439/*
9440 * "cursor(lnum, col)" function
9441 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009442 * Moves the cursor to the specified line and column.
9443 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 typval_T *argvars;
9448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009451#ifdef FEAT_VIRTUALEDIT
9452 long coladd = 0;
9453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009455 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009456 if (argvars[1].v_type == VAR_UNKNOWN)
9457 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009458 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009459
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009460 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009461 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009462 line = pos.lnum;
9463 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009464#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009465 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009466#endif
9467 }
9468 else
9469 {
9470 line = get_tv_lnum(argvars);
9471 col = get_tv_number_chk(&argvars[1], NULL);
9472#ifdef FEAT_VIRTUALEDIT
9473 if (argvars[2].v_type != VAR_UNKNOWN)
9474 coladd = get_tv_number_chk(&argvars[2], NULL);
9475#endif
9476 }
9477 if (line < 0 || col < 0
9478#ifdef FEAT_VIRTUALEDIT
9479 || coladd < 0
9480#endif
9481 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (line > 0)
9484 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (col > 0)
9486 curwin->w_cursor.col = col - 1;
9487#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009488 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#endif
9490
9491 /* Make sure the cursor is in a valid position. */
9492 check_cursor();
9493#ifdef FEAT_MBYTE
9494 /* Correct cursor for multi-byte character. */
9495 if (has_mbyte)
9496 mb_adjust_cursor();
9497#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009498
9499 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009500 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
9503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009504 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 */
9506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009508 typval_T *argvars;
9509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009511 int noref = 0;
9512
9513 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009515 if (noref < 0 || noref > 1)
9516 EMSG(_(e_invarg));
9517 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009518 {
9519 current_copyID += COPYID_INC;
9520 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522}
9523
9524/*
9525 * "delete()" function
9526 */
9527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538/*
9539 * "did_filetype()" function
9540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009543 typval_T *argvars UNUSED;
9544 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548#endif
9549}
9550
9551/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009552 * "diff_filler()" function
9553 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009556 typval_T *argvars UNUSED;
9557 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009558{
9559#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009561#endif
9562}
9563
9564/*
9565 * "diff_hlID()" function
9566 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009569 typval_T *argvars UNUSED;
9570 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009571{
9572#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009574 static linenr_T prev_lnum = 0;
9575 static int changedtick = 0;
9576 static int fnum = 0;
9577 static int change_start = 0;
9578 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009579 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009580 int filler_lines;
9581 int col;
9582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 if (lnum < 0) /* ignore type error in {lnum} arg */
9584 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009585 if (lnum != prev_lnum
9586 || changedtick != curbuf->b_changedtick
9587 || fnum != curbuf->b_fnum)
9588 {
9589 /* New line, buffer, change: need to get the values. */
9590 filler_lines = diff_check(curwin, lnum);
9591 if (filler_lines < 0)
9592 {
9593 if (filler_lines == -1)
9594 {
9595 change_start = MAXCOL;
9596 change_end = -1;
9597 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9598 hlID = HLF_ADD; /* added line */
9599 else
9600 hlID = HLF_CHD; /* changed line */
9601 }
9602 else
9603 hlID = HLF_ADD; /* added line */
9604 }
9605 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009606 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009607 prev_lnum = lnum;
9608 changedtick = curbuf->b_changedtick;
9609 fnum = curbuf->b_fnum;
9610 }
9611
9612 if (hlID == HLF_CHD || hlID == HLF_TXD)
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009615 if (col >= change_start && col <= change_end)
9616 hlID = HLF_TXD; /* changed text */
9617 else
9618 hlID = HLF_CHD; /* changed line */
9619 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009620 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009621#endif
9622}
9623
9624/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009625 * "empty({expr})" function
9626 */
9627 static void
9628f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009631{
9632 int n;
9633
9634 switch (argvars[0].v_type)
9635 {
9636 case VAR_STRING:
9637 case VAR_FUNC:
9638 n = argvars[0].vval.v_string == NULL
9639 || *argvars[0].vval.v_string == NUL;
9640 break;
9641 case VAR_NUMBER:
9642 n = argvars[0].vval.v_number == 0;
9643 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009644#ifdef FEAT_FLOAT
9645 case VAR_FLOAT:
9646 n = argvars[0].vval.v_float == 0.0;
9647 break;
9648#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009649 case VAR_LIST:
9650 n = argvars[0].vval.v_list == NULL
9651 || argvars[0].vval.v_list->lv_first == NULL;
9652 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653 case VAR_DICT:
9654 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009655 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009657 default:
9658 EMSG2(_(e_intern2), "f_empty()");
9659 n = 0;
9660 }
9661
9662 rettv->vval.v_number = n;
9663}
9664
9665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 * "escape({string}, {chars})" function
9667 */
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673 char_u buf[NUMBUFLEN];
9674
Bram Moolenaar758711c2005-02-02 23:11:38 +00009675 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9676 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678}
9679
9680/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009681 * "eval()" function
9682 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009683 static void
9684f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 typval_T *argvars;
9686 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009687{
9688 char_u *s;
9689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 s = get_tv_string_chk(&argvars[0]);
9691 if (s != NULL)
9692 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9695 {
9696 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009697 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009698 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009699 else if (*s != NUL)
9700 EMSG(_(e_trailing));
9701}
9702
9703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 * "eventhandler()" function
9705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009708 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
9715 * "executable()" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
9726 * "exists()" function
9727 */
9728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 typval_T *argvars;
9731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 char_u *p;
9734 char_u *name;
9735 int n = FALSE;
9736 int len = 0;
9737
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009738 no_autoload = TRUE;
9739
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (*p == '$') /* environment variable */
9742 {
9743 /* first try "normal" environment variables (fast) */
9744 if (mch_getenv(p + 1) != NULL)
9745 n = TRUE;
9746 else
9747 {
9748 /* try expanding things like $VIM and ${HOME} */
9749 p = expand_env_save(p);
9750 if (p != NULL && *p != '$')
9751 n = TRUE;
9752 vim_free(p);
9753 }
9754 }
9755 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009758 if (*skipwhite(p) != NUL)
9759 n = FALSE; /* trailing garbage */
9760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else if (*p == '*') /* internal or user defined function */
9762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009763 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765 else if (*p == ':')
9766 {
9767 n = cmd_exists(p + 1);
9768 }
9769 else if (*p == '#')
9770 {
9771#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009772 if (p[1] == '#')
9773 n = autocmd_supported(p + 2);
9774 else
9775 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776#endif
9777 }
9778 else /* internal variable */
9779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009780 char_u *tofree;
9781 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009783 /* get_name_len() takes care of expanding curly braces */
9784 name = p;
9785 len = get_name_len(&p, &tofree, TRUE, FALSE);
9786 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009788 if (tofree != NULL)
9789 name = tofree;
9790 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9791 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009793 /* handle d.key, l[idx], f(expr) */
9794 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9795 if (n)
9796 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 }
9798 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009799 if (*p != NUL)
9800 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
9804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009806
9807 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808}
9809
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009810#ifdef FEAT_FLOAT
9811/*
9812 * "exp()" function
9813 */
9814 static void
9815f_exp(argvars, rettv)
9816 typval_T *argvars;
9817 typval_T *rettv;
9818{
9819 float_T f;
9820
9821 rettv->v_type = VAR_FLOAT;
9822 if (get_float_arg(argvars, &f) == OK)
9823 rettv->vval.v_float = exp(f);
9824 else
9825 rettv->vval.v_float = 0.0;
9826}
9827#endif
9828
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829/*
9830 * "expand()" function
9831 */
9832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 typval_T *argvars;
9835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 char_u *s;
9838 int len;
9839 char_u *errormsg;
9840 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9841 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->v_type = VAR_STRING;
9845 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (*s == '%' || *s == '#' || *s == '<')
9847 {
9848 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009849 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 --emsg_off;
9851 }
9852 else
9853 {
9854 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009855 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 if (argvars[1].v_type != VAR_UNKNOWN
9857 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 if (!error)
9860 {
9861 ExpandInit(&xpc);
9862 xpc.xp_context = EXPAND_FILES;
9863 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 }
9865 else
9866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 }
9868}
9869
9870/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009871 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009872 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009873 */
9874 static void
9875f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 typval_T *argvars;
9877 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009879 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009881 list_T *l1, *l2;
9882 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009883 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009885
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886 l1 = argvars[0].vval.v_list;
9887 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009888 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9889 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890 {
9891 if (argvars[2].v_type != VAR_UNKNOWN)
9892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 before = get_tv_number_chk(&argvars[2], &error);
9894 if (error)
9895 return; /* type error; errmsg already given */
9896
Bram Moolenaar758711c2005-02-02 23:11:38 +00009897 if (before == l1->lv_len)
9898 item = NULL;
9899 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009900 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009901 item = list_find(l1, before);
9902 if (item == NULL)
9903 {
9904 EMSGN(_(e_listidx), before);
9905 return;
9906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 }
9908 }
9909 else
9910 item = NULL;
9911 list_extend(l1, l2, item);
9912
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 copy_tv(&argvars[0], rettv);
9914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9917 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 dict_T *d1, *d2;
9919 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 char_u *action;
9921 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009923 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924
9925 d1 = argvars[0].vval.v_dict;
9926 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009927 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9928 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 {
9930 /* Check the third argument. */
9931 if (argvars[2].v_type != VAR_UNKNOWN)
9932 {
9933 static char *(av[]) = {"keep", "force", "error"};
9934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 action = get_tv_string_chk(&argvars[2]);
9936 if (action == NULL)
9937 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 for (i = 0; i < 3; ++i)
9939 if (STRCMP(action, av[i]) == 0)
9940 break;
9941 if (i == 3)
9942 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009943 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944 return;
9945 }
9946 }
9947 else
9948 action = (char_u *)"force";
9949
9950 /* Go over all entries in the second dict and add them to the
9951 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009952 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009953 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009955 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009957 --todo;
9958 di1 = dict_find(d1, hi2->hi_key, -1);
9959 if (di1 == NULL)
9960 {
9961 di1 = dictitem_copy(HI2DI(hi2));
9962 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9963 dictitem_free(di1);
9964 }
9965 else if (*action == 'e')
9966 {
9967 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9968 break;
9969 }
9970 else if (*action == 'f')
9971 {
9972 clear_tv(&di1->di_tv);
9973 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009975 }
9976 }
9977
Bram Moolenaare9a41262005-01-15 22:18:47 +00009978 copy_tv(&argvars[0], rettv);
9979 }
9980 }
9981 else
9982 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009983}
9984
9985/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009986 * "feedkeys()" function
9987 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009988 static void
9989f_feedkeys(argvars, rettv)
9990 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009991 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009992{
9993 int remap = TRUE;
9994 char_u *keys, *flags;
9995 char_u nbuf[NUMBUFLEN];
9996 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009997 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009998
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009999 /* This is not allowed in the sandbox. If the commands would still be
10000 * executed in the sandbox it would be OK, but it probably happens later,
10001 * when "sandbox" is no longer set. */
10002 if (check_secure())
10003 return;
10004
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010005 keys = get_tv_string(&argvars[0]);
10006 if (*keys != NUL)
10007 {
10008 if (argvars[1].v_type != VAR_UNKNOWN)
10009 {
10010 flags = get_tv_string_buf(&argvars[1], nbuf);
10011 for ( ; *flags != NUL; ++flags)
10012 {
10013 switch (*flags)
10014 {
10015 case 'n': remap = FALSE; break;
10016 case 'm': remap = TRUE; break;
10017 case 't': typed = TRUE; break;
10018 }
10019 }
10020 }
10021
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010022 /* Need to escape K_SPECIAL and CSI before putting the string in the
10023 * typeahead buffer. */
10024 keys_esc = vim_strsave_escape_csi(keys);
10025 if (keys_esc != NULL)
10026 {
10027 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010028 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010029 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010030 if (vgetc_busy)
10031 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010032 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010033 }
10034}
10035
10036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 * "filereadable()" function
10038 */
10039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010041 typval_T *argvars;
10042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010044 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 char_u *p;
10046 int n;
10047
Bram Moolenaarc236c162008-07-13 17:41:49 +000010048#ifndef O_NONBLOCK
10049# define O_NONBLOCK 0
10050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010052 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10053 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
10055 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010056 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058 else
10059 n = FALSE;
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062}
10063
10064/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010065 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 * rights to write into.
10067 */
10068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010070 typval_T *argvars;
10071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010073 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074}
10075
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010076static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010077
10078 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010079findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010082 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010083{
10084#ifdef FEAT_SEARCHPATH
10085 char_u *fname;
10086 char_u *fresult = NULL;
10087 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10088 char_u *p;
10089 char_u pathbuf[NUMBUFLEN];
10090 int count = 1;
10091 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010092 int error = FALSE;
10093#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010094
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010095 rettv->vval.v_string = NULL;
10096 rettv->v_type = VAR_STRING;
10097
10098#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010100
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010101 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10104 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010105 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 else
10107 {
10108 if (*p != NUL)
10109 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010112 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010114 }
10115
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010116 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10117 error = TRUE;
10118
10119 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010121 do
10122 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010123 if (rettv->v_type == VAR_STRING)
10124 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 fresult = find_file_in_path_option(first ? fname : NULL,
10126 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010127 0, first, path,
10128 find_what,
10129 curbuf->b_ffname,
10130 find_what == FINDFILE_DIR
10131 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010133
10134 if (fresult != NULL && rettv->v_type == VAR_LIST)
10135 list_append_string(rettv->vval.v_list, fresult, -1);
10136
10137 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010139
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010140 if (rettv->v_type == VAR_STRING)
10141 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010142#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010143}
10144
Bram Moolenaar33570922005-01-25 22:26:29 +000010145static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10146static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010147
10148/*
10149 * Implementation of map() and filter().
10150 */
10151 static void
10152filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010155 int map;
10156{
10157 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010158 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010159 listitem_T *li, *nli;
10160 list_T *l = NULL;
10161 dictitem_T *di;
10162 hashtab_T *ht;
10163 hashitem_T *hi;
10164 dict_T *d = NULL;
10165 typval_T save_val;
10166 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010168 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010169 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010170 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010171 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010172
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010174 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010175 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010176 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 return;
10178 }
10179 else if (argvars[0].v_type == VAR_DICT)
10180 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010181 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010182 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 return;
10184 }
10185 else
10186 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010187 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 return;
10189 }
10190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 expr = get_tv_string_buf_chk(&argvars[1], buf);
10192 /* On type errors, the preceding call has already displayed an error
10193 * message. Avoid a misleading error message for an empty string that
10194 * was not passed as argument. */
10195 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 prepare_vimvar(VV_VAL, &save_val);
10198 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010199
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010200 /* We reset "did_emsg" to be able to detect whether an error
10201 * occurred during evaluation of the expression. */
10202 save_did_emsg = did_emsg;
10203 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010204
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010205 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 vimvars[VV_KEY].vv_type = VAR_STRING;
10209
10210 ht = &d->dv_hashtab;
10211 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010212 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 if (!HASHITEM_EMPTY(hi))
10216 {
10217 --todo;
10218 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010219 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 break;
10221 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010222 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010223 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 break;
10225 if (!map && rem)
10226 dictitem_remove(d, di);
10227 clear_tv(&vimvars[VV_KEY].vv_tv);
10228 }
10229 }
10230 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 }
10232 else
10233 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010234 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 for (li = l->lv_first; li != NULL; li = nli)
10237 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010238 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010239 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010241 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010242 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010243 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 break;
10245 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010246 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010247 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010248 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010250
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010251 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010253
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010254 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010255 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010256
10257 copy_tv(&argvars[0], rettv);
10258}
10259
10260 static int
10261filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 char_u *expr;
10264 int map;
10265 int *remp;
10266{
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010269 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270
Bram Moolenaar33570922005-01-25 22:26:29 +000010271 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 s = expr;
10273 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010274 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 if (*s != NUL) /* check for trailing chars after expr */
10276 {
10277 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010278 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 }
10280 if (map)
10281 {
10282 /* map(): replace the list item value */
10283 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010284 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 *tv = rettv;
10286 }
10287 else
10288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 int error = FALSE;
10290
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 /* On type error, nothing has been removed; return FAIL to stop the
10295 * loop. The error message was given by get_tv_number_chk(). */
10296 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010297 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010299 retval = OK;
10300theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010302 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010303}
10304
10305/*
10306 * "filter()" function
10307 */
10308 static void
10309f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 typval_T *argvars;
10311 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010312{
10313 filter_map(argvars, rettv, FALSE);
10314}
10315
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317 * "finddir({fname}[, {path}[, {count}]])" function
10318 */
10319 static void
10320f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010324 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325}
10326
10327/*
10328 * "findfile({fname}[, {path}[, {count}]])" function
10329 */
10330 static void
10331f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010334{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010335 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336}
10337
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010338#ifdef FEAT_FLOAT
10339/*
10340 * "float2nr({float})" function
10341 */
10342 static void
10343f_float2nr(argvars, rettv)
10344 typval_T *argvars;
10345 typval_T *rettv;
10346{
10347 float_T f;
10348
10349 if (get_float_arg(argvars, &f) == OK)
10350 {
10351 if (f < -0x7fffffff)
10352 rettv->vval.v_number = -0x7fffffff;
10353 else if (f > 0x7fffffff)
10354 rettv->vval.v_number = 0x7fffffff;
10355 else
10356 rettv->vval.v_number = (varnumber_T)f;
10357 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010358}
10359
10360/*
10361 * "floor({float})" function
10362 */
10363 static void
10364f_floor(argvars, rettv)
10365 typval_T *argvars;
10366 typval_T *rettv;
10367{
10368 float_T f;
10369
10370 rettv->v_type = VAR_FLOAT;
10371 if (get_float_arg(argvars, &f) == OK)
10372 rettv->vval.v_float = floor(f);
10373 else
10374 rettv->vval.v_float = 0.0;
10375}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010376
10377/*
10378 * "fmod()" function
10379 */
10380 static void
10381f_fmod(argvars, rettv)
10382 typval_T *argvars;
10383 typval_T *rettv;
10384{
10385 float_T fx, fy;
10386
10387 rettv->v_type = VAR_FLOAT;
10388 if (get_float_arg(argvars, &fx) == OK
10389 && get_float_arg(&argvars[1], &fy) == OK)
10390 rettv->vval.v_float = fmod(fx, fy);
10391 else
10392 rettv->vval.v_float = 0.0;
10393}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010394#endif
10395
Bram Moolenaar0d660222005-01-07 21:51:51 +000010396/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010397 * "fnameescape({string})" function
10398 */
10399 static void
10400f_fnameescape(argvars, rettv)
10401 typval_T *argvars;
10402 typval_T *rettv;
10403{
10404 rettv->vval.v_string = vim_strsave_fnameescape(
10405 get_tv_string(&argvars[0]), FALSE);
10406 rettv->v_type = VAR_STRING;
10407}
10408
10409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 * "fnamemodify({fname}, {mods})" function
10411 */
10412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 typval_T *argvars;
10415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 char_u *fname;
10418 char_u *mods;
10419 int usedlen = 0;
10420 int len;
10421 char_u *fbuf = NULL;
10422 char_u buf[NUMBUFLEN];
10423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 fname = get_tv_string_chk(&argvars[0]);
10425 mods = get_tv_string_buf_chk(&argvars[1], buf);
10426 if (fname == NULL || mods == NULL)
10427 fname = NULL;
10428 else
10429 {
10430 len = (int)STRLEN(fname);
10431 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 vim_free(fbuf);
10440}
10441
Bram Moolenaar33570922005-01-25 22:26:29 +000010442static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
10444/*
10445 * "foldclosed()" function
10446 */
10447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010449 typval_T *argvars;
10450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 int end;
10452{
10453#ifdef FEAT_FOLDING
10454 linenr_T lnum;
10455 linenr_T first, last;
10456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10459 {
10460 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10461 {
10462 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 return;
10467 }
10468 }
10469#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
10472
10473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010474 * "foldclosed()" function
10475 */
10476 static void
10477f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *argvars;
10479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010480{
10481 foldclosed_both(argvars, rettv, FALSE);
10482}
10483
10484/*
10485 * "foldclosedend()" function
10486 */
10487 static void
10488f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010491{
10492 foldclosed_both(argvars, rettv, TRUE);
10493}
10494
10495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "foldlevel()" function
10497 */
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503#ifdef FEAT_FOLDING
10504 linenr_T lnum;
10505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
10513 * "foldtext()" function
10514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010517 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520#ifdef FEAT_FOLDING
10521 linenr_T lnum;
10522 char_u *s;
10523 char_u *r;
10524 int len;
10525 char *txt;
10526#endif
10527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->v_type = VAR_STRING;
10529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10532 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10533 <= curbuf->b_ml.ml_line_count
10534 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 {
10536 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10538 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 {
10540 if (!linewhite(lnum))
10541 break;
10542 ++lnum;
10543 }
10544
10545 /* Find interesting text in this line. */
10546 s = skipwhite(ml_get(lnum));
10547 /* skip C comment-start */
10548 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010551 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010552 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010553 {
10554 s = skipwhite(ml_get(lnum + 1));
10555 if (*s == '*')
10556 s = skipwhite(s + 1);
10557 }
10558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 txt = _("+-%s%3ld lines: ");
10560 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 + 20 /* for %3ld */
10563 + STRLEN(s))); /* concatenated */
10564 if (r != NULL)
10565 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010566 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10567 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10568 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 len = (int)STRLEN(r);
10570 STRCAT(r, s);
10571 /* remove 'foldmarker' and 'commentstring' */
10572 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 }
10575 }
10576#endif
10577}
10578
10579/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010580 * "foldtextresult(lnum)" function
10581 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010584 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010586{
10587#ifdef FEAT_FOLDING
10588 linenr_T lnum;
10589 char_u *text;
10590 char_u buf[51];
10591 foldinfo_T foldinfo;
10592 int fold_count;
10593#endif
10594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595 rettv->v_type = VAR_STRING;
10596 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010597#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 /* treat illegal types and illegal string values for {lnum} the same */
10600 if (lnum < 0)
10601 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602 fold_count = foldedCount(curwin, lnum, &foldinfo);
10603 if (fold_count > 0)
10604 {
10605 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10606 &foldinfo, buf);
10607 if (text == buf)
10608 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010610 }
10611#endif
10612}
10613
10614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 * "foreground()" function
10616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010619 typval_T *argvars UNUSED;
10620 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622#ifdef FEAT_GUI
10623 if (gui.in_use)
10624 gui_mch_set_foreground();
10625#else
10626# ifdef WIN32
10627 win32_set_foreground();
10628# endif
10629#endif
10630}
10631
10632/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010633 * "function()" function
10634 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 typval_T *argvars;
10638 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010639{
10640 char_u *s;
10641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010644 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010645 /* Don't check an autoload name for existence here. */
10646 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010647 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010648 else
10649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 rettv->vval.v_string = vim_strsave(s);
10651 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010652 }
10653}
10654
10655/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010656 * "garbagecollect()" function
10657 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010658 static void
10659f_garbagecollect(argvars, rettv)
10660 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010661 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010662{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010663 /* This is postponed until we are back at the toplevel, because we may be
10664 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10665 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010666
10667 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10668 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010669}
10670
10671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010672 * "get()" function
10673 */
10674 static void
10675f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010678{
Bram Moolenaar33570922005-01-25 22:26:29 +000010679 listitem_T *li;
10680 list_T *l;
10681 dictitem_T *di;
10682 dict_T *d;
10683 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010684
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010687 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 int error = FALSE;
10690
10691 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10692 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010694 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 else if (argvars[0].v_type == VAR_DICT)
10697 {
10698 if ((d = argvars[0].vval.v_dict) != NULL)
10699 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010700 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010701 if (di != NULL)
10702 tv = &di->di_tv;
10703 }
10704 }
10705 else
10706 EMSG2(_(e_listdictarg), "get()");
10707
10708 if (tv == NULL)
10709 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010710 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010711 copy_tv(&argvars[2], rettv);
10712 }
10713 else
10714 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715}
10716
Bram Moolenaar342337a2005-07-21 21:11:17 +000010717static 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 +000010718
10719/*
10720 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010721 * Return a range (from start to end) of lines in rettv from the specified
10722 * buffer.
10723 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010724 */
10725 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010726get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010727 buf_T *buf;
10728 linenr_T start;
10729 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010730 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010731 typval_T *rettv;
10732{
10733 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010734
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010735 if (retlist && rettv_list_alloc(rettv) == FAIL)
10736 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010737
10738 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10739 return;
10740
10741 if (!retlist)
10742 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010743 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10744 p = ml_get_buf(buf, start, FALSE);
10745 else
10746 p = (char_u *)"";
10747
10748 rettv->v_type = VAR_STRING;
10749 rettv->vval.v_string = vim_strsave(p);
10750 }
10751 else
10752 {
10753 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010754 return;
10755
10756 if (start < 1)
10757 start = 1;
10758 if (end > buf->b_ml.ml_line_count)
10759 end = buf->b_ml.ml_line_count;
10760 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010761 if (list_append_string(rettv->vval.v_list,
10762 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010763 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010764 }
10765}
10766
10767/*
10768 * "getbufline()" function
10769 */
10770 static void
10771f_getbufline(argvars, rettv)
10772 typval_T *argvars;
10773 typval_T *rettv;
10774{
10775 linenr_T lnum;
10776 linenr_T end;
10777 buf_T *buf;
10778
10779 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10780 ++emsg_off;
10781 buf = get_buf_tv(&argvars[0]);
10782 --emsg_off;
10783
Bram Moolenaar661b1822005-07-28 22:36:45 +000010784 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010785 if (argvars[2].v_type == VAR_UNKNOWN)
10786 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010787 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010788 end = get_tv_lnum_buf(&argvars[2], buf);
10789
Bram Moolenaar342337a2005-07-21 21:11:17 +000010790 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010791}
10792
Bram Moolenaar0d660222005-01-07 21:51:51 +000010793/*
10794 * "getbufvar()" function
10795 */
10796 static void
10797f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010798 typval_T *argvars;
10799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800{
10801 buf_T *buf;
10802 buf_T *save_curbuf;
10803 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010804 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10807 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808 ++emsg_off;
10809 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010810
10811 rettv->v_type = VAR_STRING;
10812 rettv->vval.v_string = NULL;
10813
10814 if (buf != NULL && varname != NULL)
10815 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010816 /* set curbuf to be our buf, temporarily */
10817 save_curbuf = curbuf;
10818 curbuf = buf;
10819
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822 else
10823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 if (*varname == NUL)
10825 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10826 * scope prefix before the NUL byte is required by
10827 * find_var_in_ht(). */
10828 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010830 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010833 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010834
10835 /* restore previous notion of curbuf */
10836 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010837 }
10838
10839 --emsg_off;
10840}
10841
10842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 * "getchar()" function
10844 */
10845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 typval_T *argvars;
10848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849{
10850 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010853 /* Position the cursor. Needed after a message that ends in a space. */
10854 windgoto(msg_row, msg_col);
10855
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 ++no_mapping;
10857 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010858 for (;;)
10859 {
10860 if (argvars[0].v_type == VAR_UNKNOWN)
10861 /* getchar(): blocking wait. */
10862 n = safe_vgetc();
10863 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10864 /* getchar(1): only check if char avail */
10865 n = vpeekc();
10866 else if (error || vpeekc() == NUL)
10867 /* illegal argument or getchar(0) and no char avail: return zero */
10868 n = 0;
10869 else
10870 /* getchar(0) and char avail: return char */
10871 n = safe_vgetc();
10872 if (n == K_IGNORE)
10873 continue;
10874 break;
10875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 --no_mapping;
10877 --allow_keys;
10878
Bram Moolenaar219b8702006-11-01 14:32:36 +000010879 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10880 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10881 vimvars[VV_MOUSE_COL].vv_nr = 0;
10882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 if (IS_SPECIAL(n) || mod_mask != 0)
10885 {
10886 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10887 int i = 0;
10888
10889 /* Turn a special key into three bytes, plus modifier. */
10890 if (mod_mask != 0)
10891 {
10892 temp[i++] = K_SPECIAL;
10893 temp[i++] = KS_MODIFIER;
10894 temp[i++] = mod_mask;
10895 }
10896 if (IS_SPECIAL(n))
10897 {
10898 temp[i++] = K_SPECIAL;
10899 temp[i++] = K_SECOND(n);
10900 temp[i++] = K_THIRD(n);
10901 }
10902#ifdef FEAT_MBYTE
10903 else if (has_mbyte)
10904 i += (*mb_char2bytes)(n, temp + i);
10905#endif
10906 else
10907 temp[i++] = n;
10908 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
10910 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010911
10912#ifdef FEAT_MOUSE
10913 if (n == K_LEFTMOUSE
10914 || n == K_LEFTMOUSE_NM
10915 || n == K_LEFTDRAG
10916 || n == K_LEFTRELEASE
10917 || n == K_LEFTRELEASE_NM
10918 || n == K_MIDDLEMOUSE
10919 || n == K_MIDDLEDRAG
10920 || n == K_MIDDLERELEASE
10921 || n == K_RIGHTMOUSE
10922 || n == K_RIGHTDRAG
10923 || n == K_RIGHTRELEASE
10924 || n == K_X1MOUSE
10925 || n == K_X1DRAG
10926 || n == K_X1RELEASE
10927 || n == K_X2MOUSE
10928 || n == K_X2DRAG
10929 || n == K_X2RELEASE
10930 || n == K_MOUSEDOWN
10931 || n == K_MOUSEUP)
10932 {
10933 int row = mouse_row;
10934 int col = mouse_col;
10935 win_T *win;
10936 linenr_T lnum;
10937# ifdef FEAT_WINDOWS
10938 win_T *wp;
10939# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010940 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010941
10942 if (row >= 0 && col >= 0)
10943 {
10944 /* Find the window at the mouse coordinates and compute the
10945 * text position. */
10946 win = mouse_find_win(&row, &col);
10947 (void)mouse_comp_pos(win, &row, &col, &lnum);
10948# ifdef FEAT_WINDOWS
10949 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010950 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010951# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010952 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010953 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10954 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10955 }
10956 }
10957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 }
10959}
10960
10961/*
10962 * "getcharmod()" function
10963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970}
10971
10972/*
10973 * "getcmdline()" function
10974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010977 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980 rettv->v_type = VAR_STRING;
10981 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982}
10983
10984/*
10985 * "getcmdpos()" function
10986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010989 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993}
10994
10995/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010996 * "getcmdtype()" function
10997 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010998 static void
10999f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011000 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011001 typval_T *rettv;
11002{
11003 rettv->v_type = VAR_STRING;
11004 rettv->vval.v_string = alloc(2);
11005 if (rettv->vval.v_string != NULL)
11006 {
11007 rettv->vval.v_string[0] = get_cmdline_type();
11008 rettv->vval.v_string[1] = NUL;
11009 }
11010}
11011
11012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 * "getcwd()" function
11014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011017 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019{
11020 char_u cwd[MAXPATHL];
11021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 else
11026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011029 if (rettv->vval.v_string != NULL)
11030 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031#endif
11032 }
11033}
11034
11035/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011036 * "getfontname()" function
11037 */
11038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011040 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011041 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 rettv->v_type = VAR_STRING;
11044 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011045#ifdef FEAT_GUI
11046 if (gui.in_use)
11047 {
11048 GuiFont font;
11049 char_u *name = NULL;
11050
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011051 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011052 {
11053 /* Get the "Normal" font. Either the name saved by
11054 * hl_set_font_name() or from the font ID. */
11055 font = gui.norm_font;
11056 name = hl_get_font_name();
11057 }
11058 else
11059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011061 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11062 return;
11063 font = gui_mch_get_font(name, FALSE);
11064 if (font == NOFONT)
11065 return; /* Invalid font name, return empty string. */
11066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011068 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011069 gui_mch_free_font(font);
11070 }
11071#endif
11072}
11073
11074/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011075 * "getfperm({fname})" function
11076 */
11077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 typval_T *argvars;
11080 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011081{
11082 char_u *fname;
11083 struct stat st;
11084 char_u *perm = NULL;
11085 char_u flags[] = "rwx";
11086 int i;
11087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011091 if (mch_stat((char *)fname, &st) >= 0)
11092 {
11093 perm = vim_strsave((char_u *)"---------");
11094 if (perm != NULL)
11095 {
11096 for (i = 0; i < 9; i++)
11097 {
11098 if (st.st_mode & (1 << (8 - i)))
11099 perm[i] = flags[i % 3];
11100 }
11101 }
11102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011104}
11105
11106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 * "getfsize({fname})" function
11108 */
11109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *argvars;
11112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113{
11114 char_u *fname;
11115 struct stat st;
11116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120
11121 if (mch_stat((char *)fname, &st) >= 0)
11122 {
11123 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011124 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011128
11129 /* non-perfect check for overflow */
11130 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11131 rettv->vval.v_number = -2;
11132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 }
11134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
11139 * "getftime({fname})" function
11140 */
11141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011143 typval_T *argvars;
11144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145{
11146 char_u *fname;
11147 struct stat st;
11148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150
11151 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155}
11156
11157/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011158 * "getftype({fname})" function
11159 */
11160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 typval_T *argvars;
11163 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011164{
11165 char_u *fname;
11166 struct stat st;
11167 char_u *type = NULL;
11168 char *t;
11169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011173 if (mch_lstat((char *)fname, &st) >= 0)
11174 {
11175#ifdef S_ISREG
11176 if (S_ISREG(st.st_mode))
11177 t = "file";
11178 else if (S_ISDIR(st.st_mode))
11179 t = "dir";
11180# ifdef S_ISLNK
11181 else if (S_ISLNK(st.st_mode))
11182 t = "link";
11183# endif
11184# ifdef S_ISBLK
11185 else if (S_ISBLK(st.st_mode))
11186 t = "bdev";
11187# endif
11188# ifdef S_ISCHR
11189 else if (S_ISCHR(st.st_mode))
11190 t = "cdev";
11191# endif
11192# ifdef S_ISFIFO
11193 else if (S_ISFIFO(st.st_mode))
11194 t = "fifo";
11195# endif
11196# ifdef S_ISSOCK
11197 else if (S_ISSOCK(st.st_mode))
11198 t = "fifo";
11199# endif
11200 else
11201 t = "other";
11202#else
11203# ifdef S_IFMT
11204 switch (st.st_mode & S_IFMT)
11205 {
11206 case S_IFREG: t = "file"; break;
11207 case S_IFDIR: t = "dir"; break;
11208# ifdef S_IFLNK
11209 case S_IFLNK: t = "link"; break;
11210# endif
11211# ifdef S_IFBLK
11212 case S_IFBLK: t = "bdev"; break;
11213# endif
11214# ifdef S_IFCHR
11215 case S_IFCHR: t = "cdev"; break;
11216# endif
11217# ifdef S_IFIFO
11218 case S_IFIFO: t = "fifo"; break;
11219# endif
11220# ifdef S_IFSOCK
11221 case S_IFSOCK: t = "socket"; break;
11222# endif
11223 default: t = "other";
11224 }
11225# else
11226 if (mch_isdir(fname))
11227 t = "dir";
11228 else
11229 t = "file";
11230# endif
11231#endif
11232 type = vim_strsave((char_u *)t);
11233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011235}
11236
11237/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011238 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011239 */
11240 static void
11241f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011242 typval_T *argvars;
11243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011244{
11245 linenr_T lnum;
11246 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011247 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248
11249 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011250 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011251 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011252 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011253 retlist = FALSE;
11254 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011256 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011257 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011258 retlist = TRUE;
11259 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011260
Bram Moolenaar342337a2005-07-21 21:11:17 +000011261 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011262}
11263
11264/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011265 * "getmatches()" function
11266 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011267 static void
11268f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011269 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011270 typval_T *rettv;
11271{
11272#ifdef FEAT_SEARCH_EXTRA
11273 dict_T *dict;
11274 matchitem_T *cur = curwin->w_match_head;
11275
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011276 if (rettv_list_alloc(rettv) == OK)
11277 {
11278 while (cur != NULL)
11279 {
11280 dict = dict_alloc();
11281 if (dict == NULL)
11282 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011283 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11284 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11285 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11286 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11287 list_append_dict(rettv->vval.v_list, dict);
11288 cur = cur->next;
11289 }
11290 }
11291#endif
11292}
11293
11294/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011295 * "getpid()" function
11296 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011297 static void
11298f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011299 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011300 typval_T *rettv;
11301{
11302 rettv->vval.v_number = mch_get_pid();
11303}
11304
11305/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011306 * "getpos(string)" function
11307 */
11308 static void
11309f_getpos(argvars, rettv)
11310 typval_T *argvars;
11311 typval_T *rettv;
11312{
11313 pos_T *fp;
11314 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011315 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011316
11317 if (rettv_list_alloc(rettv) == OK)
11318 {
11319 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011320 fp = var2fpos(&argvars[0], TRUE, &fnum);
11321 if (fnum != -1)
11322 list_append_number(l, (varnumber_T)fnum);
11323 else
11324 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011325 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11326 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011327 list_append_number(l, (fp != NULL)
11328 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011329 : (varnumber_T)0);
11330 list_append_number(l,
11331#ifdef FEAT_VIRTUALEDIT
11332 (fp != NULL) ? (varnumber_T)fp->coladd :
11333#endif
11334 (varnumber_T)0);
11335 }
11336 else
11337 rettv->vval.v_number = FALSE;
11338}
11339
11340/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011341 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011342 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011343 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011344f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011345 typval_T *argvars UNUSED;
11346 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011347{
11348#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011349 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011350#endif
11351
Bram Moolenaar2641f772005-03-25 21:58:17 +000011352#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011353 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011354 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011355 wp = NULL;
11356 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11357 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011358 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011359 if (wp == NULL)
11360 return;
11361 }
11362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011363 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011364 }
11365#endif
11366}
11367
11368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 * "getreg()" function
11370 */
11371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
11376 char_u *strregname;
11377 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011378 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011381 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 strregname = get_tv_string_chk(&argvars[0]);
11384 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011385 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 regname = (strregname == NULL ? '"' : *strregname);
11391 if (regname == 0)
11392 regname = '"';
11393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 rettv->vval.v_string = error ? NULL :
11396 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397}
11398
11399/*
11400 * "getregtype()" function
11401 */
11402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *argvars;
11405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406{
11407 char_u *strregname;
11408 int regname;
11409 char_u buf[NUMBUFLEN + 2];
11410 long reglen = 0;
11411
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011412 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 {
11414 strregname = get_tv_string_chk(&argvars[0]);
11415 if (strregname == NULL) /* type error; errmsg already given */
11416 {
11417 rettv->v_type = VAR_STRING;
11418 rettv->vval.v_string = NULL;
11419 return;
11420 }
11421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 else
11423 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011424 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425
11426 regname = (strregname == NULL ? '"' : *strregname);
11427 if (regname == 0)
11428 regname = '"';
11429
11430 buf[0] = NUL;
11431 buf[1] = NUL;
11432 switch (get_reg_type(regname, &reglen))
11433 {
11434 case MLINE: buf[0] = 'V'; break;
11435 case MCHAR: buf[0] = 'v'; break;
11436#ifdef FEAT_VISUAL
11437 case MBLOCK:
11438 buf[0] = Ctrl_V;
11439 sprintf((char *)buf + 1, "%ld", reglen + 1);
11440 break;
11441#endif
11442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445}
11446
11447/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011448 * "gettabvar()" function
11449 */
11450 static void
11451f_gettabvar(argvars, rettv)
11452 typval_T *argvars;
11453 typval_T *rettv;
11454{
11455 tabpage_T *tp;
11456 dictitem_T *v;
11457 char_u *varname;
11458
11459 rettv->v_type = VAR_STRING;
11460 rettv->vval.v_string = NULL;
11461
11462 varname = get_tv_string_chk(&argvars[1]);
11463 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11464 if (tp != NULL && varname != NULL)
11465 {
11466 /* look up the variable */
11467 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11468 if (v != NULL)
11469 copy_tv(&v->di_tv, rettv);
11470 }
11471}
11472
11473/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011474 * "gettabwinvar()" function
11475 */
11476 static void
11477f_gettabwinvar(argvars, rettv)
11478 typval_T *argvars;
11479 typval_T *rettv;
11480{
11481 getwinvar(argvars, rettv, 1);
11482}
11483
11484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 * "getwinposx()" function
11486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011489 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493#ifdef FEAT_GUI
11494 if (gui.in_use)
11495 {
11496 int x, y;
11497
11498 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 }
11501#endif
11502}
11503
11504/*
11505 * "getwinposy()" function
11506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513#ifdef FEAT_GUI
11514 if (gui.in_use)
11515 {
11516 int x, y;
11517
11518 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 }
11521#endif
11522}
11523
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011524/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011525 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011526 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011527 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011528find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011529 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011530 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011531{
11532#ifdef FEAT_WINDOWS
11533 win_T *wp;
11534#endif
11535 int nr;
11536
11537 nr = get_tv_number_chk(vp, NULL);
11538
11539#ifdef FEAT_WINDOWS
11540 if (nr < 0)
11541 return NULL;
11542 if (nr == 0)
11543 return curwin;
11544
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011545 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11546 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011547 if (--nr <= 0)
11548 break;
11549 return wp;
11550#else
11551 if (nr == 0 || nr == 1)
11552 return curwin;
11553 return NULL;
11554#endif
11555}
11556
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557/*
11558 * "getwinvar()" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011565 getwinvar(argvars, rettv, 0);
11566}
11567
11568/*
11569 * getwinvar() and gettabwinvar()
11570 */
11571 static void
11572getwinvar(argvars, rettv, off)
11573 typval_T *argvars;
11574 typval_T *rettv;
11575 int off; /* 1 for gettabwinvar() */
11576{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 win_T *win, *oldcurwin;
11578 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011579 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011580 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011582#ifdef FEAT_WINDOWS
11583 if (off == 1)
11584 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11585 else
11586 tp = curtab;
11587#endif
11588 win = find_win_by_nr(&argvars[off], tp);
11589 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->v_type = VAR_STRING;
11593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594
11595 if (win != NULL && varname != NULL)
11596 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011597 /* Set curwin to be our win, temporarily. Also set curbuf, so
11598 * that we can get buffer-local options. */
11599 oldcurwin = curwin;
11600 curwin = win;
11601 curbuf = win->w_buffer;
11602
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 else
11606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011607 if (*varname == NUL)
11608 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11609 * scope prefix before the NUL byte is required by
11610 * find_var_in_ht(). */
11611 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011613 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011615 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011617
11618 /* restore previous notion of curwin */
11619 curwin = oldcurwin;
11620 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 }
11622
11623 --emsg_off;
11624}
11625
11626/*
11627 * "glob()" function
11628 */
11629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011631 typval_T *argvars;
11632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011634 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011636 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011638 /* When the optional second argument is non-zero, don't remove matches
11639 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11640 if (argvars[1].v_type != VAR_UNKNOWN
11641 && get_tv_number_chk(&argvars[1], &error))
11642 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011644 if (!error)
11645 {
11646 ExpandInit(&xpc);
11647 xpc.xp_context = EXPAND_FILES;
11648 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11649 NULL, flags, WILD_ALL);
11650 }
11651 else
11652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
11656 * "globpath()" function
11657 */
11658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 typval_T *argvars;
11661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011663 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011666 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011668 /* When the optional second argument is non-zero, don't remove matches
11669 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11670 if (argvars[2].v_type != VAR_UNKNOWN
11671 && get_tv_number_chk(&argvars[2], &error))
11672 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011674 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 rettv->vval.v_string = NULL;
11676 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011677 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11678 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679}
11680
11681/*
11682 * "has()" function
11683 */
11684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 typval_T *argvars;
11687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688{
11689 int i;
11690 char_u *name;
11691 int n = FALSE;
11692 static char *(has_list[]) =
11693 {
11694#ifdef AMIGA
11695 "amiga",
11696# ifdef FEAT_ARP
11697 "arp",
11698# endif
11699#endif
11700#ifdef __BEOS__
11701 "beos",
11702#endif
11703#ifdef MSDOS
11704# ifdef DJGPP
11705 "dos32",
11706# else
11707 "dos16",
11708# endif
11709#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011710#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 "mac",
11712#endif
11713#if defined(MACOS_X_UNIX)
11714 "macunix",
11715#endif
11716#ifdef OS2
11717 "os2",
11718#endif
11719#ifdef __QNX__
11720 "qnx",
11721#endif
11722#ifdef RISCOS
11723 "riscos",
11724#endif
11725#ifdef UNIX
11726 "unix",
11727#endif
11728#ifdef VMS
11729 "vms",
11730#endif
11731#ifdef WIN16
11732 "win16",
11733#endif
11734#ifdef WIN32
11735 "win32",
11736#endif
11737#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11738 "win32unix",
11739#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011740#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 "win64",
11742#endif
11743#ifdef EBCDIC
11744 "ebcdic",
11745#endif
11746#ifndef CASE_INSENSITIVE_FILENAME
11747 "fname_case",
11748#endif
11749#ifdef FEAT_ARABIC
11750 "arabic",
11751#endif
11752#ifdef FEAT_AUTOCMD
11753 "autocmd",
11754#endif
11755#ifdef FEAT_BEVAL
11756 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011757# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11758 "balloon_multiline",
11759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760#endif
11761#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11762 "builtin_terms",
11763# ifdef ALL_BUILTIN_TCAPS
11764 "all_builtin_terms",
11765# endif
11766#endif
11767#ifdef FEAT_BYTEOFF
11768 "byte_offset",
11769#endif
11770#ifdef FEAT_CINDENT
11771 "cindent",
11772#endif
11773#ifdef FEAT_CLIENTSERVER
11774 "clientserver",
11775#endif
11776#ifdef FEAT_CLIPBOARD
11777 "clipboard",
11778#endif
11779#ifdef FEAT_CMDL_COMPL
11780 "cmdline_compl",
11781#endif
11782#ifdef FEAT_CMDHIST
11783 "cmdline_hist",
11784#endif
11785#ifdef FEAT_COMMENTS
11786 "comments",
11787#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011788#ifdef FEAT_CONCEAL
11789 "conceal",
11790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791#ifdef FEAT_CRYPT
11792 "cryptv",
11793#endif
11794#ifdef FEAT_CSCOPE
11795 "cscope",
11796#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011797#ifdef FEAT_CURSORBIND
11798 "cursorbind",
11799#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011800#ifdef CURSOR_SHAPE
11801 "cursorshape",
11802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803#ifdef DEBUG
11804 "debug",
11805#endif
11806#ifdef FEAT_CON_DIALOG
11807 "dialog_con",
11808#endif
11809#ifdef FEAT_GUI_DIALOG
11810 "dialog_gui",
11811#endif
11812#ifdef FEAT_DIFF
11813 "diff",
11814#endif
11815#ifdef FEAT_DIGRAPHS
11816 "digraphs",
11817#endif
11818#ifdef FEAT_DND
11819 "dnd",
11820#endif
11821#ifdef FEAT_EMACS_TAGS
11822 "emacs_tags",
11823#endif
11824 "eval", /* always present, of course! */
11825#ifdef FEAT_EX_EXTRA
11826 "ex_extra",
11827#endif
11828#ifdef FEAT_SEARCH_EXTRA
11829 "extra_search",
11830#endif
11831#ifdef FEAT_FKMAP
11832 "farsi",
11833#endif
11834#ifdef FEAT_SEARCHPATH
11835 "file_in_path",
11836#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011837#if defined(UNIX) && !defined(USE_SYSTEM)
11838 "filterpipe",
11839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840#ifdef FEAT_FIND_ID
11841 "find_in_path",
11842#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011843#ifdef FEAT_FLOAT
11844 "float",
11845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846#ifdef FEAT_FOLDING
11847 "folding",
11848#endif
11849#ifdef FEAT_FOOTER
11850 "footer",
11851#endif
11852#if !defined(USE_SYSTEM) && defined(UNIX)
11853 "fork",
11854#endif
11855#ifdef FEAT_GETTEXT
11856 "gettext",
11857#endif
11858#ifdef FEAT_GUI
11859 "gui",
11860#endif
11861#ifdef FEAT_GUI_ATHENA
11862# ifdef FEAT_GUI_NEXTAW
11863 "gui_neXtaw",
11864# else
11865 "gui_athena",
11866# endif
11867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868#ifdef FEAT_GUI_GTK
11869 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011872#ifdef FEAT_GUI_GNOME
11873 "gui_gnome",
11874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875#ifdef FEAT_GUI_MAC
11876 "gui_mac",
11877#endif
11878#ifdef FEAT_GUI_MOTIF
11879 "gui_motif",
11880#endif
11881#ifdef FEAT_GUI_PHOTON
11882 "gui_photon",
11883#endif
11884#ifdef FEAT_GUI_W16
11885 "gui_win16",
11886#endif
11887#ifdef FEAT_GUI_W32
11888 "gui_win32",
11889#endif
11890#ifdef FEAT_HANGULIN
11891 "hangul_input",
11892#endif
11893#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11894 "iconv",
11895#endif
11896#ifdef FEAT_INS_EXPAND
11897 "insert_expand",
11898#endif
11899#ifdef FEAT_JUMPLIST
11900 "jumplist",
11901#endif
11902#ifdef FEAT_KEYMAP
11903 "keymap",
11904#endif
11905#ifdef FEAT_LANGMAP
11906 "langmap",
11907#endif
11908#ifdef FEAT_LIBCALL
11909 "libcall",
11910#endif
11911#ifdef FEAT_LINEBREAK
11912 "linebreak",
11913#endif
11914#ifdef FEAT_LISP
11915 "lispindent",
11916#endif
11917#ifdef FEAT_LISTCMDS
11918 "listcmds",
11919#endif
11920#ifdef FEAT_LOCALMAP
11921 "localmap",
11922#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011923#ifdef FEAT_LUA
11924# ifndef DYNAMIC_LUA
11925 "lua",
11926# endif
11927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928#ifdef FEAT_MENU
11929 "menu",
11930#endif
11931#ifdef FEAT_SESSION
11932 "mksession",
11933#endif
11934#ifdef FEAT_MODIFY_FNAME
11935 "modify_fname",
11936#endif
11937#ifdef FEAT_MOUSE
11938 "mouse",
11939#endif
11940#ifdef FEAT_MOUSESHAPE
11941 "mouseshape",
11942#endif
11943#if defined(UNIX) || defined(VMS)
11944# ifdef FEAT_MOUSE_DEC
11945 "mouse_dec",
11946# endif
11947# ifdef FEAT_MOUSE_GPM
11948 "mouse_gpm",
11949# endif
11950# ifdef FEAT_MOUSE_JSB
11951 "mouse_jsbterm",
11952# endif
11953# ifdef FEAT_MOUSE_NET
11954 "mouse_netterm",
11955# endif
11956# ifdef FEAT_MOUSE_PTERM
11957 "mouse_pterm",
11958# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011959# ifdef FEAT_SYSMOUSE
11960 "mouse_sysmouse",
11961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962# ifdef FEAT_MOUSE_XTERM
11963 "mouse_xterm",
11964# endif
11965#endif
11966#ifdef FEAT_MBYTE
11967 "multi_byte",
11968#endif
11969#ifdef FEAT_MBYTE_IME
11970 "multi_byte_ime",
11971#endif
11972#ifdef FEAT_MULTI_LANG
11973 "multi_lang",
11974#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011975#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011976#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011977 "mzscheme",
11978#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980#ifdef FEAT_OLE
11981 "ole",
11982#endif
11983#ifdef FEAT_OSFILETYPE
11984 "osfiletype",
11985#endif
11986#ifdef FEAT_PATH_EXTRA
11987 "path_extra",
11988#endif
11989#ifdef FEAT_PERL
11990#ifndef DYNAMIC_PERL
11991 "perl",
11992#endif
11993#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011994#ifdef FEAT_PERSISTENT_UNDO
11995 "persistent_undo",
11996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997#ifdef FEAT_PYTHON
11998#ifndef DYNAMIC_PYTHON
11999 "python",
12000#endif
12001#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012002#ifdef FEAT_PYTHON3
12003#ifndef DYNAMIC_PYTHON3
12004 "python3",
12005#endif
12006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007#ifdef FEAT_POSTSCRIPT
12008 "postscript",
12009#endif
12010#ifdef FEAT_PRINTER
12011 "printer",
12012#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012013#ifdef FEAT_PROFILE
12014 "profile",
12015#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012016#ifdef FEAT_RELTIME
12017 "reltime",
12018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019#ifdef FEAT_QUICKFIX
12020 "quickfix",
12021#endif
12022#ifdef FEAT_RIGHTLEFT
12023 "rightleft",
12024#endif
12025#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12026 "ruby",
12027#endif
12028#ifdef FEAT_SCROLLBIND
12029 "scrollbind",
12030#endif
12031#ifdef FEAT_CMDL_INFO
12032 "showcmd",
12033 "cmdline_info",
12034#endif
12035#ifdef FEAT_SIGNS
12036 "signs",
12037#endif
12038#ifdef FEAT_SMARTINDENT
12039 "smartindent",
12040#endif
12041#ifdef FEAT_SNIFF
12042 "sniff",
12043#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012044#ifdef STARTUPTIME
12045 "startuptime",
12046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047#ifdef FEAT_STL_OPT
12048 "statusline",
12049#endif
12050#ifdef FEAT_SUN_WORKSHOP
12051 "sun_workshop",
12052#endif
12053#ifdef FEAT_NETBEANS_INTG
12054 "netbeans_intg",
12055#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012056#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012057 "spell",
12058#endif
12059#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 "syntax",
12061#endif
12062#if defined(USE_SYSTEM) || !defined(UNIX)
12063 "system",
12064#endif
12065#ifdef FEAT_TAG_BINS
12066 "tag_binary",
12067#endif
12068#ifdef FEAT_TAG_OLDSTATIC
12069 "tag_old_static",
12070#endif
12071#ifdef FEAT_TAG_ANYWHITE
12072 "tag_any_white",
12073#endif
12074#ifdef FEAT_TCL
12075# ifndef DYNAMIC_TCL
12076 "tcl",
12077# endif
12078#endif
12079#ifdef TERMINFO
12080 "terminfo",
12081#endif
12082#ifdef FEAT_TERMRESPONSE
12083 "termresponse",
12084#endif
12085#ifdef FEAT_TEXTOBJ
12086 "textobjects",
12087#endif
12088#ifdef HAVE_TGETENT
12089 "tgetent",
12090#endif
12091#ifdef FEAT_TITLE
12092 "title",
12093#endif
12094#ifdef FEAT_TOOLBAR
12095 "toolbar",
12096#endif
12097#ifdef FEAT_USR_CMDS
12098 "user-commands", /* was accidentally included in 5.4 */
12099 "user_commands",
12100#endif
12101#ifdef FEAT_VIMINFO
12102 "viminfo",
12103#endif
12104#ifdef FEAT_VERTSPLIT
12105 "vertsplit",
12106#endif
12107#ifdef FEAT_VIRTUALEDIT
12108 "virtualedit",
12109#endif
12110#ifdef FEAT_VISUAL
12111 "visual",
12112#endif
12113#ifdef FEAT_VISUALEXTRA
12114 "visualextra",
12115#endif
12116#ifdef FEAT_VREPLACE
12117 "vreplace",
12118#endif
12119#ifdef FEAT_WILDIGN
12120 "wildignore",
12121#endif
12122#ifdef FEAT_WILDMENU
12123 "wildmenu",
12124#endif
12125#ifdef FEAT_WINDOWS
12126 "windows",
12127#endif
12128#ifdef FEAT_WAK
12129 "winaltkeys",
12130#endif
12131#ifdef FEAT_WRITEBACKUP
12132 "writebackup",
12133#endif
12134#ifdef FEAT_XIM
12135 "xim",
12136#endif
12137#ifdef FEAT_XFONTSET
12138 "xfontset",
12139#endif
12140#ifdef USE_XSMP
12141 "xsmp",
12142#endif
12143#ifdef USE_XSMP_INTERACT
12144 "xsmp_interact",
12145#endif
12146#ifdef FEAT_XCLIPBOARD
12147 "xterm_clipboard",
12148#endif
12149#ifdef FEAT_XTERM_SAVE
12150 "xterm_save",
12151#endif
12152#if defined(UNIX) && defined(FEAT_X11)
12153 "X11",
12154#endif
12155 NULL
12156 };
12157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 for (i = 0; has_list[i] != NULL; ++i)
12160 if (STRICMP(name, has_list[i]) == 0)
12161 {
12162 n = TRUE;
12163 break;
12164 }
12165
12166 if (n == FALSE)
12167 {
12168 if (STRNICMP(name, "patch", 5) == 0)
12169 n = has_patch(atoi((char *)name + 5));
12170 else if (STRICMP(name, "vim_starting") == 0)
12171 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012172#ifdef FEAT_MBYTE
12173 else if (STRICMP(name, "multi_byte_encoding") == 0)
12174 n = has_mbyte;
12175#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012176#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12177 else if (STRICMP(name, "balloon_multiline") == 0)
12178 n = multiline_balloon_available();
12179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180#ifdef DYNAMIC_TCL
12181 else if (STRICMP(name, "tcl") == 0)
12182 n = tcl_enabled(FALSE);
12183#endif
12184#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12185 else if (STRICMP(name, "iconv") == 0)
12186 n = iconv_enabled(FALSE);
12187#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012188#ifdef DYNAMIC_LUA
12189 else if (STRICMP(name, "lua") == 0)
12190 n = lua_enabled(FALSE);
12191#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012192#ifdef DYNAMIC_MZSCHEME
12193 else if (STRICMP(name, "mzscheme") == 0)
12194 n = mzscheme_enabled(FALSE);
12195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196#ifdef DYNAMIC_RUBY
12197 else if (STRICMP(name, "ruby") == 0)
12198 n = ruby_enabled(FALSE);
12199#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012200#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201#ifdef DYNAMIC_PYTHON
12202 else if (STRICMP(name, "python") == 0)
12203 n = python_enabled(FALSE);
12204#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012205#endif
12206#ifdef FEAT_PYTHON3
12207#ifdef DYNAMIC_PYTHON3
12208 else if (STRICMP(name, "python3") == 0)
12209 n = python3_enabled(FALSE);
12210#endif
12211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212#ifdef DYNAMIC_PERL
12213 else if (STRICMP(name, "perl") == 0)
12214 n = perl_enabled(FALSE);
12215#endif
12216#ifdef FEAT_GUI
12217 else if (STRICMP(name, "gui_running") == 0)
12218 n = (gui.in_use || gui.starting);
12219# ifdef FEAT_GUI_W32
12220 else if (STRICMP(name, "gui_win32s") == 0)
12221 n = gui_is_win32s();
12222# endif
12223# ifdef FEAT_BROWSE
12224 else if (STRICMP(name, "browse") == 0)
12225 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12226# endif
12227#endif
12228#ifdef FEAT_SYN_HL
12229 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012230 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231#endif
12232#if defined(WIN3264)
12233 else if (STRICMP(name, "win95") == 0)
12234 n = mch_windows95();
12235#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012236#ifdef FEAT_NETBEANS_INTG
12237 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012238 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 }
12241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243}
12244
12245/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012246 * "has_key()" function
12247 */
12248 static void
12249f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012250 typval_T *argvars;
12251 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012252{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012253 if (argvars[0].v_type != VAR_DICT)
12254 {
12255 EMSG(_(e_dictreq));
12256 return;
12257 }
12258 if (argvars[0].vval.v_dict == NULL)
12259 return;
12260
12261 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012262 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012263}
12264
12265/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012266 * "haslocaldir()" function
12267 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012268 static void
12269f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012270 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012271 typval_T *rettv;
12272{
12273 rettv->vval.v_number = (curwin->w_localdir != NULL);
12274}
12275
12276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 * "hasmapto()" function
12278 */
12279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012280f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012281 typval_T *argvars;
12282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283{
12284 char_u *name;
12285 char_u *mode;
12286 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012287 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012290 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 mode = (char_u *)"nvo";
12292 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012295 if (argvars[2].v_type != VAR_UNKNOWN)
12296 abbr = get_tv_number(&argvars[2]);
12297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298
Bram Moolenaar2c932302006-03-18 21:42:09 +000012299 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303}
12304
12305/*
12306 * "histadd()" function
12307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012310 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312{
12313#ifdef FEAT_CMDHIST
12314 int histype;
12315 char_u *str;
12316 char_u buf[NUMBUFLEN];
12317#endif
12318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 if (check_restricted() || check_secure())
12321 return;
12322#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012323 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12324 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 if (histype >= 0)
12326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 if (*str != NUL)
12329 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012330 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 return;
12334 }
12335 }
12336#endif
12337}
12338
12339/*
12340 * "histdel()" function
12341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012344 typval_T *argvars UNUSED;
12345 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346{
12347#ifdef FEAT_CMDHIST
12348 int n;
12349 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12353 if (str == NULL)
12354 n = 0;
12355 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012357 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012358 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012360 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 else
12363 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012364 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365 get_tv_string_buf(&argvars[1], buf));
12366 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367#endif
12368}
12369
12370/*
12371 * "histget()" function
12372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012375 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377{
12378#ifdef FEAT_CMDHIST
12379 int type;
12380 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012381 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12384 if (str == NULL)
12385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 {
12388 type = get_histtype(str);
12389 if (argvars[1].v_type == VAR_UNKNOWN)
12390 idx = get_history_idx(type);
12391 else
12392 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12393 /* -1 on type error */
12394 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400}
12401
12402/*
12403 * "histnr()" function
12404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
12410 int i;
12411
12412#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012413 char_u *history = get_tv_string_chk(&argvars[0]);
12414
12415 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 if (i >= HIST_CMD && i < HIST_COUNT)
12417 i = get_history_idx(i);
12418 else
12419#endif
12420 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422}
12423
12424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 * "highlightID(name)" function
12426 */
12427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 typval_T *argvars;
12430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012432 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433}
12434
12435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012436 * "highlight_exists()" function
12437 */
12438 static void
12439f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 typval_T *argvars;
12441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012442{
12443 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12444}
12445
12446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 * "hostname()" function
12448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453{
12454 char_u hostname[256];
12455
12456 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->v_type = VAR_STRING;
12458 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459}
12460
12461/*
12462 * iconv() function
12463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012466 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469#ifdef FEAT_MBYTE
12470 char_u buf1[NUMBUFLEN];
12471 char_u buf2[NUMBUFLEN];
12472 char_u *from, *to, *str;
12473 vimconv_T vimconv;
12474#endif
12475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476 rettv->v_type = VAR_STRING;
12477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478
12479#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 str = get_tv_string(&argvars[0]);
12481 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12482 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 vimconv.vc_type = CONV_NONE;
12484 convert_setup(&vimconv, from, to);
12485
12486 /* If the encodings are equal, no conversion needed. */
12487 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491
12492 convert_setup(&vimconv, NULL, NULL);
12493 vim_free(from);
12494 vim_free(to);
12495#endif
12496}
12497
12498/*
12499 * "indent()" function
12500 */
12501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012503 typval_T *argvars;
12504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505{
12506 linenr_T lnum;
12507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513}
12514
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012515/*
12516 * "index()" function
12517 */
12518 static void
12519f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012520 typval_T *argvars;
12521 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012522{
Bram Moolenaar33570922005-01-25 22:26:29 +000012523 list_T *l;
12524 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012525 long idx = 0;
12526 int ic = FALSE;
12527
12528 rettv->vval.v_number = -1;
12529 if (argvars[0].v_type != VAR_LIST)
12530 {
12531 EMSG(_(e_listreq));
12532 return;
12533 }
12534 l = argvars[0].vval.v_list;
12535 if (l != NULL)
12536 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012537 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540 int error = FALSE;
12541
Bram Moolenaar758711c2005-02-02 23:11:38 +000012542 /* Start at specified item. Use the cached index that list_find()
12543 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012545 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012546 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 ic = get_tv_number_chk(&argvars[3], &error);
12548 if (error)
12549 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012551
Bram Moolenaar758711c2005-02-02 23:11:38 +000012552 for ( ; item != NULL; item = item->li_next, ++idx)
12553 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012554 {
12555 rettv->vval.v_number = idx;
12556 break;
12557 }
12558 }
12559}
12560
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561static int inputsecret_flag = 0;
12562
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012563static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12564
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012566 * This function is used by f_input() and f_inputdialog() functions. The third
12567 * argument to f_input() specifies the type of completion to use at the
12568 * prompt. The third argument to f_inputdialog() specifies the value to return
12569 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 */
12571 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012572get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012573 typval_T *argvars;
12574 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012575 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 char_u *p = NULL;
12579 int c;
12580 char_u buf[NUMBUFLEN];
12581 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012582 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012583 int xp_type = EXPAND_NOTHING;
12584 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012587 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588
12589#ifdef NO_CONSOLE_INPUT
12590 /* While starting up, there is no place to enter text. */
12591 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593#endif
12594
12595 cmd_silent = FALSE; /* Want to see the prompt. */
12596 if (prompt != NULL)
12597 {
12598 /* Only the part of the message after the last NL is considered as
12599 * prompt for the command line */
12600 p = vim_strrchr(prompt, '\n');
12601 if (p == NULL)
12602 p = prompt;
12603 else
12604 {
12605 ++p;
12606 c = *p;
12607 *p = NUL;
12608 msg_start();
12609 msg_clr_eos();
12610 msg_puts_attr(prompt, echo_attr);
12611 msg_didout = FALSE;
12612 msg_starthere();
12613 *p = c;
12614 }
12615 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012617 if (argvars[1].v_type != VAR_UNKNOWN)
12618 {
12619 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12620 if (defstr != NULL)
12621 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012623 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012624 {
12625 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012626 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012627 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012628
Bram Moolenaar4463f292005-09-25 22:20:24 +000012629 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012630
Bram Moolenaar4463f292005-09-25 22:20:24 +000012631 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12632 if (xp_name == NULL)
12633 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012635 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012636
Bram Moolenaar4463f292005-09-25 22:20:24 +000012637 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12638 &xp_arg) == FAIL)
12639 return;
12640 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012641 }
12642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 if (defstr != NULL)
12644 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012645 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12646 xp_type, xp_arg);
12647
12648 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650 /* since the user typed this, no need to wait for return */
12651 need_wait_return = FALSE;
12652 msg_didout = FALSE;
12653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 cmd_silent = cmd_silent_save;
12655}
12656
12657/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012658 * "input()" function
12659 * Also handles inputsecret() when inputsecret is set.
12660 */
12661 static void
12662f_input(argvars, rettv)
12663 typval_T *argvars;
12664 typval_T *rettv;
12665{
12666 get_user_input(argvars, rettv, FALSE);
12667}
12668
12669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 * "inputdialog()" function
12671 */
12672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 typval_T *argvars;
12675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676{
12677#if defined(FEAT_GUI_TEXTDIALOG)
12678 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12679 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12680 {
12681 char_u *message;
12682 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 message = get_tv_string_chk(&argvars[0]);
12686 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012687 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012688 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 else
12690 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691 if (message != NULL && defstr != NULL
12692 && do_dialog(VIM_QUESTION, NULL, message,
12693 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 else
12696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 if (message != NULL && defstr != NULL
12698 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012699 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->vval.v_string = vim_strsave(
12701 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 }
12707 else
12708#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012709 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710}
12711
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012712/*
12713 * "inputlist()" function
12714 */
12715 static void
12716f_inputlist(argvars, rettv)
12717 typval_T *argvars;
12718 typval_T *rettv;
12719{
12720 listitem_T *li;
12721 int selected;
12722 int mouse_used;
12723
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012724#ifdef NO_CONSOLE_INPUT
12725 /* While starting up, there is no place to enter text. */
12726 if (no_console_input())
12727 return;
12728#endif
12729 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12730 {
12731 EMSG2(_(e_listarg), "inputlist()");
12732 return;
12733 }
12734
12735 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012736 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012737 lines_left = Rows; /* avoid more prompt */
12738 msg_scroll = TRUE;
12739 msg_clr_eos();
12740
12741 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12742 {
12743 msg_puts(get_tv_string(&li->li_tv));
12744 msg_putchar('\n');
12745 }
12746
12747 /* Ask for choice. */
12748 selected = prompt_for_number(&mouse_used);
12749 if (mouse_used)
12750 selected -= lines_left;
12751
12752 rettv->vval.v_number = selected;
12753}
12754
12755
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12757
12758/*
12759 * "inputrestore()" function
12760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766 if (ga_userinput.ga_len > 0)
12767 {
12768 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12770 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012771 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 }
12773 else if (p_verbose > 1)
12774 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012775 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 }
12778}
12779
12780/*
12781 * "inputsave()" function
12782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012788 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 if (ga_grow(&ga_userinput, 1) == OK)
12790 {
12791 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12792 + ga_userinput.ga_len);
12793 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012794 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 }
12796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
12800/*
12801 * "inputsecret()" function
12802 */
12803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 typval_T *argvars;
12806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807{
12808 ++cmdline_star;
12809 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 --cmdline_star;
12812 --inputsecret_flag;
12813}
12814
12815/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012816 * "insert()" function
12817 */
12818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 typval_T *argvars;
12821 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012822{
12823 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 listitem_T *item;
12825 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012827
12828 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012829 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012830 else if ((l = argvars[0].vval.v_list) != NULL
12831 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012832 {
12833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 before = get_tv_number_chk(&argvars[2], &error);
12835 if (error)
12836 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012837
Bram Moolenaar758711c2005-02-02 23:11:38 +000012838 if (before == l->lv_len)
12839 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012840 else
12841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012842 item = list_find(l, before);
12843 if (item == NULL)
12844 {
12845 EMSGN(_(e_listidx), before);
12846 l = NULL;
12847 }
12848 }
12849 if (l != NULL)
12850 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012851 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012852 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012853 }
12854 }
12855}
12856
12857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 * "isdirectory()" function
12859 */
12860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012862 typval_T *argvars;
12863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866}
12867
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012868/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012869 * "islocked()" function
12870 */
12871 static void
12872f_islocked(argvars, rettv)
12873 typval_T *argvars;
12874 typval_T *rettv;
12875{
12876 lval_T lv;
12877 char_u *end;
12878 dictitem_T *di;
12879
12880 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012881 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12882 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012883 if (end != NULL && lv.ll_name != NULL)
12884 {
12885 if (*end != NUL)
12886 EMSG(_(e_trailing));
12887 else
12888 {
12889 if (lv.ll_tv == NULL)
12890 {
12891 if (check_changedtick(lv.ll_name))
12892 rettv->vval.v_number = 1; /* always locked */
12893 else
12894 {
12895 di = find_var(lv.ll_name, NULL);
12896 if (di != NULL)
12897 {
12898 /* Consider a variable locked when:
12899 * 1. the variable itself is locked
12900 * 2. the value of the variable is locked.
12901 * 3. the List or Dict value is locked.
12902 */
12903 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12904 || tv_islocked(&di->di_tv));
12905 }
12906 }
12907 }
12908 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012909 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012910 else if (lv.ll_newkey != NULL)
12911 EMSG2(_(e_dictkey), lv.ll_newkey);
12912 else if (lv.ll_list != NULL)
12913 /* List item. */
12914 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12915 else
12916 /* Dictionary item. */
12917 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12918 }
12919 }
12920
12921 clear_lval(&lv);
12922}
12923
Bram Moolenaar33570922005-01-25 22:26:29 +000012924static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012925
12926/*
12927 * Turn a dict into a list:
12928 * "what" == 0: list of keys
12929 * "what" == 1: list of values
12930 * "what" == 2: list of items
12931 */
12932 static void
12933dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012934 typval_T *argvars;
12935 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012936 int what;
12937{
Bram Moolenaar33570922005-01-25 22:26:29 +000012938 list_T *l2;
12939 dictitem_T *di;
12940 hashitem_T *hi;
12941 listitem_T *li;
12942 listitem_T *li2;
12943 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012944 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012945
Bram Moolenaar8c711452005-01-14 21:53:12 +000012946 if (argvars[0].v_type != VAR_DICT)
12947 {
12948 EMSG(_(e_dictreq));
12949 return;
12950 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012951 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012952 return;
12953
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012954 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012955 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012956
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012957 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012959 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012960 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012961 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012962 --todo;
12963 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012964
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012965 li = listitem_alloc();
12966 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012967 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012968 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012969
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012970 if (what == 0)
12971 {
12972 /* keys() */
12973 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012974 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012975 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12976 }
12977 else if (what == 1)
12978 {
12979 /* values() */
12980 copy_tv(&di->di_tv, &li->li_tv);
12981 }
12982 else
12983 {
12984 /* items() */
12985 l2 = list_alloc();
12986 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012987 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012988 li->li_tv.vval.v_list = l2;
12989 if (l2 == NULL)
12990 break;
12991 ++l2->lv_refcount;
12992
12993 li2 = listitem_alloc();
12994 if (li2 == NULL)
12995 break;
12996 list_append(l2, li2);
12997 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012998 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012999 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13000
13001 li2 = listitem_alloc();
13002 if (li2 == NULL)
13003 break;
13004 list_append(l2, li2);
13005 copy_tv(&di->di_tv, &li2->li_tv);
13006 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007 }
13008 }
13009}
13010
13011/*
13012 * "items(dict)" function
13013 */
13014 static void
13015f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013016 typval_T *argvars;
13017 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013018{
13019 dict_list(argvars, rettv, 2);
13020}
13021
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013023 * "join()" function
13024 */
13025 static void
13026f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013027 typval_T *argvars;
13028 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013029{
13030 garray_T ga;
13031 char_u *sep;
13032
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013033 if (argvars[0].v_type != VAR_LIST)
13034 {
13035 EMSG(_(e_listreq));
13036 return;
13037 }
13038 if (argvars[0].vval.v_list == NULL)
13039 return;
13040 if (argvars[1].v_type == VAR_UNKNOWN)
13041 sep = (char_u *)" ";
13042 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013044
13045 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046
13047 if (sep != NULL)
13048 {
13049 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013050 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 ga_append(&ga, NUL);
13052 rettv->vval.v_string = (char_u *)ga.ga_data;
13053 }
13054 else
13055 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013056}
13057
13058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013059 * "keys()" function
13060 */
13061 static void
13062f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013063 typval_T *argvars;
13064 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013065{
13066 dict_list(argvars, rettv, 0);
13067}
13068
13069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 * "last_buffer_nr()" function.
13071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013074 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076{
13077 int n = 0;
13078 buf_T *buf;
13079
13080 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13081 if (n < buf->b_fnum)
13082 n = buf->b_fnum;
13083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013084 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013085}
13086
13087/*
13088 * "len()" function
13089 */
13090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013092 typval_T *argvars;
13093 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013094{
13095 switch (argvars[0].v_type)
13096 {
13097 case VAR_STRING:
13098 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013099 rettv->vval.v_number = (varnumber_T)STRLEN(
13100 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013101 break;
13102 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013104 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013105 case VAR_DICT:
13106 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13107 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013108 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013109 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013110 break;
13111 }
13112}
13113
Bram Moolenaar33570922005-01-25 22:26:29 +000013114static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013115
13116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013118 typval_T *argvars;
13119 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013120 int type;
13121{
13122#ifdef FEAT_LIBCALL
13123 char_u *string_in;
13124 char_u **string_result;
13125 int nr_result;
13126#endif
13127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013129 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013131
13132 if (check_restricted() || check_secure())
13133 return;
13134
13135#ifdef FEAT_LIBCALL
13136 /* The first two args must be strings, otherwise its meaningless */
13137 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13138 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013139 string_in = NULL;
13140 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013141 string_in = argvars[2].vval.v_string;
13142 if (type == VAR_NUMBER)
13143 string_result = NULL;
13144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013146 if (mch_libcall(argvars[0].vval.v_string,
13147 argvars[1].vval.v_string,
13148 string_in,
13149 argvars[2].vval.v_number,
13150 string_result,
13151 &nr_result) == OK
13152 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013154 }
13155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156}
13157
13158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013159 * "libcall()" function
13160 */
13161 static void
13162f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *argvars;
13164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013165{
13166 libcall_common(argvars, rettv, VAR_STRING);
13167}
13168
13169/*
13170 * "libcallnr()" function
13171 */
13172 static void
13173f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013174 typval_T *argvars;
13175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013176{
13177 libcall_common(argvars, rettv, VAR_NUMBER);
13178}
13179
13180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 * "line(string)" function
13182 */
13183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *argvars;
13186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187{
13188 linenr_T lnum = 0;
13189 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013190 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013192 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 if (fp != NULL)
13194 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
13199 * "line2byte(lnum)" function
13200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013203 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208#else
13209 linenr_T lnum;
13210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13216 if (rettv->vval.v_number >= 0)
13217 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218#endif
13219}
13220
13221/*
13222 * "lispindent(lnum)" function
13223 */
13224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013226 typval_T *argvars;
13227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228{
13229#ifdef FEAT_LISP
13230 pos_T pos;
13231 linenr_T lnum;
13232
13233 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13236 {
13237 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 curwin->w_cursor = pos;
13240 }
13241 else
13242#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244}
13245
13246/*
13247 * "localtime()" function
13248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013251 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255}
13256
Bram Moolenaar33570922005-01-25 22:26:29 +000013257static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258
13259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013260get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013261 typval_T *argvars;
13262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 int exact;
13264{
13265 char_u *keys;
13266 char_u *which;
13267 char_u buf[NUMBUFLEN];
13268 char_u *keys_buf = NULL;
13269 char_u *rhs;
13270 int mode;
13271 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013272 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273
13274 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013275 rettv->v_type = VAR_STRING;
13276 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 if (*keys == NUL)
13280 return;
13281
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013282 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013285 if (argvars[2].v_type != VAR_UNKNOWN)
13286 abbr = get_tv_number(&argvars[2]);
13287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 else
13289 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013290 if (which == NULL)
13291 return;
13292
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 mode = get_map_mode(&which, 0);
13294
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013295 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013296 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 vim_free(keys_buf);
13298 if (rhs != NULL)
13299 {
13300 ga_init(&ga);
13301 ga.ga_itemsize = 1;
13302 ga.ga_growsize = 40;
13303
13304 while (*rhs != NUL)
13305 ga_concat(&ga, str2special(&rhs, FALSE));
13306
13307 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 }
13310}
13311
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013312#ifdef FEAT_FLOAT
13313/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013314 * "log()" function
13315 */
13316 static void
13317f_log(argvars, rettv)
13318 typval_T *argvars;
13319 typval_T *rettv;
13320{
13321 float_T f;
13322
13323 rettv->v_type = VAR_FLOAT;
13324 if (get_float_arg(argvars, &f) == OK)
13325 rettv->vval.v_float = log(f);
13326 else
13327 rettv->vval.v_float = 0.0;
13328}
13329
13330/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013331 * "log10()" function
13332 */
13333 static void
13334f_log10(argvars, rettv)
13335 typval_T *argvars;
13336 typval_T *rettv;
13337{
13338 float_T f;
13339
13340 rettv->v_type = VAR_FLOAT;
13341 if (get_float_arg(argvars, &f) == OK)
13342 rettv->vval.v_float = log10(f);
13343 else
13344 rettv->vval.v_float = 0.0;
13345}
13346#endif
13347
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013349 * "map()" function
13350 */
13351 static void
13352f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013355{
13356 filter_map(argvars, rettv, TRUE);
13357}
13358
13359/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013360 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 */
13362 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013363f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 typval_T *argvars;
13365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013367 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368}
13369
13370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013371 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 */
13373 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013374f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013375 typval_T *argvars;
13376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379}
13380
Bram Moolenaar33570922005-01-25 22:26:29 +000013381static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382
13383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013385 typval_T *argvars;
13386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 int type;
13388{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013389 char_u *str = NULL;
13390 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 char_u *pat;
13392 regmatch_T regmatch;
13393 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013394 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 char_u *save_cpo;
13396 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013397 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013398 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013399 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 list_T *l = NULL;
13401 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013402 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013403 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404
13405 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13406 save_cpo = p_cpo;
13407 p_cpo = (char_u *)"";
13408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013409 rettv->vval.v_number = -1;
13410 if (type == 3)
13411 {
13412 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013414 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013415 }
13416 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 rettv->v_type = VAR_STRING;
13419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013422 if (argvars[0].v_type == VAR_LIST)
13423 {
13424 if ((l = argvars[0].vval.v_list) == NULL)
13425 goto theend;
13426 li = l->lv_first;
13427 }
13428 else
13429 expr = str = get_tv_string(&argvars[0]);
13430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13432 if (pat == NULL)
13433 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437 int error = FALSE;
13438
13439 start = get_tv_number_chk(&argvars[2], &error);
13440 if (error)
13441 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013442 if (l != NULL)
13443 {
13444 li = list_find(l, start);
13445 if (li == NULL)
13446 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013447 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013448 }
13449 else
13450 {
13451 if (start < 0)
13452 start = 0;
13453 if (start > (long)STRLEN(str))
13454 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013455 /* When "count" argument is there ignore matches before "start",
13456 * otherwise skip part of the string. Differs when pattern is "^"
13457 * or "\<". */
13458 if (argvars[3].v_type != VAR_UNKNOWN)
13459 startcol = start;
13460 else
13461 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013462 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013464 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465 nth = get_tv_number_chk(&argvars[3], &error);
13466 if (error)
13467 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 }
13469
13470 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13471 if (regmatch.regprog != NULL)
13472 {
13473 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013474
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013475 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013476 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013477 if (l != NULL)
13478 {
13479 if (li == NULL)
13480 {
13481 match = FALSE;
13482 break;
13483 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013484 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013485 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013486 if (str == NULL)
13487 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013488 }
13489
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013490 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013491
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013492 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013493 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013494 if (l == NULL && !match)
13495 break;
13496
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013497 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013498 if (l != NULL)
13499 {
13500 li = li->li_next;
13501 ++idx;
13502 }
13503 else
13504 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013505#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013506 startcol = (colnr_T)(regmatch.startp[0]
13507 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013508#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013509 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013510#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013511 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013512 }
13513
13514 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013516 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013518 int i;
13519
13520 /* return list with matched string and submatches */
13521 for (i = 0; i < NSUBEXP; ++i)
13522 {
13523 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013524 {
13525 if (list_append_string(rettv->vval.v_list,
13526 (char_u *)"", 0) == FAIL)
13527 break;
13528 }
13529 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013530 regmatch.startp[i],
13531 (int)(regmatch.endp[i] - regmatch.startp[i]))
13532 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013533 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013534 }
13535 }
13536 else if (type == 2)
13537 {
13538 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013539 if (l != NULL)
13540 copy_tv(&li->li_tv, rettv);
13541 else
13542 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013544 }
13545 else if (l != NULL)
13546 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 else
13548 {
13549 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 (varnumber_T)(regmatch.startp[0] - str);
13552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013555 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 }
13557 }
13558 vim_free(regmatch.regprog);
13559 }
13560
13561theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 p_cpo = save_cpo;
13564}
13565
13566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013567 * "match()" function
13568 */
13569 static void
13570f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013573{
13574 find_some_match(argvars, rettv, 1);
13575}
13576
13577/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013578 * "matchadd()" function
13579 */
13580 static void
13581f_matchadd(argvars, rettv)
13582 typval_T *argvars;
13583 typval_T *rettv;
13584{
13585#ifdef FEAT_SEARCH_EXTRA
13586 char_u buf[NUMBUFLEN];
13587 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13588 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13589 int prio = 10; /* default priority */
13590 int id = -1;
13591 int error = FALSE;
13592
13593 rettv->vval.v_number = -1;
13594
13595 if (grp == NULL || pat == NULL)
13596 return;
13597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013599 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013600 if (argvars[3].v_type != VAR_UNKNOWN)
13601 id = get_tv_number_chk(&argvars[3], &error);
13602 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013603 if (error == TRUE)
13604 return;
13605 if (id >= 1 && id <= 3)
13606 {
13607 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13608 return;
13609 }
13610
13611 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13612#endif
13613}
13614
13615/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013616 * "matcharg()" function
13617 */
13618 static void
13619f_matcharg(argvars, rettv)
13620 typval_T *argvars;
13621 typval_T *rettv;
13622{
13623 if (rettv_list_alloc(rettv) == OK)
13624 {
13625#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013626 int id = get_tv_number(&argvars[0]);
13627 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013628
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013629 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013630 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013631 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13632 {
13633 list_append_string(rettv->vval.v_list,
13634 syn_id2name(m->hlg_id), -1);
13635 list_append_string(rettv->vval.v_list, m->pattern, -1);
13636 }
13637 else
13638 {
13639 list_append_string(rettv->vval.v_list, NUL, -1);
13640 list_append_string(rettv->vval.v_list, NUL, -1);
13641 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013642 }
13643#endif
13644 }
13645}
13646
13647/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013648 * "matchdelete()" function
13649 */
13650 static void
13651f_matchdelete(argvars, rettv)
13652 typval_T *argvars;
13653 typval_T *rettv;
13654{
13655#ifdef FEAT_SEARCH_EXTRA
13656 rettv->vval.v_number = match_delete(curwin,
13657 (int)get_tv_number(&argvars[0]), TRUE);
13658#endif
13659}
13660
13661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013662 * "matchend()" function
13663 */
13664 static void
13665f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013668{
13669 find_some_match(argvars, rettv, 0);
13670}
13671
13672/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013673 * "matchlist()" function
13674 */
13675 static void
13676f_matchlist(argvars, rettv)
13677 typval_T *argvars;
13678 typval_T *rettv;
13679{
13680 find_some_match(argvars, rettv, 3);
13681}
13682
13683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013684 * "matchstr()" function
13685 */
13686 static void
13687f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690{
13691 find_some_match(argvars, rettv, 2);
13692}
13693
Bram Moolenaar33570922005-01-25 22:26:29 +000013694static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013695
13696 static void
13697max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 typval_T *argvars;
13699 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013700 int domax;
13701{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013702 long n = 0;
13703 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013705
13706 if (argvars[0].v_type == VAR_LIST)
13707 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013708 list_T *l;
13709 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013710
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013711 l = argvars[0].vval.v_list;
13712 if (l != NULL)
13713 {
13714 li = l->lv_first;
13715 if (li != NULL)
13716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013718 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013719 {
13720 li = li->li_next;
13721 if (li == NULL)
13722 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013723 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013724 if (domax ? i > n : i < n)
13725 n = i;
13726 }
13727 }
13728 }
13729 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013730 else if (argvars[0].v_type == VAR_DICT)
13731 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013733 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013735 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013736
13737 d = argvars[0].vval.v_dict;
13738 if (d != NULL)
13739 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013740 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013745 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013747 if (first)
13748 {
13749 n = i;
13750 first = FALSE;
13751 }
13752 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013753 n = i;
13754 }
13755 }
13756 }
13757 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013758 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013759 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013761}
13762
13763/*
13764 * "max()" function
13765 */
13766 static void
13767f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013768 typval_T *argvars;
13769 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013770{
13771 max_min(argvars, rettv, TRUE);
13772}
13773
13774/*
13775 * "min()" function
13776 */
13777 static void
13778f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *argvars;
13780 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013781{
13782 max_min(argvars, rettv, FALSE);
13783}
13784
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013785static int mkdir_recurse __ARGS((char_u *dir, int prot));
13786
13787/*
13788 * Create the directory in which "dir" is located, and higher levels when
13789 * needed.
13790 */
13791 static int
13792mkdir_recurse(dir, prot)
13793 char_u *dir;
13794 int prot;
13795{
13796 char_u *p;
13797 char_u *updir;
13798 int r = FAIL;
13799
13800 /* Get end of directory name in "dir".
13801 * We're done when it's "/" or "c:/". */
13802 p = gettail_sep(dir);
13803 if (p <= get_past_head(dir))
13804 return OK;
13805
13806 /* If the directory exists we're done. Otherwise: create it.*/
13807 updir = vim_strnsave(dir, (int)(p - dir));
13808 if (updir == NULL)
13809 return FAIL;
13810 if (mch_isdir(updir))
13811 r = OK;
13812 else if (mkdir_recurse(updir, prot) == OK)
13813 r = vim_mkdir_emsg(updir, prot);
13814 vim_free(updir);
13815 return r;
13816}
13817
13818#ifdef vim_mkdir
13819/*
13820 * "mkdir()" function
13821 */
13822 static void
13823f_mkdir(argvars, rettv)
13824 typval_T *argvars;
13825 typval_T *rettv;
13826{
13827 char_u *dir;
13828 char_u buf[NUMBUFLEN];
13829 int prot = 0755;
13830
13831 rettv->vval.v_number = FAIL;
13832 if (check_restricted() || check_secure())
13833 return;
13834
13835 dir = get_tv_string_buf(&argvars[0], buf);
13836 if (argvars[1].v_type != VAR_UNKNOWN)
13837 {
13838 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839 prot = get_tv_number_chk(&argvars[2], NULL);
13840 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013841 mkdir_recurse(dir, prot);
13842 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013843 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013844}
13845#endif
13846
Bram Moolenaar0d660222005-01-07 21:51:51 +000013847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 * "mode()" function
13849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 typval_T *argvars;
13853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013855 char_u buf[3];
13856
13857 buf[1] = NUL;
13858 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859
13860#ifdef FEAT_VISUAL
13861 if (VIsual_active)
13862 {
13863 if (VIsual_select)
13864 buf[0] = VIsual_mode + 's' - 'v';
13865 else
13866 buf[0] = VIsual_mode;
13867 }
13868 else
13869#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013870 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13871 || State == CONFIRM)
13872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013874 if (State == ASKMORE)
13875 buf[1] = 'm';
13876 else if (State == CONFIRM)
13877 buf[1] = '?';
13878 }
13879 else if (State == EXTERNCMD)
13880 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 else if (State & INSERT)
13882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013883#ifdef FEAT_VREPLACE
13884 if (State & VREPLACE_FLAG)
13885 {
13886 buf[0] = 'R';
13887 buf[1] = 'v';
13888 }
13889 else
13890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 if (State & REPLACE_FLAG)
13892 buf[0] = 'R';
13893 else
13894 buf[0] = 'i';
13895 }
13896 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013899 if (exmode_active)
13900 buf[1] = 'v';
13901 }
13902 else if (exmode_active)
13903 {
13904 buf[0] = 'c';
13905 buf[1] = 'e';
13906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013910 if (finish_op)
13911 buf[1] = 'o';
13912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013914 /* Clear out the minor mode when the argument is not a non-zero number or
13915 * non-empty string. */
13916 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013917 buf[1] = NUL;
13918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013919 rettv->vval.v_string = vim_strsave(buf);
13920 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921}
13922
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013923#ifdef FEAT_MZSCHEME
13924/*
13925 * "mzeval()" function
13926 */
13927 static void
13928f_mzeval(argvars, rettv)
13929 typval_T *argvars;
13930 typval_T *rettv;
13931{
13932 char_u *str;
13933 char_u buf[NUMBUFLEN];
13934
13935 str = get_tv_string_buf(&argvars[0], buf);
13936 do_mzeval(str, rettv);
13937}
13938#endif
13939
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013941 * "nextnonblank()" function
13942 */
13943 static void
13944f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013945 typval_T *argvars;
13946 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947{
13948 linenr_T lnum;
13949
13950 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 {
13954 lnum = 0;
13955 break;
13956 }
13957 if (*skipwhite(ml_get(lnum)) != NUL)
13958 break;
13959 }
13960 rettv->vval.v_number = lnum;
13961}
13962
13963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964 * "nr2char()" function
13965 */
13966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013968 typval_T *argvars;
13969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970{
13971 char_u buf[NUMBUFLEN];
13972
13973#ifdef FEAT_MBYTE
13974 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 else
13977#endif
13978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 buf[1] = NUL;
13981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982 rettv->v_type = VAR_STRING;
13983 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984}
13985
13986/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013987 * "pathshorten()" function
13988 */
13989 static void
13990f_pathshorten(argvars, rettv)
13991 typval_T *argvars;
13992 typval_T *rettv;
13993{
13994 char_u *p;
13995
13996 rettv->v_type = VAR_STRING;
13997 p = get_tv_string_chk(&argvars[0]);
13998 if (p == NULL)
13999 rettv->vval.v_string = NULL;
14000 else
14001 {
14002 p = vim_strsave(p);
14003 rettv->vval.v_string = p;
14004 if (p != NULL)
14005 shorten_dir(p);
14006 }
14007}
14008
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014009#ifdef FEAT_FLOAT
14010/*
14011 * "pow()" function
14012 */
14013 static void
14014f_pow(argvars, rettv)
14015 typval_T *argvars;
14016 typval_T *rettv;
14017{
14018 float_T fx, fy;
14019
14020 rettv->v_type = VAR_FLOAT;
14021 if (get_float_arg(argvars, &fx) == OK
14022 && get_float_arg(&argvars[1], &fy) == OK)
14023 rettv->vval.v_float = pow(fx, fy);
14024 else
14025 rettv->vval.v_float = 0.0;
14026}
14027#endif
14028
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014030 * "prevnonblank()" function
14031 */
14032 static void
14033f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014034 typval_T *argvars;
14035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014036{
14037 linenr_T lnum;
14038
14039 lnum = get_tv_lnum(argvars);
14040 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14041 lnum = 0;
14042 else
14043 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14044 --lnum;
14045 rettv->vval.v_number = lnum;
14046}
14047
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014048#ifdef HAVE_STDARG_H
14049/* This dummy va_list is here because:
14050 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14051 * - locally in the function results in a "used before set" warning
14052 * - using va_start() to initialize it gives "function with fixed args" error */
14053static va_list ap;
14054#endif
14055
Bram Moolenaar8c711452005-01-14 21:53:12 +000014056/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014057 * "printf()" function
14058 */
14059 static void
14060f_printf(argvars, rettv)
14061 typval_T *argvars;
14062 typval_T *rettv;
14063{
14064 rettv->v_type = VAR_STRING;
14065 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014066#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014067 {
14068 char_u buf[NUMBUFLEN];
14069 int len;
14070 char_u *s;
14071 int saved_did_emsg = did_emsg;
14072 char *fmt;
14073
14074 /* Get the required length, allocate the buffer and do it for real. */
14075 did_emsg = FALSE;
14076 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014077 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014078 if (!did_emsg)
14079 {
14080 s = alloc(len + 1);
14081 if (s != NULL)
14082 {
14083 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014084 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014085 }
14086 }
14087 did_emsg |= saved_did_emsg;
14088 }
14089#endif
14090}
14091
14092/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014093 * "pumvisible()" function
14094 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014095 static void
14096f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014097 typval_T *argvars UNUSED;
14098 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014099{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014100#ifdef FEAT_INS_EXPAND
14101 if (pum_visible())
14102 rettv->vval.v_number = 1;
14103#endif
14104}
14105
14106/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014107 * "range()" function
14108 */
14109 static void
14110f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 typval_T *argvars;
14112 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014113{
14114 long start;
14115 long end;
14116 long stride = 1;
14117 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121 if (argvars[1].v_type == VAR_UNKNOWN)
14122 {
14123 end = start - 1;
14124 start = 0;
14125 }
14126 else
14127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014129 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014131 }
14132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133 if (error)
14134 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014135 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014136 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014137 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014138 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014139 else
14140 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014141 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014142 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014143 if (list_append_number(rettv->vval.v_list,
14144 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014145 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014146 }
14147}
14148
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014149/*
14150 * "readfile()" function
14151 */
14152 static void
14153f_readfile(argvars, rettv)
14154 typval_T *argvars;
14155 typval_T *rettv;
14156{
14157 int binary = FALSE;
14158 char_u *fname;
14159 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014160 listitem_T *li;
14161#define FREAD_SIZE 200 /* optimized for text lines */
14162 char_u buf[FREAD_SIZE];
14163 int readlen; /* size of last fread() */
14164 int buflen; /* nr of valid chars in buf[] */
14165 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14166 int tolist; /* first byte in buf[] still to be put in list */
14167 int chop; /* how many CR to chop off */
14168 char_u *prev = NULL; /* previously read bytes, if any */
14169 int prevlen = 0; /* length of "prev" if not NULL */
14170 char_u *s;
14171 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014172 long maxline = MAXLNUM;
14173 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014174
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014175 if (argvars[1].v_type != VAR_UNKNOWN)
14176 {
14177 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14178 binary = TRUE;
14179 if (argvars[2].v_type != VAR_UNKNOWN)
14180 maxline = get_tv_number(&argvars[2]);
14181 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014183 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014184 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014185
14186 /* Always open the file in binary mode, library functions have a mind of
14187 * their own about CR-LF conversion. */
14188 fname = get_tv_string(&argvars[0]);
14189 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14190 {
14191 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14192 return;
14193 }
14194
14195 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014196 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014197 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014198 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014199 buflen = filtd + readlen;
14200 tolist = 0;
14201 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14202 {
14203 if (buf[filtd] == '\n' || readlen <= 0)
14204 {
14205 /* Only when in binary mode add an empty list item when the
14206 * last line ends in a '\n'. */
14207 if (!binary && readlen == 0 && filtd == 0)
14208 break;
14209
14210 /* Found end-of-line or end-of-file: add a text line to the
14211 * list. */
14212 chop = 0;
14213 if (!binary)
14214 while (filtd - chop - 1 >= tolist
14215 && buf[filtd - chop - 1] == '\r')
14216 ++chop;
14217 len = filtd - tolist - chop;
14218 if (prev == NULL)
14219 s = vim_strnsave(buf + tolist, len);
14220 else
14221 {
14222 s = alloc((unsigned)(prevlen + len + 1));
14223 if (s != NULL)
14224 {
14225 mch_memmove(s, prev, prevlen);
14226 vim_free(prev);
14227 prev = NULL;
14228 mch_memmove(s + prevlen, buf + tolist, len);
14229 s[prevlen + len] = NUL;
14230 }
14231 }
14232 tolist = filtd + 1;
14233
14234 li = listitem_alloc();
14235 if (li == NULL)
14236 {
14237 vim_free(s);
14238 break;
14239 }
14240 li->li_tv.v_type = VAR_STRING;
14241 li->li_tv.v_lock = 0;
14242 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014243 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014244
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014245 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014246 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014247 if (readlen <= 0)
14248 break;
14249 }
14250 else if (buf[filtd] == NUL)
14251 buf[filtd] = '\n';
14252 }
14253 if (readlen <= 0)
14254 break;
14255
14256 if (tolist == 0)
14257 {
14258 /* "buf" is full, need to move text to an allocated buffer */
14259 if (prev == NULL)
14260 {
14261 prev = vim_strnsave(buf, buflen);
14262 prevlen = buflen;
14263 }
14264 else
14265 {
14266 s = alloc((unsigned)(prevlen + buflen));
14267 if (s != NULL)
14268 {
14269 mch_memmove(s, prev, prevlen);
14270 mch_memmove(s + prevlen, buf, buflen);
14271 vim_free(prev);
14272 prev = s;
14273 prevlen += buflen;
14274 }
14275 }
14276 filtd = 0;
14277 }
14278 else
14279 {
14280 mch_memmove(buf, buf + tolist, buflen - tolist);
14281 filtd -= tolist;
14282 }
14283 }
14284
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014285 /*
14286 * For a negative line count use only the lines at the end of the file,
14287 * free the rest.
14288 */
14289 if (maxline < 0)
14290 while (cnt > -maxline)
14291 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014292 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014293 --cnt;
14294 }
14295
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014296 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014297 fclose(fd);
14298}
14299
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014300#if defined(FEAT_RELTIME)
14301static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14302
14303/*
14304 * Convert a List to proftime_T.
14305 * Return FAIL when there is something wrong.
14306 */
14307 static int
14308list2proftime(arg, tm)
14309 typval_T *arg;
14310 proftime_T *tm;
14311{
14312 long n1, n2;
14313 int error = FALSE;
14314
14315 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14316 || arg->vval.v_list->lv_len != 2)
14317 return FAIL;
14318 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14319 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14320# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014321 tm->HighPart = n1;
14322 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014323# else
14324 tm->tv_sec = n1;
14325 tm->tv_usec = n2;
14326# endif
14327 return error ? FAIL : OK;
14328}
14329#endif /* FEAT_RELTIME */
14330
14331/*
14332 * "reltime()" function
14333 */
14334 static void
14335f_reltime(argvars, rettv)
14336 typval_T *argvars;
14337 typval_T *rettv;
14338{
14339#ifdef FEAT_RELTIME
14340 proftime_T res;
14341 proftime_T start;
14342
14343 if (argvars[0].v_type == VAR_UNKNOWN)
14344 {
14345 /* No arguments: get current time. */
14346 profile_start(&res);
14347 }
14348 else if (argvars[1].v_type == VAR_UNKNOWN)
14349 {
14350 if (list2proftime(&argvars[0], &res) == FAIL)
14351 return;
14352 profile_end(&res);
14353 }
14354 else
14355 {
14356 /* Two arguments: compute the difference. */
14357 if (list2proftime(&argvars[0], &start) == FAIL
14358 || list2proftime(&argvars[1], &res) == FAIL)
14359 return;
14360 profile_sub(&res, &start);
14361 }
14362
14363 if (rettv_list_alloc(rettv) == OK)
14364 {
14365 long n1, n2;
14366
14367# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014368 n1 = res.HighPart;
14369 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014370# else
14371 n1 = res.tv_sec;
14372 n2 = res.tv_usec;
14373# endif
14374 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14375 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14376 }
14377#endif
14378}
14379
14380/*
14381 * "reltimestr()" function
14382 */
14383 static void
14384f_reltimestr(argvars, rettv)
14385 typval_T *argvars;
14386 typval_T *rettv;
14387{
14388#ifdef FEAT_RELTIME
14389 proftime_T tm;
14390#endif
14391
14392 rettv->v_type = VAR_STRING;
14393 rettv->vval.v_string = NULL;
14394#ifdef FEAT_RELTIME
14395 if (list2proftime(&argvars[0], &tm) == OK)
14396 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14397#endif
14398}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014399
Bram Moolenaar0d660222005-01-07 21:51:51 +000014400#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14401static void make_connection __ARGS((void));
14402static int check_connection __ARGS((void));
14403
14404 static void
14405make_connection()
14406{
14407 if (X_DISPLAY == NULL
14408# ifdef FEAT_GUI
14409 && !gui.in_use
14410# endif
14411 )
14412 {
14413 x_force_connect = TRUE;
14414 setup_term_clip();
14415 x_force_connect = FALSE;
14416 }
14417}
14418
14419 static int
14420check_connection()
14421{
14422 make_connection();
14423 if (X_DISPLAY == NULL)
14424 {
14425 EMSG(_("E240: No connection to Vim server"));
14426 return FAIL;
14427 }
14428 return OK;
14429}
14430#endif
14431
14432#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014433static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434
14435 static void
14436remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014437 typval_T *argvars;
14438 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439 int expr;
14440{
14441 char_u *server_name;
14442 char_u *keys;
14443 char_u *r = NULL;
14444 char_u buf[NUMBUFLEN];
14445# ifdef WIN32
14446 HWND w;
14447# else
14448 Window w;
14449# endif
14450
14451 if (check_restricted() || check_secure())
14452 return;
14453
14454# ifdef FEAT_X11
14455 if (check_connection() == FAIL)
14456 return;
14457# endif
14458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 server_name = get_tv_string_chk(&argvars[0]);
14460 if (server_name == NULL)
14461 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462 keys = get_tv_string_buf(&argvars[1], buf);
14463# ifdef WIN32
14464 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14465# else
14466 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14467 < 0)
14468# endif
14469 {
14470 if (r != NULL)
14471 EMSG(r); /* sending worked but evaluation failed */
14472 else
14473 EMSG2(_("E241: Unable to send to %s"), server_name);
14474 return;
14475 }
14476
14477 rettv->vval.v_string = r;
14478
14479 if (argvars[2].v_type != VAR_UNKNOWN)
14480 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014481 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014482 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014485 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014486 v.di_tv.v_type = VAR_STRING;
14487 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014488 idvar = get_tv_string_chk(&argvars[2]);
14489 if (idvar != NULL)
14490 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492 }
14493}
14494#endif
14495
14496/*
14497 * "remote_expr()" function
14498 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499 static void
14500f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503{
14504 rettv->v_type = VAR_STRING;
14505 rettv->vval.v_string = NULL;
14506#ifdef FEAT_CLIENTSERVER
14507 remote_common(argvars, rettv, TRUE);
14508#endif
14509}
14510
14511/*
14512 * "remote_foreground()" function
14513 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014514 static void
14515f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014516 typval_T *argvars UNUSED;
14517 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014518{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519#ifdef FEAT_CLIENTSERVER
14520# ifdef WIN32
14521 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 {
14523 char_u *server_name = get_tv_string_chk(&argvars[0]);
14524
14525 if (server_name != NULL)
14526 serverForeground(server_name);
14527 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528# else
14529 /* Send a foreground() expression to the server. */
14530 argvars[1].v_type = VAR_STRING;
14531 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14532 argvars[2].v_type = VAR_UNKNOWN;
14533 remote_common(argvars, rettv, TRUE);
14534 vim_free(argvars[1].vval.v_string);
14535# endif
14536#endif
14537}
14538
Bram Moolenaar0d660222005-01-07 21:51:51 +000014539 static void
14540f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014543{
14544#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546 char_u *s = NULL;
14547# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014548 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551
14552 if (check_restricted() || check_secure())
14553 {
14554 rettv->vval.v_number = -1;
14555 return;
14556 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 serverid = get_tv_string_chk(&argvars[0]);
14558 if (serverid == NULL)
14559 {
14560 rettv->vval.v_number = -1;
14561 return; /* type error; errmsg already given */
14562 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014564 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565 if (n == 0)
14566 rettv->vval.v_number = -1;
14567 else
14568 {
14569 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14570 rettv->vval.v_number = (s != NULL);
14571 }
14572# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 if (check_connection() == FAIL)
14574 return;
14575
14576 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578# endif
14579
14580 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014582 char_u *retvar;
14583
Bram Moolenaar33570922005-01-25 22:26:29 +000014584 v.di_tv.v_type = VAR_STRING;
14585 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 retvar = get_tv_string_chk(&argvars[1]);
14587 if (retvar != NULL)
14588 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590 }
14591#else
14592 rettv->vval.v_number = -1;
14593#endif
14594}
14595
Bram Moolenaar0d660222005-01-07 21:51:51 +000014596 static void
14597f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014598 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600{
14601 char_u *r = NULL;
14602
14603#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014604 char_u *serverid = get_tv_string_chk(&argvars[0]);
14605
14606 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607 {
14608# ifdef WIN32
14609 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014610 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014612 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613 if (n != 0)
14614 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14615 if (r == NULL)
14616# else
14617 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619# endif
14620 EMSG(_("E277: Unable to read a server reply"));
14621 }
14622#endif
14623 rettv->v_type = VAR_STRING;
14624 rettv->vval.v_string = r;
14625}
14626
14627/*
14628 * "remote_send()" function
14629 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630 static void
14631f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014632 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634{
14635 rettv->v_type = VAR_STRING;
14636 rettv->vval.v_string = NULL;
14637#ifdef FEAT_CLIENTSERVER
14638 remote_common(argvars, rettv, FALSE);
14639#endif
14640}
14641
14642/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014643 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014644 */
14645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014646f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014647 typval_T *argvars;
14648 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014649{
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 list_T *l;
14651 listitem_T *item, *item2;
14652 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014653 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014654 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014655 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014656 dict_T *d;
14657 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014658
Bram Moolenaar8c711452005-01-14 21:53:12 +000014659 if (argvars[0].v_type == VAR_DICT)
14660 {
14661 if (argvars[2].v_type != VAR_UNKNOWN)
14662 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014663 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014664 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 key = get_tv_string_chk(&argvars[1]);
14667 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 di = dict_find(d, key, -1);
14670 if (di == NULL)
14671 EMSG2(_(e_dictkey), key);
14672 else
14673 {
14674 *rettv = di->di_tv;
14675 init_tv(&di->di_tv);
14676 dictitem_remove(d, di);
14677 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014679 }
14680 }
14681 else if (argvars[0].v_type != VAR_LIST)
14682 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014683 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014684 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014686 int error = FALSE;
14687
14688 idx = get_tv_number_chk(&argvars[1], &error);
14689 if (error)
14690 ; /* type error: do nothing, errmsg already given */
14691 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014692 EMSGN(_(e_listidx), idx);
14693 else
14694 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014695 if (argvars[2].v_type == VAR_UNKNOWN)
14696 {
14697 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014698 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014699 *rettv = item->li_tv;
14700 vim_free(item);
14701 }
14702 else
14703 {
14704 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 end = get_tv_number_chk(&argvars[2], &error);
14706 if (error)
14707 ; /* type error: do nothing */
14708 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014709 EMSGN(_(e_listidx), end);
14710 else
14711 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014712 int cnt = 0;
14713
14714 for (li = item; li != NULL; li = li->li_next)
14715 {
14716 ++cnt;
14717 if (li == item2)
14718 break;
14719 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014720 if (li == NULL) /* didn't find "item2" after "item" */
14721 EMSG(_(e_invrange));
14722 else
14723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014724 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014725 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014726 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014727 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014728 l->lv_first = item;
14729 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014730 item->li_prev = NULL;
14731 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014732 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014733 }
14734 }
14735 }
14736 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014737 }
14738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739}
14740
14741/*
14742 * "rename({from}, {to})" function
14743 */
14744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 typval_T *argvars;
14747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748{
14749 char_u buf[NUMBUFLEN];
14750
14751 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14755 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756}
14757
14758/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014759 * "repeat()" function
14760 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014761 static void
14762f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014763 typval_T *argvars;
14764 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014765{
14766 char_u *p;
14767 int n;
14768 int slen;
14769 int len;
14770 char_u *r;
14771 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014772
14773 n = get_tv_number(&argvars[1]);
14774 if (argvars[0].v_type == VAR_LIST)
14775 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014776 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014777 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014778 if (list_extend(rettv->vval.v_list,
14779 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014780 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014781 }
14782 else
14783 {
14784 p = get_tv_string(&argvars[0]);
14785 rettv->v_type = VAR_STRING;
14786 rettv->vval.v_string = NULL;
14787
14788 slen = (int)STRLEN(p);
14789 len = slen * n;
14790 if (len <= 0)
14791 return;
14792
14793 r = alloc(len + 1);
14794 if (r != NULL)
14795 {
14796 for (i = 0; i < n; i++)
14797 mch_memmove(r + i * slen, p, (size_t)slen);
14798 r[len] = NUL;
14799 }
14800
14801 rettv->vval.v_string = r;
14802 }
14803}
14804
14805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 * "resolve()" function
14807 */
14808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 typval_T *argvars;
14811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812{
14813 char_u *p;
14814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014815 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816#ifdef FEAT_SHORTCUT
14817 {
14818 char_u *v = NULL;
14819
14820 v = mch_resolve_shortcut(p);
14821 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 }
14826#else
14827# ifdef HAVE_READLINK
14828 {
14829 char_u buf[MAXPATHL + 1];
14830 char_u *cpy;
14831 int len;
14832 char_u *remain = NULL;
14833 char_u *q;
14834 int is_relative_to_current = FALSE;
14835 int has_trailing_pathsep = FALSE;
14836 int limit = 100;
14837
14838 p = vim_strsave(p);
14839
14840 if (p[0] == '.' && (vim_ispathsep(p[1])
14841 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14842 is_relative_to_current = TRUE;
14843
14844 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014845 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846 has_trailing_pathsep = TRUE;
14847
14848 q = getnextcomp(p);
14849 if (*q != NUL)
14850 {
14851 /* Separate the first path component in "p", and keep the
14852 * remainder (beginning with the path separator). */
14853 remain = vim_strsave(q - 1);
14854 q[-1] = NUL;
14855 }
14856
14857 for (;;)
14858 {
14859 for (;;)
14860 {
14861 len = readlink((char *)p, (char *)buf, MAXPATHL);
14862 if (len <= 0)
14863 break;
14864 buf[len] = NUL;
14865
14866 if (limit-- == 0)
14867 {
14868 vim_free(p);
14869 vim_free(remain);
14870 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014871 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 goto fail;
14873 }
14874
14875 /* Ensure that the result will have a trailing path separator
14876 * if the argument has one. */
14877 if (remain == NULL && has_trailing_pathsep)
14878 add_pathsep(buf);
14879
14880 /* Separate the first path component in the link value and
14881 * concatenate the remainders. */
14882 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14883 if (*q != NUL)
14884 {
14885 if (remain == NULL)
14886 remain = vim_strsave(q - 1);
14887 else
14888 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014889 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 if (cpy != NULL)
14891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 vim_free(remain);
14893 remain = cpy;
14894 }
14895 }
14896 q[-1] = NUL;
14897 }
14898
14899 q = gettail(p);
14900 if (q > p && *q == NUL)
14901 {
14902 /* Ignore trailing path separator. */
14903 q[-1] = NUL;
14904 q = gettail(p);
14905 }
14906 if (q > p && !mch_isFullName(buf))
14907 {
14908 /* symlink is relative to directory of argument */
14909 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14910 if (cpy != NULL)
14911 {
14912 STRCPY(cpy, p);
14913 STRCPY(gettail(cpy), buf);
14914 vim_free(p);
14915 p = cpy;
14916 }
14917 }
14918 else
14919 {
14920 vim_free(p);
14921 p = vim_strsave(buf);
14922 }
14923 }
14924
14925 if (remain == NULL)
14926 break;
14927
14928 /* Append the first path component of "remain" to "p". */
14929 q = getnextcomp(remain + 1);
14930 len = q - remain - (*q != NUL);
14931 cpy = vim_strnsave(p, STRLEN(p) + len);
14932 if (cpy != NULL)
14933 {
14934 STRNCAT(cpy, remain, len);
14935 vim_free(p);
14936 p = cpy;
14937 }
14938 /* Shorten "remain". */
14939 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014940 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 else
14942 {
14943 vim_free(remain);
14944 remain = NULL;
14945 }
14946 }
14947
14948 /* If the result is a relative path name, make it explicitly relative to
14949 * the current directory if and only if the argument had this form. */
14950 if (!vim_ispathsep(*p))
14951 {
14952 if (is_relative_to_current
14953 && *p != NUL
14954 && !(p[0] == '.'
14955 && (p[1] == NUL
14956 || vim_ispathsep(p[1])
14957 || (p[1] == '.'
14958 && (p[2] == NUL
14959 || vim_ispathsep(p[2]))))))
14960 {
14961 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014962 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 if (cpy != NULL)
14964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 vim_free(p);
14966 p = cpy;
14967 }
14968 }
14969 else if (!is_relative_to_current)
14970 {
14971 /* Strip leading "./". */
14972 q = p;
14973 while (q[0] == '.' && vim_ispathsep(q[1]))
14974 q += 2;
14975 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014976 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 }
14978 }
14979
14980 /* Ensure that the result will have no trailing path separator
14981 * if the argument had none. But keep "/" or "//". */
14982 if (!has_trailing_pathsep)
14983 {
14984 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014985 if (after_pathsep(p, q))
14986 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 }
14988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014989 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 }
14991# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014992 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993# endif
14994#endif
14995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997
14998#ifdef HAVE_READLINK
14999fail:
15000#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002}
15003
15004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015005 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 */
15007 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015009 typval_T *argvars;
15010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011{
Bram Moolenaar33570922005-01-25 22:26:29 +000015012 list_T *l;
15013 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015 if (argvars[0].v_type != VAR_LIST)
15016 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015017 else if ((l = argvars[0].vval.v_list) != NULL
15018 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019 {
15020 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015021 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015022 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023 while (li != NULL)
15024 {
15025 ni = li->li_prev;
15026 list_append(l, li);
15027 li = ni;
15028 }
15029 rettv->vval.v_list = l;
15030 rettv->v_type = VAR_LIST;
15031 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015032 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034}
15035
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015036#define SP_NOMOVE 0x01 /* don't move cursor */
15037#define SP_REPEAT 0x02 /* repeat to find outer pair */
15038#define SP_RETCOUNT 0x04 /* return matchcount */
15039#define SP_SETPCMARK 0x08 /* set previous context mark */
15040#define SP_START 0x10 /* accept match at start position */
15041#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15042#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015043
Bram Moolenaar33570922005-01-25 22:26:29 +000015044static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045
15046/*
15047 * Get flags for a search function.
15048 * Possibly sets "p_ws".
15049 * Returns BACKWARD, FORWARD or zero (for an error).
15050 */
15051 static int
15052get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015053 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054 int *flagsp;
15055{
15056 int dir = FORWARD;
15057 char_u *flags;
15058 char_u nbuf[NUMBUFLEN];
15059 int mask;
15060
15061 if (varp->v_type != VAR_UNKNOWN)
15062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 flags = get_tv_string_buf_chk(varp, nbuf);
15064 if (flags == NULL)
15065 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 while (*flags != NUL)
15067 {
15068 switch (*flags)
15069 {
15070 case 'b': dir = BACKWARD; break;
15071 case 'w': p_ws = TRUE; break;
15072 case 'W': p_ws = FALSE; break;
15073 default: mask = 0;
15074 if (flagsp != NULL)
15075 switch (*flags)
15076 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015077 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015078 case 'e': mask = SP_END; break;
15079 case 'm': mask = SP_RETCOUNT; break;
15080 case 'n': mask = SP_NOMOVE; break;
15081 case 'p': mask = SP_SUBPAT; break;
15082 case 'r': mask = SP_REPEAT; break;
15083 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 }
15085 if (mask == 0)
15086 {
15087 EMSG2(_(e_invarg2), flags);
15088 dir = 0;
15089 }
15090 else
15091 *flagsp |= mask;
15092 }
15093 if (dir == 0)
15094 break;
15095 ++flags;
15096 }
15097 }
15098 return dir;
15099}
15100
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015102 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015104 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015105search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015107 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015108 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015110 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 char_u *pat;
15112 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015113 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 int save_p_ws = p_ws;
15115 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015116 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015117 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015118 proftime_T tm;
15119#ifdef FEAT_RELTIME
15120 long time_limit = 0;
15121#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015122 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015123 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015126 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015127 if (dir == 0)
15128 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015129 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015130 if (flags & SP_START)
15131 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015132 if (flags & SP_END)
15133 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015134
Bram Moolenaar76929292008-01-06 19:07:36 +000015135 /* Optional arguments: line number to stop searching and timeout. */
15136 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015137 {
15138 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15139 if (lnum_stop < 0)
15140 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015141#ifdef FEAT_RELTIME
15142 if (argvars[3].v_type != VAR_UNKNOWN)
15143 {
15144 time_limit = get_tv_number_chk(&argvars[3], NULL);
15145 if (time_limit < 0)
15146 goto theend;
15147 }
15148#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015149 }
15150
Bram Moolenaar76929292008-01-06 19:07:36 +000015151#ifdef FEAT_RELTIME
15152 /* Set the time limit, if there is one. */
15153 profile_setlimit(time_limit, &tm);
15154#endif
15155
Bram Moolenaar231334e2005-07-25 20:46:57 +000015156 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015157 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015158 * Check to make sure only those flags are set.
15159 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15160 * flags cannot be set. Check for that condition also.
15161 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015162 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015163 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015166 goto theend;
15167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015169 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015170 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015171 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015172 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015174 if (flags & SP_SUBPAT)
15175 retval = subpatnum;
15176 else
15177 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015178 if (flags & SP_SETPCMARK)
15179 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181 if (match_pos != NULL)
15182 {
15183 /* Store the match cursor position */
15184 match_pos->lnum = pos.lnum;
15185 match_pos->col = pos.col + 1;
15186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 /* "/$" will put the cursor after the end of the line, may need to
15188 * correct that here */
15189 check_cursor();
15190 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015191
15192 /* If 'n' flag is used: restore cursor position. */
15193 if (flags & SP_NOMOVE)
15194 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015195 else
15196 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015197theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015199
15200 return retval;
15201}
15202
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015203#ifdef FEAT_FLOAT
15204/*
15205 * "round({float})" function
15206 */
15207 static void
15208f_round(argvars, rettv)
15209 typval_T *argvars;
15210 typval_T *rettv;
15211{
15212 float_T f;
15213
15214 rettv->v_type = VAR_FLOAT;
15215 if (get_float_arg(argvars, &f) == OK)
15216 /* round() is not in C90, use ceil() or floor() instead. */
15217 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15218 else
15219 rettv->vval.v_float = 0.0;
15220}
15221#endif
15222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015223/*
15224 * "search()" function
15225 */
15226 static void
15227f_search(argvars, rettv)
15228 typval_T *argvars;
15229 typval_T *rettv;
15230{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015231 int flags = 0;
15232
15233 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234}
15235
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015237 * "searchdecl()" function
15238 */
15239 static void
15240f_searchdecl(argvars, rettv)
15241 typval_T *argvars;
15242 typval_T *rettv;
15243{
15244 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015245 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015246 int error = FALSE;
15247 char_u *name;
15248
15249 rettv->vval.v_number = 1; /* default: FAIL */
15250
15251 name = get_tv_string_chk(&argvars[0]);
15252 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015253 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015254 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015255 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15256 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15257 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015258 if (!error && name != NULL)
15259 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015260 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015261}
15262
15263/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015264 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015266 static int
15267searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015269 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270{
15271 char_u *spat, *mpat, *epat;
15272 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 int dir;
15275 int flags = 0;
15276 char_u nbuf1[NUMBUFLEN];
15277 char_u nbuf2[NUMBUFLEN];
15278 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015279 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015280 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015281 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015284 spat = get_tv_string_chk(&argvars[0]);
15285 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15286 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15287 if (spat == NULL || mpat == NULL || epat == NULL)
15288 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290 /* Handle the optional fourth argument: flags */
15291 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015292 if (dir == 0)
15293 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015294
15295 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015296 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15297 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015298 if ((flags & (SP_END | SP_SUBPAT)) != 0
15299 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015300 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015301 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015302 goto theend;
15303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015305 /* Using 'r' implies 'W', otherwise it doesn't work. */
15306 if (flags & SP_REPEAT)
15307 p_ws = FALSE;
15308
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015309 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015310 if (argvars[3].v_type == VAR_UNKNOWN
15311 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 skip = (char_u *)"";
15313 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015316 if (argvars[5].v_type != VAR_UNKNOWN)
15317 {
15318 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15319 if (lnum_stop < 0)
15320 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015321#ifdef FEAT_RELTIME
15322 if (argvars[6].v_type != VAR_UNKNOWN)
15323 {
15324 time_limit = get_tv_number_chk(&argvars[6], NULL);
15325 if (time_limit < 0)
15326 goto theend;
15327 }
15328#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 }
15330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015331 if (skip == NULL)
15332 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015334 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015335 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015336
15337theend:
15338 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015339
15340 return retval;
15341}
15342
15343/*
15344 * "searchpair()" function
15345 */
15346 static void
15347f_searchpair(argvars, rettv)
15348 typval_T *argvars;
15349 typval_T *rettv;
15350{
15351 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15352}
15353
15354/*
15355 * "searchpairpos()" function
15356 */
15357 static void
15358f_searchpairpos(argvars, rettv)
15359 typval_T *argvars;
15360 typval_T *rettv;
15361{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015362 pos_T match_pos;
15363 int lnum = 0;
15364 int col = 0;
15365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015366 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015367 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015368
15369 if (searchpair_cmn(argvars, &match_pos) > 0)
15370 {
15371 lnum = match_pos.lnum;
15372 col = match_pos.col;
15373 }
15374
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015375 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15376 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015377}
15378
15379/*
15380 * Search for a start/middle/end thing.
15381 * Used by searchpair(), see its documentation for the details.
15382 * Returns 0 or -1 for no match,
15383 */
15384 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015385do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15386 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015387 char_u *spat; /* start pattern */
15388 char_u *mpat; /* middle pattern */
15389 char_u *epat; /* end pattern */
15390 int dir; /* BACKWARD or FORWARD */
15391 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015392 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015393 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015394 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015395 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015396{
15397 char_u *save_cpo;
15398 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15399 long retval = 0;
15400 pos_T pos;
15401 pos_T firstpos;
15402 pos_T foundpos;
15403 pos_T save_cursor;
15404 pos_T save_pos;
15405 int n;
15406 int r;
15407 int nest = 1;
15408 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015409 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015410 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015411
15412 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15413 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015414 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015415
Bram Moolenaar76929292008-01-06 19:07:36 +000015416#ifdef FEAT_RELTIME
15417 /* Set the time limit, if there is one. */
15418 profile_setlimit(time_limit, &tm);
15419#endif
15420
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015421 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15422 * start/middle/end (pat3, for the top pair). */
15423 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15424 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15425 if (pat2 == NULL || pat3 == NULL)
15426 goto theend;
15427 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15428 if (*mpat == NUL)
15429 STRCPY(pat3, pat2);
15430 else
15431 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15432 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015433 if (flags & SP_START)
15434 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015435
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 save_cursor = curwin->w_cursor;
15437 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015438 clearpos(&firstpos);
15439 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 pat = pat3;
15441 for (;;)
15442 {
15443 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015444 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15446 /* didn't find it or found the first match again: FAIL */
15447 break;
15448
15449 if (firstpos.lnum == 0)
15450 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015451 if (equalpos(pos, foundpos))
15452 {
15453 /* Found the same position again. Can happen with a pattern that
15454 * has "\zs" at the end and searching backwards. Advance one
15455 * character and try again. */
15456 if (dir == BACKWARD)
15457 decl(&pos);
15458 else
15459 incl(&pos);
15460 }
15461 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015463 /* clear the start flag to avoid getting stuck here */
15464 options &= ~SEARCH_START;
15465
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 /* If the skip pattern matches, ignore this match. */
15467 if (*skip != NUL)
15468 {
15469 save_pos = curwin->w_cursor;
15470 curwin->w_cursor = pos;
15471 r = eval_to_bool(skip, &err, NULL, FALSE);
15472 curwin->w_cursor = save_pos;
15473 if (err)
15474 {
15475 /* Evaluating {skip} caused an error, break here. */
15476 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015477 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 break;
15479 }
15480 if (r)
15481 continue;
15482 }
15483
15484 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15485 {
15486 /* Found end when searching backwards or start when searching
15487 * forward: nested pair. */
15488 ++nest;
15489 pat = pat2; /* nested, don't search for middle */
15490 }
15491 else
15492 {
15493 /* Found end when searching forward or start when searching
15494 * backward: end of (nested) pair; or found middle in outer pair. */
15495 if (--nest == 1)
15496 pat = pat3; /* outer level, search for middle */
15497 }
15498
15499 if (nest == 0)
15500 {
15501 /* Found the match: return matchcount or line number. */
15502 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015503 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015505 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015506 if (flags & SP_SETPCMARK)
15507 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 curwin->w_cursor = pos;
15509 if (!(flags & SP_REPEAT))
15510 break;
15511 nest = 1; /* search for next unmatched */
15512 }
15513 }
15514
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015515 if (match_pos != NULL)
15516 {
15517 /* Store the match cursor position */
15518 match_pos->lnum = curwin->w_cursor.lnum;
15519 match_pos->col = curwin->w_cursor.col + 1;
15520 }
15521
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015523 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 curwin->w_cursor = save_cursor;
15525
15526theend:
15527 vim_free(pat2);
15528 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015529 if (p_cpo == empty_option)
15530 p_cpo = save_cpo;
15531 else
15532 /* Darn, evaluating the {skip} expression changed the value. */
15533 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015534
15535 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536}
15537
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015538/*
15539 * "searchpos()" function
15540 */
15541 static void
15542f_searchpos(argvars, rettv)
15543 typval_T *argvars;
15544 typval_T *rettv;
15545{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015546 pos_T match_pos;
15547 int lnum = 0;
15548 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015549 int n;
15550 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015551
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015552 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015553 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015554
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015555 n = search_cmn(argvars, &match_pos, &flags);
15556 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015557 {
15558 lnum = match_pos.lnum;
15559 col = match_pos.col;
15560 }
15561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015562 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15563 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015564 if (flags & SP_SUBPAT)
15565 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015566}
15567
15568
Bram Moolenaar0d660222005-01-07 21:51:51 +000015569 static void
15570f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574#ifdef FEAT_CLIENTSERVER
15575 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015576 char_u *server = get_tv_string_chk(&argvars[0]);
15577 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578
Bram Moolenaar0d660222005-01-07 21:51:51 +000015579 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015580 if (server == NULL || reply == NULL)
15581 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 if (check_restricted() || check_secure())
15583 return;
15584# ifdef FEAT_X11
15585 if (check_connection() == FAIL)
15586 return;
15587# endif
15588
15589 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015591 EMSG(_("E258: Unable to send to client"));
15592 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015594 rettv->vval.v_number = 0;
15595#else
15596 rettv->vval.v_number = -1;
15597#endif
15598}
15599
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 static void
15601f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015602 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015603 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604{
15605 char_u *r = NULL;
15606
15607#ifdef FEAT_CLIENTSERVER
15608# ifdef WIN32
15609 r = serverGetVimNames();
15610# else
15611 make_connection();
15612 if (X_DISPLAY != NULL)
15613 r = serverGetVimNames(X_DISPLAY);
15614# endif
15615#endif
15616 rettv->v_type = VAR_STRING;
15617 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618}
15619
15620/*
15621 * "setbufvar()" function
15622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015624f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015625 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015626 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627{
15628 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 char_u nbuf[NUMBUFLEN];
15633
15634 if (check_restricted() || check_secure())
15635 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15637 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015638 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 varp = &argvars[2];
15640
15641 if (buf != NULL && varname != NULL && varp != NULL)
15642 {
15643 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645
15646 if (*varname == '&')
15647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 long numval;
15649 char_u *strval;
15650 int error = FALSE;
15651
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015653 numval = get_tv_number_chk(varp, &error);
15654 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 if (!error && strval != NULL)
15656 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 }
15658 else
15659 {
15660 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15661 if (bufvarname != NULL)
15662 {
15663 STRCPY(bufvarname, "b:");
15664 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015665 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 vim_free(bufvarname);
15667 }
15668 }
15669
15670 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673}
15674
15675/*
15676 * "setcmdpos()" function
15677 */
15678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 typval_T *argvars;
15681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015683 int pos = (int)get_tv_number(&argvars[0]) - 1;
15684
15685 if (pos >= 0)
15686 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687}
15688
15689/*
15690 * "setline()" function
15691 */
15692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015694 typval_T *argvars;
15695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696{
15697 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015698 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015699 list_T *l = NULL;
15700 listitem_T *li = NULL;
15701 long added = 0;
15702 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015704 lnum = get_tv_lnum(&argvars[0]);
15705 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015707 l = argvars[1].vval.v_list;
15708 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015710 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015711 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015712
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015713 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015714 for (;;)
15715 {
15716 if (l != NULL)
15717 {
15718 /* list argument, get next string */
15719 if (li == NULL)
15720 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015722 li = li->li_next;
15723 }
15724
15725 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015727 break;
15728 if (lnum <= curbuf->b_ml.ml_line_count)
15729 {
15730 /* existing line, replace it */
15731 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15732 {
15733 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015734 if (lnum == curwin->w_cursor.lnum)
15735 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015736 rettv->vval.v_number = 0; /* OK */
15737 }
15738 }
15739 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15740 {
15741 /* lnum is one past the last line, append the line */
15742 ++added;
15743 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15744 rettv->vval.v_number = 0; /* OK */
15745 }
15746
15747 if (l == NULL) /* only one string argument */
15748 break;
15749 ++lnum;
15750 }
15751
15752 if (added > 0)
15753 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754}
15755
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015756static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15757
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015759 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015760 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015761 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015762set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015763 win_T *wp UNUSED;
15764 typval_T *list_arg UNUSED;
15765 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015766 typval_T *rettv;
15767{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015768#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015769 char_u *act;
15770 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015771#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015772
Bram Moolenaar2641f772005-03-25 21:58:17 +000015773 rettv->vval.v_number = -1;
15774
15775#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015776 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015777 EMSG(_(e_listreq));
15778 else
15779 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015780 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015781
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015782 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015783 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015784 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015785 if (act == NULL)
15786 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015787 if (*act == 'a' || *act == 'r')
15788 action = *act;
15789 }
15790
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015791 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015792 rettv->vval.v_number = 0;
15793 }
15794#endif
15795}
15796
15797/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015798 * "setloclist()" function
15799 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015800 static void
15801f_setloclist(argvars, rettv)
15802 typval_T *argvars;
15803 typval_T *rettv;
15804{
15805 win_T *win;
15806
15807 rettv->vval.v_number = -1;
15808
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015809 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015810 if (win != NULL)
15811 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15812}
15813
15814/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015815 * "setmatches()" function
15816 */
15817 static void
15818f_setmatches(argvars, rettv)
15819 typval_T *argvars;
15820 typval_T *rettv;
15821{
15822#ifdef FEAT_SEARCH_EXTRA
15823 list_T *l;
15824 listitem_T *li;
15825 dict_T *d;
15826
15827 rettv->vval.v_number = -1;
15828 if (argvars[0].v_type != VAR_LIST)
15829 {
15830 EMSG(_(e_listreq));
15831 return;
15832 }
15833 if ((l = argvars[0].vval.v_list) != NULL)
15834 {
15835
15836 /* To some extent make sure that we are dealing with a list from
15837 * "getmatches()". */
15838 li = l->lv_first;
15839 while (li != NULL)
15840 {
15841 if (li->li_tv.v_type != VAR_DICT
15842 || (d = li->li_tv.vval.v_dict) == NULL)
15843 {
15844 EMSG(_(e_invarg));
15845 return;
15846 }
15847 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15848 && dict_find(d, (char_u *)"pattern", -1) != NULL
15849 && dict_find(d, (char_u *)"priority", -1) != NULL
15850 && dict_find(d, (char_u *)"id", -1) != NULL))
15851 {
15852 EMSG(_(e_invarg));
15853 return;
15854 }
15855 li = li->li_next;
15856 }
15857
15858 clear_matches(curwin);
15859 li = l->lv_first;
15860 while (li != NULL)
15861 {
15862 d = li->li_tv.vval.v_dict;
15863 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15864 get_dict_string(d, (char_u *)"pattern", FALSE),
15865 (int)get_dict_number(d, (char_u *)"priority"),
15866 (int)get_dict_number(d, (char_u *)"id"));
15867 li = li->li_next;
15868 }
15869 rettv->vval.v_number = 0;
15870 }
15871#endif
15872}
15873
15874/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015875 * "setpos()" function
15876 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015877 static void
15878f_setpos(argvars, rettv)
15879 typval_T *argvars;
15880 typval_T *rettv;
15881{
15882 pos_T pos;
15883 int fnum;
15884 char_u *name;
15885
Bram Moolenaar08250432008-02-13 11:42:46 +000015886 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015887 name = get_tv_string_chk(argvars);
15888 if (name != NULL)
15889 {
15890 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15891 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015892 if (--pos.col < 0)
15893 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015894 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015895 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015896 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015897 if (fnum == curbuf->b_fnum)
15898 {
15899 curwin->w_cursor = pos;
15900 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015901 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015902 }
15903 else
15904 EMSG(_(e_invarg));
15905 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015906 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15907 {
15908 /* set mark */
15909 if (setmark_pos(name[1], &pos, fnum) == OK)
15910 rettv->vval.v_number = 0;
15911 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015912 else
15913 EMSG(_(e_invarg));
15914 }
15915 }
15916}
15917
15918/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015919 * "setqflist()" function
15920 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015921 static void
15922f_setqflist(argvars, rettv)
15923 typval_T *argvars;
15924 typval_T *rettv;
15925{
15926 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15927}
15928
15929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 * "setreg()" function
15931 */
15932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015933f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015934 typval_T *argvars;
15935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936{
15937 int regname;
15938 char_u *strregname;
15939 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015940 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 int append;
15942 char_u yank_type;
15943 long block_len;
15944
15945 block_len = -1;
15946 yank_type = MAUTO;
15947 append = FALSE;
15948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015949 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015950 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015952 if (strregname == NULL)
15953 return; /* type error; errmsg already given */
15954 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 if (regname == 0 || regname == '@')
15956 regname = '"';
15957 else if (regname == '=')
15958 return;
15959
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015960 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015962 stropt = get_tv_string_chk(&argvars[2]);
15963 if (stropt == NULL)
15964 return; /* type error */
15965 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 switch (*stropt)
15967 {
15968 case 'a': case 'A': /* append */
15969 append = TRUE;
15970 break;
15971 case 'v': case 'c': /* character-wise selection */
15972 yank_type = MCHAR;
15973 break;
15974 case 'V': case 'l': /* line-wise selection */
15975 yank_type = MLINE;
15976 break;
15977#ifdef FEAT_VISUAL
15978 case 'b': case Ctrl_V: /* block-wise selection */
15979 yank_type = MBLOCK;
15980 if (VIM_ISDIGIT(stropt[1]))
15981 {
15982 ++stropt;
15983 block_len = getdigits(&stropt) - 1;
15984 --stropt;
15985 }
15986 break;
15987#endif
15988 }
15989 }
15990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015991 strval = get_tv_string_chk(&argvars[1]);
15992 if (strval != NULL)
15993 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015995 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996}
15997
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015998/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015999 * "settabvar()" function
16000 */
16001 static void
16002f_settabvar(argvars, rettv)
16003 typval_T *argvars;
16004 typval_T *rettv;
16005{
16006 tabpage_T *save_curtab;
16007 char_u *varname, *tabvarname;
16008 typval_T *varp;
16009 tabpage_T *tp;
16010
16011 rettv->vval.v_number = 0;
16012
16013 if (check_restricted() || check_secure())
16014 return;
16015
16016 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16017 varname = get_tv_string_chk(&argvars[1]);
16018 varp = &argvars[2];
16019
16020 if (tp != NULL && varname != NULL && varp != NULL)
16021 {
16022 save_curtab = curtab;
16023 goto_tabpage_tp(tp);
16024
16025 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16026 if (tabvarname != NULL)
16027 {
16028 STRCPY(tabvarname, "t:");
16029 STRCPY(tabvarname + 2, varname);
16030 set_var(tabvarname, varp, TRUE);
16031 vim_free(tabvarname);
16032 }
16033
16034 /* Restore current tabpage */
16035 if (valid_tabpage(save_curtab))
16036 goto_tabpage_tp(save_curtab);
16037 }
16038}
16039
16040/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016041 * "settabwinvar()" function
16042 */
16043 static void
16044f_settabwinvar(argvars, rettv)
16045 typval_T *argvars;
16046 typval_T *rettv;
16047{
16048 setwinvar(argvars, rettv, 1);
16049}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050
16051/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016052 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016055f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016056 typval_T *argvars;
16057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016059 setwinvar(argvars, rettv, 0);
16060}
16061
16062/*
16063 * "setwinvar()" and "settabwinvar()" functions
16064 */
16065 static void
16066setwinvar(argvars, rettv, off)
16067 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016068 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016069 int off;
16070{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 win_T *win;
16072#ifdef FEAT_WINDOWS
16073 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016074 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075#endif
16076 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016079 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080
16081 if (check_restricted() || check_secure())
16082 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016083
16084#ifdef FEAT_WINDOWS
16085 if (off == 1)
16086 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16087 else
16088 tp = curtab;
16089#endif
16090 win = find_win_by_nr(&argvars[off], tp);
16091 varname = get_tv_string_chk(&argvars[off + 1]);
16092 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093
16094 if (win != NULL && varname != NULL && varp != NULL)
16095 {
16096#ifdef FEAT_WINDOWS
16097 /* set curwin to be our win, temporarily */
16098 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016099 save_curtab = curtab;
16100 goto_tabpage_tp(tp);
16101 if (!win_valid(win))
16102 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 curwin = win;
16104 curbuf = curwin->w_buffer;
16105#endif
16106
16107 if (*varname == '&')
16108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016109 long numval;
16110 char_u *strval;
16111 int error = FALSE;
16112
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 numval = get_tv_number_chk(varp, &error);
16115 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 if (!error && strval != NULL)
16117 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 }
16119 else
16120 {
16121 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16122 if (winvarname != NULL)
16123 {
16124 STRCPY(winvarname, "w:");
16125 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016126 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 vim_free(winvarname);
16128 }
16129 }
16130
16131#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016132 /* Restore current tabpage and window, if still valid (autocomands can
16133 * make them invalid). */
16134 if (valid_tabpage(save_curtab))
16135 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 if (win_valid(save_curwin))
16137 {
16138 curwin = save_curwin;
16139 curbuf = curwin->w_buffer;
16140 }
16141#endif
16142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143}
16144
16145/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016146 * "shellescape({string})" function
16147 */
16148 static void
16149f_shellescape(argvars, rettv)
16150 typval_T *argvars;
16151 typval_T *rettv;
16152{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016153 rettv->vval.v_string = vim_strsave_shellescape(
16154 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016155 rettv->v_type = VAR_STRING;
16156}
16157
16158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 */
16161 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016163 typval_T *argvars;
16164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 p = get_tv_string(&argvars[0]);
16169 rettv->vval.v_string = vim_strsave(p);
16170 simplify_filename(rettv->vval.v_string); /* simplify in place */
16171 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172}
16173
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016174#ifdef FEAT_FLOAT
16175/*
16176 * "sin()" function
16177 */
16178 static void
16179f_sin(argvars, rettv)
16180 typval_T *argvars;
16181 typval_T *rettv;
16182{
16183 float_T f;
16184
16185 rettv->v_type = VAR_FLOAT;
16186 if (get_float_arg(argvars, &f) == OK)
16187 rettv->vval.v_float = sin(f);
16188 else
16189 rettv->vval.v_float = 0.0;
16190}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016191
16192/*
16193 * "sinh()" function
16194 */
16195 static void
16196f_sinh(argvars, rettv)
16197 typval_T *argvars;
16198 typval_T *rettv;
16199{
16200 float_T f;
16201
16202 rettv->v_type = VAR_FLOAT;
16203 if (get_float_arg(argvars, &f) == OK)
16204 rettv->vval.v_float = sinh(f);
16205 else
16206 rettv->vval.v_float = 0.0;
16207}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016208#endif
16209
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210static int
16211#ifdef __BORLANDC__
16212 _RTLENTRYF
16213#endif
16214 item_compare __ARGS((const void *s1, const void *s2));
16215static int
16216#ifdef __BORLANDC__
16217 _RTLENTRYF
16218#endif
16219 item_compare2 __ARGS((const void *s1, const void *s2));
16220
16221static int item_compare_ic;
16222static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016223static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224#define ITEM_COMPARE_FAIL 999
16225
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 static int
16230#ifdef __BORLANDC__
16231_RTLENTRYF
16232#endif
16233item_compare(s1, s2)
16234 const void *s1;
16235 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016237 char_u *p1, *p2;
16238 char_u *tofree1, *tofree2;
16239 int res;
16240 char_u numbuf1[NUMBUFLEN];
16241 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016243 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16244 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016245 if (p1 == NULL)
16246 p1 = (char_u *)"";
16247 if (p2 == NULL)
16248 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 if (item_compare_ic)
16250 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 res = STRCMP(p1, p2);
16253 vim_free(tofree1);
16254 vim_free(tofree2);
16255 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256}
16257
16258 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016259#ifdef __BORLANDC__
16260_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262item_compare2(s1, s2)
16263 const void *s1;
16264 const void *s2;
16265{
16266 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016268 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016269 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016271 /* shortcut after failure in previous call; compare all items equal */
16272 if (item_compare_func_err)
16273 return 0;
16274
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016275 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16276 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16278 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279
16280 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016281 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016282 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016283 clear_tv(&argv[0]);
16284 clear_tv(&argv[1]);
16285
16286 if (res == FAIL)
16287 res = ITEM_COMPARE_FAIL;
16288 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16290 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016291 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292 clear_tv(&rettv);
16293 return res;
16294}
16295
16296/*
16297 * "sort({list})" function
16298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 typval_T *argvars;
16302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303{
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 list_T *l;
16305 listitem_T *li;
16306 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 long len;
16308 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 if (argvars[0].v_type != VAR_LIST)
16311 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 else
16313 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016315 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 return;
16317 rettv->vval.v_list = l;
16318 rettv->v_type = VAR_LIST;
16319 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 len = list_len(l);
16322 if (len <= 1)
16323 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 item_compare_ic = FALSE;
16326 item_compare_func = NULL;
16327 if (argvars[1].v_type != VAR_UNKNOWN)
16328 {
16329 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016330 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331 else
16332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 int error = FALSE;
16334
16335 i = get_tv_number_chk(&argvars[1], &error);
16336 if (error)
16337 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338 if (i == 1)
16339 item_compare_ic = TRUE;
16340 else
16341 item_compare_func = get_tv_string(&argvars[1]);
16342 }
16343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 if (ptrs == NULL)
16348 return;
16349 i = 0;
16350 for (li = l->lv_first; li != NULL; li = li->li_next)
16351 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016353 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 /* test the compare function */
16355 if (item_compare_func != NULL
16356 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16357 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016358 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016360 {
16361 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 item_compare_func == NULL ? item_compare : item_compare2);
16364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 if (!item_compare_func_err)
16366 {
16367 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016368 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016369 l->lv_len = 0;
16370 for (i = 0; i < len; ++i)
16371 list_append(l, ptrs[i]);
16372 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373 }
16374
16375 vim_free(ptrs);
16376 }
16377}
16378
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016379/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016380 * "soundfold({word})" function
16381 */
16382 static void
16383f_soundfold(argvars, rettv)
16384 typval_T *argvars;
16385 typval_T *rettv;
16386{
16387 char_u *s;
16388
16389 rettv->v_type = VAR_STRING;
16390 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016391#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016392 rettv->vval.v_string = eval_soundfold(s);
16393#else
16394 rettv->vval.v_string = vim_strsave(s);
16395#endif
16396}
16397
16398/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016399 * "spellbadword()" function
16400 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016401 static void
16402f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016403 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016404 typval_T *rettv;
16405{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016406 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016407 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016408 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016409
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016410 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016411 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016412
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016413#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016414 if (argvars[0].v_type == VAR_UNKNOWN)
16415 {
16416 /* Find the start and length of the badly spelled word. */
16417 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16418 if (len != 0)
16419 word = ml_get_cursor();
16420 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016421 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016422 {
16423 char_u *str = get_tv_string_chk(&argvars[0]);
16424 int capcol = -1;
16425
16426 if (str != NULL)
16427 {
16428 /* Check the argument for spelling. */
16429 while (*str != NUL)
16430 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016431 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016432 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016433 {
16434 word = str;
16435 break;
16436 }
16437 str += len;
16438 }
16439 }
16440 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016441#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016442
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016443 list_append_string(rettv->vval.v_list, word, len);
16444 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016445 attr == HLF_SPB ? "bad" :
16446 attr == HLF_SPR ? "rare" :
16447 attr == HLF_SPL ? "local" :
16448 attr == HLF_SPC ? "caps" :
16449 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016450}
16451
16452/*
16453 * "spellsuggest()" function
16454 */
16455 static void
16456f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016457 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016458 typval_T *rettv;
16459{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016460#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016461 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016462 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016463 int maxcount;
16464 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016465 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016466 listitem_T *li;
16467 int need_capital = FALSE;
16468#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016469
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016470 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016471 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016472
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016473#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016474 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016475 {
16476 str = get_tv_string(&argvars[0]);
16477 if (argvars[1].v_type != VAR_UNKNOWN)
16478 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016479 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016480 if (maxcount <= 0)
16481 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016482 if (argvars[2].v_type != VAR_UNKNOWN)
16483 {
16484 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16485 if (typeerr)
16486 return;
16487 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016488 }
16489 else
16490 maxcount = 25;
16491
Bram Moolenaar4770d092006-01-12 23:22:24 +000016492 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016493
16494 for (i = 0; i < ga.ga_len; ++i)
16495 {
16496 str = ((char_u **)ga.ga_data)[i];
16497
16498 li = listitem_alloc();
16499 if (li == NULL)
16500 vim_free(str);
16501 else
16502 {
16503 li->li_tv.v_type = VAR_STRING;
16504 li->li_tv.v_lock = 0;
16505 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016506 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016507 }
16508 }
16509 ga_clear(&ga);
16510 }
16511#endif
16512}
16513
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016515f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016516 typval_T *argvars;
16517 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518{
16519 char_u *str;
16520 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016521 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 regmatch_T regmatch;
16523 char_u patbuf[NUMBUFLEN];
16524 char_u *save_cpo;
16525 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016526 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016527 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529
16530 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16531 save_cpo = p_cpo;
16532 p_cpo = (char_u *)"";
16533
16534 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016535 if (argvars[1].v_type != VAR_UNKNOWN)
16536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016537 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16538 if (pat == NULL)
16539 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016542 }
16543 if (pat == NULL || *pat == NUL)
16544 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016546 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 if (typeerr)
16549 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaar0d660222005-01-07 21:51:51 +000016551 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16552 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016555 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016557 if (*str == NUL)
16558 match = FALSE; /* empty item at the end */
16559 else
16560 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 if (match)
16562 end = regmatch.startp[0];
16563 else
16564 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016565 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16566 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016567 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016568 if (list_append_string(rettv->vval.v_list, str,
16569 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016570 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571 }
16572 if (!match)
16573 break;
16574 /* Advance to just after the match. */
16575 if (regmatch.endp[0] > str)
16576 col = 0;
16577 else
16578 {
16579 /* Don't get stuck at the same match. */
16580#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016581 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016582#else
16583 col = 1;
16584#endif
16585 }
16586 str = regmatch.endp[0];
16587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588
Bram Moolenaar0d660222005-01-07 21:51:51 +000016589 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593}
16594
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016595#ifdef FEAT_FLOAT
16596/*
16597 * "sqrt()" function
16598 */
16599 static void
16600f_sqrt(argvars, rettv)
16601 typval_T *argvars;
16602 typval_T *rettv;
16603{
16604 float_T f;
16605
16606 rettv->v_type = VAR_FLOAT;
16607 if (get_float_arg(argvars, &f) == OK)
16608 rettv->vval.v_float = sqrt(f);
16609 else
16610 rettv->vval.v_float = 0.0;
16611}
16612
16613/*
16614 * "str2float()" function
16615 */
16616 static void
16617f_str2float(argvars, rettv)
16618 typval_T *argvars;
16619 typval_T *rettv;
16620{
16621 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16622
16623 if (*p == '+')
16624 p = skipwhite(p + 1);
16625 (void)string2float(p, &rettv->vval.v_float);
16626 rettv->v_type = VAR_FLOAT;
16627}
16628#endif
16629
Bram Moolenaar2c932302006-03-18 21:42:09 +000016630/*
16631 * "str2nr()" function
16632 */
16633 static void
16634f_str2nr(argvars, rettv)
16635 typval_T *argvars;
16636 typval_T *rettv;
16637{
16638 int base = 10;
16639 char_u *p;
16640 long n;
16641
16642 if (argvars[1].v_type != VAR_UNKNOWN)
16643 {
16644 base = get_tv_number(&argvars[1]);
16645 if (base != 8 && base != 10 && base != 16)
16646 {
16647 EMSG(_(e_invarg));
16648 return;
16649 }
16650 }
16651
16652 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016653 if (*p == '+')
16654 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016655 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16656 rettv->vval.v_number = n;
16657}
16658
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659#ifdef HAVE_STRFTIME
16660/*
16661 * "strftime({format}[, {time}])" function
16662 */
16663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016664f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016665 typval_T *argvars;
16666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667{
16668 char_u result_buf[256];
16669 struct tm *curtime;
16670 time_t seconds;
16671 char_u *p;
16672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016676 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 seconds = time(NULL);
16678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 curtime = localtime(&seconds);
16681 /* MSVC returns NULL for an invalid value of seconds. */
16682 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016683 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 else
16685 {
16686# ifdef FEAT_MBYTE
16687 vimconv_T conv;
16688 char_u *enc;
16689
16690 conv.vc_type = CONV_NONE;
16691 enc = enc_locale();
16692 convert_setup(&conv, p_enc, enc);
16693 if (conv.vc_type != CONV_NONE)
16694 p = string_convert(&conv, p, NULL);
16695# endif
16696 if (p != NULL)
16697 (void)strftime((char *)result_buf, sizeof(result_buf),
16698 (char *)p, curtime);
16699 else
16700 result_buf[0] = NUL;
16701
16702# ifdef FEAT_MBYTE
16703 if (conv.vc_type != CONV_NONE)
16704 vim_free(p);
16705 convert_setup(&conv, enc, p_enc);
16706 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016707 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 else
16709# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016710 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711
16712# ifdef FEAT_MBYTE
16713 /* Release conversion descriptors */
16714 convert_setup(&conv, NULL, NULL);
16715 vim_free(enc);
16716# endif
16717 }
16718}
16719#endif
16720
16721/*
16722 * "stridx()" function
16723 */
16724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 typval_T *argvars;
16727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
16729 char_u buf[NUMBUFLEN];
16730 char_u *needle;
16731 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016732 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016734 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 needle = get_tv_string_chk(&argvars[1]);
16737 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016738 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016739 if (needle == NULL || haystack == NULL)
16740 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741
Bram Moolenaar33570922005-01-25 22:26:29 +000016742 if (argvars[2].v_type != VAR_UNKNOWN)
16743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016744 int error = FALSE;
16745
16746 start_idx = get_tv_number_chk(&argvars[2], &error);
16747 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016748 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016749 if (start_idx >= 0)
16750 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 }
16752
16753 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16754 if (pos != NULL)
16755 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756}
16757
16758/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016759 * "string()" function
16760 */
16761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 typval_T *argvars;
16764 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016765{
16766 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016767 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016769 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016770 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016771 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016772 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016773 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774}
16775
16776/*
16777 * "strlen()" function
16778 */
16779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016780f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 typval_T *argvars;
16782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016784 rettv->vval.v_number = (varnumber_T)(STRLEN(
16785 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786}
16787
16788/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016789 * "strchars()" function
16790 */
16791 static void
16792f_strchars(argvars, rettv)
16793 typval_T *argvars;
16794 typval_T *rettv;
16795{
16796 char_u *s = get_tv_string(&argvars[0]);
16797#ifdef FEAT_MBYTE
16798 varnumber_T len = 0;
16799
16800 while (*s != NUL)
16801 {
16802 mb_cptr2char_adv(&s);
16803 ++len;
16804 }
16805 rettv->vval.v_number = len;
16806#else
16807 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16808#endif
16809}
16810
16811/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016812 * "strdisplaywidth()" function
16813 */
16814 static void
16815f_strdisplaywidth(argvars, rettv)
16816 typval_T *argvars;
16817 typval_T *rettv;
16818{
16819 char_u *s = get_tv_string(&argvars[0]);
16820 int col = 0;
16821
16822 if (argvars[1].v_type != VAR_UNKNOWN)
16823 col = get_tv_number(&argvars[1]);
16824
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016825 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016826}
16827
16828/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016829 * "strwidth()" function
16830 */
16831 static void
16832f_strwidth(argvars, rettv)
16833 typval_T *argvars;
16834 typval_T *rettv;
16835{
16836 char_u *s = get_tv_string(&argvars[0]);
16837
16838 rettv->vval.v_number = (varnumber_T)(
16839#ifdef FEAT_MBYTE
16840 mb_string2cells(s, -1)
16841#else
16842 STRLEN(s)
16843#endif
16844 );
16845}
16846
16847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 * "strpart()" function
16849 */
16850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016852 typval_T *argvars;
16853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854{
16855 char_u *p;
16856 int n;
16857 int len;
16858 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016859 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016861 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 slen = (int)STRLEN(p);
16863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016864 n = get_tv_number_chk(&argvars[1], &error);
16865 if (error)
16866 len = 0;
16867 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016868 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 else
16870 len = slen - n; /* default len: all bytes that are available. */
16871
16872 /*
16873 * Only return the overlap between the specified part and the actual
16874 * string.
16875 */
16876 if (n < 0)
16877 {
16878 len += n;
16879 n = 0;
16880 }
16881 else if (n > slen)
16882 n = slen;
16883 if (len < 0)
16884 len = 0;
16885 else if (n + len > slen)
16886 len = slen - n;
16887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016888 rettv->v_type = VAR_STRING;
16889 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890}
16891
16892/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893 * "strridx()" function
16894 */
16895 static void
16896f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 typval_T *argvars;
16898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899{
16900 char_u buf[NUMBUFLEN];
16901 char_u *needle;
16902 char_u *haystack;
16903 char_u *rest;
16904 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016905 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907 needle = get_tv_string_chk(&argvars[1]);
16908 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016909
16910 rettv->vval.v_number = -1;
16911 if (needle == NULL || haystack == NULL)
16912 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016913
16914 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016915 if (argvars[2].v_type != VAR_UNKNOWN)
16916 {
16917 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016918 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016919 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016920 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016921 }
16922 else
16923 end_idx = haystack_len;
16924
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016926 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016928 lastmatch = haystack + end_idx;
16929 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016931 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 for (rest = haystack; *rest != '\0'; ++rest)
16933 {
16934 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016935 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 break;
16937 lastmatch = rest;
16938 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016939 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940
16941 if (lastmatch == NULL)
16942 rettv->vval.v_number = -1;
16943 else
16944 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16945}
16946
16947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 * "strtrans()" function
16949 */
16950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016951f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 typval_T *argvars;
16953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016955 rettv->v_type = VAR_STRING;
16956 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957}
16958
16959/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 * "submatch()" function
16961 */
16962 static void
16963f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016964 typval_T *argvars;
16965 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966{
16967 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 rettv->vval.v_string =
16969 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970}
16971
16972/*
16973 * "substitute()" function
16974 */
16975 static void
16976f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 typval_T *argvars;
16978 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979{
16980 char_u patbuf[NUMBUFLEN];
16981 char_u subbuf[NUMBUFLEN];
16982 char_u flagsbuf[NUMBUFLEN];
16983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 char_u *str = get_tv_string_chk(&argvars[0]);
16985 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16986 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16987 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16988
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16991 rettv->vval.v_string = NULL;
16992 else
16993 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994}
16995
16996/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016997 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017000f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017001 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003{
17004 int id = 0;
17005#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017006 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 long col;
17008 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017009 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011 lnum = get_tv_lnum(argvars); /* -1 on type error */
17012 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17013 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017016 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017017 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018#endif
17019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021}
17022
17023/*
17024 * "synIDattr(id, what [, mode])" function
17025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017027f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030{
17031 char_u *p = NULL;
17032#ifdef FEAT_SYN_HL
17033 int id;
17034 char_u *what;
17035 char_u *mode;
17036 char_u modebuf[NUMBUFLEN];
17037 int modec;
17038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017039 id = get_tv_number(&argvars[0]);
17040 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017041 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017045 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 modec = 0; /* replace invalid with current */
17047 }
17048 else
17049 {
17050#ifdef FEAT_GUI
17051 if (gui.in_use)
17052 modec = 'g';
17053 else
17054#endif
17055 if (t_colors > 1)
17056 modec = 'c';
17057 else
17058 modec = 't';
17059 }
17060
17061
17062 switch (TOLOWER_ASC(what[0]))
17063 {
17064 case 'b':
17065 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17066 p = highlight_color(id, what, modec);
17067 else /* bold */
17068 p = highlight_has_attr(id, HL_BOLD, modec);
17069 break;
17070
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017071 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 p = highlight_color(id, what, modec);
17073 break;
17074
17075 case 'i':
17076 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17077 p = highlight_has_attr(id, HL_INVERSE, modec);
17078 else /* italic */
17079 p = highlight_has_attr(id, HL_ITALIC, modec);
17080 break;
17081
17082 case 'n': /* name */
17083 p = get_highlight_name(NULL, id - 1);
17084 break;
17085
17086 case 'r': /* reverse */
17087 p = highlight_has_attr(id, HL_INVERSE, modec);
17088 break;
17089
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017090 case 's':
17091 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17092 p = highlight_color(id, what, modec);
17093 else /* standout */
17094 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 break;
17096
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017097 case 'u':
17098 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17099 /* underline */
17100 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17101 else
17102 /* undercurl */
17103 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 break;
17105 }
17106
17107 if (p != NULL)
17108 p = vim_strsave(p);
17109#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017110 rettv->v_type = VAR_STRING;
17111 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112}
17113
17114/*
17115 * "synIDtrans(id)" function
17116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017119 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121{
17122 int id;
17123
17124#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126
17127 if (id > 0)
17128 id = syn_get_final_id(id);
17129 else
17130#endif
17131 id = 0;
17132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134}
17135
17136/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017137 * "synconcealed(lnum, col)" function
17138 */
17139 static void
17140f_synconcealed(argvars, rettv)
17141 typval_T *argvars UNUSED;
17142 typval_T *rettv;
17143{
17144#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17145 long lnum;
17146 long col;
17147 int syntax_flags = 0;
17148 int cchar;
17149 int matchid = 0;
17150 char_u str[NUMBUFLEN];
17151#endif
17152
17153 rettv->v_type = VAR_LIST;
17154 rettv->vval.v_list = NULL;
17155
17156#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17157 lnum = get_tv_lnum(argvars); /* -1 on type error */
17158 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17159
17160 vim_memset(str, NUL, sizeof(str));
17161
17162 if (rettv_list_alloc(rettv) != FAIL)
17163 {
17164 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17165 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17166 && curwin->w_p_cole > 0)
17167 {
17168 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17169 syntax_flags = get_syntax_info(&matchid);
17170
17171 /* get the conceal character */
17172 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17173 {
17174 cchar = syn_get_sub_char();
17175 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17176 cchar = lcs_conceal;
17177 if (cchar != NUL)
17178 {
17179# ifdef FEAT_MBYTE
17180 if (has_mbyte)
17181 (*mb_char2bytes)(cchar, str);
17182 else
17183# endif
17184 str[0] = cchar;
17185 }
17186 }
17187 }
17188
17189 list_append_number(rettv->vval.v_list,
17190 (syntax_flags & HL_CONCEAL) != 0);
17191 /* -1 to auto-determine strlen */
17192 list_append_string(rettv->vval.v_list, str, -1);
17193 list_append_number(rettv->vval.v_list, matchid);
17194 }
17195#endif
17196}
17197
17198/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017199 * "synstack(lnum, col)" function
17200 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017201 static void
17202f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017203 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017204 typval_T *rettv;
17205{
17206#ifdef FEAT_SYN_HL
17207 long lnum;
17208 long col;
17209 int i;
17210 int id;
17211#endif
17212
17213 rettv->v_type = VAR_LIST;
17214 rettv->vval.v_list = NULL;
17215
17216#ifdef FEAT_SYN_HL
17217 lnum = get_tv_lnum(argvars); /* -1 on type error */
17218 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17219
17220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017221 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017222 && rettv_list_alloc(rettv) != FAIL)
17223 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017224 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017225 for (i = 0; ; ++i)
17226 {
17227 id = syn_get_stack_item(i);
17228 if (id < 0)
17229 break;
17230 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17231 break;
17232 }
17233 }
17234#endif
17235}
17236
17237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 * "system()" function
17239 */
17240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017242 typval_T *argvars;
17243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017245 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017247 char_u *infile = NULL;
17248 char_u buf[NUMBUFLEN];
17249 int err = FALSE;
17250 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017252 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017253 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017254
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017256 {
17257 /*
17258 * Write the string to a temp file, to be used for input of the shell
17259 * command.
17260 */
17261 if ((infile = vim_tempname('i')) == NULL)
17262 {
17263 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017264 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017265 }
17266
17267 fd = mch_fopen((char *)infile, WRITEBIN);
17268 if (fd == NULL)
17269 {
17270 EMSG2(_(e_notopen), infile);
17271 goto done;
17272 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017273 p = get_tv_string_buf_chk(&argvars[1], buf);
17274 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017275 {
17276 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017277 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017278 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017279 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17280 err = TRUE;
17281 if (fclose(fd) != 0)
17282 err = TRUE;
17283 if (err)
17284 {
17285 EMSG(_("E677: Error writing temp file"));
17286 goto done;
17287 }
17288 }
17289
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017290 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17291 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017292
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293#ifdef USE_CR
17294 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017295 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 {
17297 char_u *s;
17298
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017299 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 {
17301 if (*s == CAR)
17302 *s = NL;
17303 }
17304 }
17305#else
17306# ifdef USE_CRNL
17307 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017308 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 {
17310 char_u *s, *d;
17311
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017312 d = res;
17313 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 {
17315 if (s[0] == CAR && s[1] == NL)
17316 ++s;
17317 *d++ = *s;
17318 }
17319 *d = NUL;
17320 }
17321# endif
17322#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017323
17324done:
17325 if (infile != NULL)
17326 {
17327 mch_remove(infile);
17328 vim_free(infile);
17329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017330 rettv->v_type = VAR_STRING;
17331 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332}
17333
17334/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017335 * "tabpagebuflist()" function
17336 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017337 static void
17338f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017339 typval_T *argvars UNUSED;
17340 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017341{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017342#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017343 tabpage_T *tp;
17344 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017345
17346 if (argvars[0].v_type == VAR_UNKNOWN)
17347 wp = firstwin;
17348 else
17349 {
17350 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17351 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017352 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017353 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017354 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017355 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017356 for (; wp != NULL; wp = wp->w_next)
17357 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017358 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017359 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017360 }
17361#endif
17362}
17363
17364
17365/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017366 * "tabpagenr()" function
17367 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017368 static void
17369f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017370 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017371 typval_T *rettv;
17372{
17373 int nr = 1;
17374#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017375 char_u *arg;
17376
17377 if (argvars[0].v_type != VAR_UNKNOWN)
17378 {
17379 arg = get_tv_string_chk(&argvars[0]);
17380 nr = 0;
17381 if (arg != NULL)
17382 {
17383 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017384 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017385 else
17386 EMSG2(_(e_invexpr2), arg);
17387 }
17388 }
17389 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017390 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017391#endif
17392 rettv->vval.v_number = nr;
17393}
17394
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017395
17396#ifdef FEAT_WINDOWS
17397static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17398
17399/*
17400 * Common code for tabpagewinnr() and winnr().
17401 */
17402 static int
17403get_winnr(tp, argvar)
17404 tabpage_T *tp;
17405 typval_T *argvar;
17406{
17407 win_T *twin;
17408 int nr = 1;
17409 win_T *wp;
17410 char_u *arg;
17411
17412 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17413 if (argvar->v_type != VAR_UNKNOWN)
17414 {
17415 arg = get_tv_string_chk(argvar);
17416 if (arg == NULL)
17417 nr = 0; /* type error; errmsg already given */
17418 else if (STRCMP(arg, "$") == 0)
17419 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17420 else if (STRCMP(arg, "#") == 0)
17421 {
17422 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17423 if (twin == NULL)
17424 nr = 0;
17425 }
17426 else
17427 {
17428 EMSG2(_(e_invexpr2), arg);
17429 nr = 0;
17430 }
17431 }
17432
17433 if (nr > 0)
17434 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17435 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017436 {
17437 if (wp == NULL)
17438 {
17439 /* didn't find it in this tabpage */
17440 nr = 0;
17441 break;
17442 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017443 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017444 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017445 return nr;
17446}
17447#endif
17448
17449/*
17450 * "tabpagewinnr()" function
17451 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017452 static void
17453f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017454 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017455 typval_T *rettv;
17456{
17457 int nr = 1;
17458#ifdef FEAT_WINDOWS
17459 tabpage_T *tp;
17460
17461 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17462 if (tp == NULL)
17463 nr = 0;
17464 else
17465 nr = get_winnr(tp, &argvars[1]);
17466#endif
17467 rettv->vval.v_number = nr;
17468}
17469
17470
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017471/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017472 * "tagfiles()" function
17473 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017474 static void
17475f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017476 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017477 typval_T *rettv;
17478{
17479 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017480 tagname_T tn;
17481 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017483 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017484 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017486 for (first = TRUE; ; first = FALSE)
17487 if (get_tagfname(&tn, first, fname) == FAIL
17488 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017489 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017490 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017491}
17492
17493/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017494 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017495 */
17496 static void
17497f_taglist(argvars, rettv)
17498 typval_T *argvars;
17499 typval_T *rettv;
17500{
17501 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017502
17503 tag_pattern = get_tv_string(&argvars[0]);
17504
17505 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017506 if (*tag_pattern == NUL)
17507 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017508
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017509 if (rettv_list_alloc(rettv) == OK)
17510 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017511}
17512
17513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 * "tempname()" function
17515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017517f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017518 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520{
17521 static int x = 'A';
17522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->v_type = VAR_STRING;
17524 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525
17526 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17527 * names. Skip 'I' and 'O', they are used for shell redirection. */
17528 do
17529 {
17530 if (x == 'Z')
17531 x = '0';
17532 else if (x == '9')
17533 x = 'A';
17534 else
17535 {
17536#ifdef EBCDIC
17537 if (x == 'I')
17538 x = 'J';
17539 else if (x == 'R')
17540 x = 'S';
17541 else
17542#endif
17543 ++x;
17544 }
17545 } while (x == 'I' || x == 'O');
17546}
17547
17548/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017549 * "test(list)" function: Just checking the walls...
17550 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017551 static void
17552f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017553 typval_T *argvars UNUSED;
17554 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017555{
17556 /* Used for unit testing. Change the code below to your liking. */
17557#if 0
17558 listitem_T *li;
17559 list_T *l;
17560 char_u *bad, *good;
17561
17562 if (argvars[0].v_type != VAR_LIST)
17563 return;
17564 l = argvars[0].vval.v_list;
17565 if (l == NULL)
17566 return;
17567 li = l->lv_first;
17568 if (li == NULL)
17569 return;
17570 bad = get_tv_string(&li->li_tv);
17571 li = li->li_next;
17572 if (li == NULL)
17573 return;
17574 good = get_tv_string(&li->li_tv);
17575 rettv->vval.v_number = test_edit_score(bad, good);
17576#endif
17577}
17578
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017579#ifdef FEAT_FLOAT
17580/*
17581 * "tan()" function
17582 */
17583 static void
17584f_tan(argvars, rettv)
17585 typval_T *argvars;
17586 typval_T *rettv;
17587{
17588 float_T f;
17589
17590 rettv->v_type = VAR_FLOAT;
17591 if (get_float_arg(argvars, &f) == OK)
17592 rettv->vval.v_float = tan(f);
17593 else
17594 rettv->vval.v_float = 0.0;
17595}
17596
17597/*
17598 * "tanh()" function
17599 */
17600 static void
17601f_tanh(argvars, rettv)
17602 typval_T *argvars;
17603 typval_T *rettv;
17604{
17605 float_T f;
17606
17607 rettv->v_type = VAR_FLOAT;
17608 if (get_float_arg(argvars, &f) == OK)
17609 rettv->vval.v_float = tanh(f);
17610 else
17611 rettv->vval.v_float = 0.0;
17612}
17613#endif
17614
Bram Moolenaard52d9742005-08-21 22:20:28 +000017615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 * "tolower(string)" function
17617 */
17618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017619f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017620 typval_T *argvars;
17621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622{
17623 char_u *p;
17624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625 p = vim_strsave(get_tv_string(&argvars[0]));
17626 rettv->v_type = VAR_STRING;
17627 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
17629 if (p != NULL)
17630 while (*p != NUL)
17631 {
17632#ifdef FEAT_MBYTE
17633 int l;
17634
17635 if (enc_utf8)
17636 {
17637 int c, lc;
17638
17639 c = utf_ptr2char(p);
17640 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017641 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 /* TODO: reallocate string when byte count changes. */
17643 if (utf_char2len(lc) == l)
17644 utf_char2bytes(lc, p);
17645 p += l;
17646 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017647 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 p += l; /* skip multi-byte character */
17649 else
17650#endif
17651 {
17652 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17653 ++p;
17654 }
17655 }
17656}
17657
17658/*
17659 * "toupper(string)" function
17660 */
17661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017663 typval_T *argvars;
17664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017667 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668}
17669
17670/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017671 * "tr(string, fromstr, tostr)" function
17672 */
17673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017675 typval_T *argvars;
17676 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017677{
17678 char_u *instr;
17679 char_u *fromstr;
17680 char_u *tostr;
17681 char_u *p;
17682#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017683 int inlen;
17684 int fromlen;
17685 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017686 int idx;
17687 char_u *cpstr;
17688 int cplen;
17689 int first = TRUE;
17690#endif
17691 char_u buf[NUMBUFLEN];
17692 char_u buf2[NUMBUFLEN];
17693 garray_T ga;
17694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017696 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17697 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017698
17699 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700 rettv->v_type = VAR_STRING;
17701 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017702 if (fromstr == NULL || tostr == NULL)
17703 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017704 ga_init2(&ga, (int)sizeof(char), 80);
17705
17706#ifdef FEAT_MBYTE
17707 if (!has_mbyte)
17708#endif
17709 /* not multi-byte: fromstr and tostr must be the same length */
17710 if (STRLEN(fromstr) != STRLEN(tostr))
17711 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017712#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017713error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017714#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017715 EMSG2(_(e_invarg2), fromstr);
17716 ga_clear(&ga);
17717 return;
17718 }
17719
17720 /* fromstr and tostr have to contain the same number of chars */
17721 while (*instr != NUL)
17722 {
17723#ifdef FEAT_MBYTE
17724 if (has_mbyte)
17725 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017726 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017727 cpstr = instr;
17728 cplen = inlen;
17729 idx = 0;
17730 for (p = fromstr; *p != NUL; p += fromlen)
17731 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017732 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017733 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17734 {
17735 for (p = tostr; *p != NUL; p += tolen)
17736 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017737 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017738 if (idx-- == 0)
17739 {
17740 cplen = tolen;
17741 cpstr = p;
17742 break;
17743 }
17744 }
17745 if (*p == NUL) /* tostr is shorter than fromstr */
17746 goto error;
17747 break;
17748 }
17749 ++idx;
17750 }
17751
17752 if (first && cpstr == instr)
17753 {
17754 /* Check that fromstr and tostr have the same number of
17755 * (multi-byte) characters. Done only once when a character
17756 * of instr doesn't appear in fromstr. */
17757 first = FALSE;
17758 for (p = tostr; *p != NUL; p += tolen)
17759 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017760 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017761 --idx;
17762 }
17763 if (idx != 0)
17764 goto error;
17765 }
17766
17767 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017768 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017769 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017770
17771 instr += inlen;
17772 }
17773 else
17774#endif
17775 {
17776 /* When not using multi-byte chars we can do it faster. */
17777 p = vim_strchr(fromstr, *instr);
17778 if (p != NULL)
17779 ga_append(&ga, tostr[p - fromstr]);
17780 else
17781 ga_append(&ga, *instr);
17782 ++instr;
17783 }
17784 }
17785
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017786 /* add a terminating NUL */
17787 ga_grow(&ga, 1);
17788 ga_append(&ga, NUL);
17789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017791}
17792
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017793#ifdef FEAT_FLOAT
17794/*
17795 * "trunc({float})" function
17796 */
17797 static void
17798f_trunc(argvars, rettv)
17799 typval_T *argvars;
17800 typval_T *rettv;
17801{
17802 float_T f;
17803
17804 rettv->v_type = VAR_FLOAT;
17805 if (get_float_arg(argvars, &f) == OK)
17806 /* trunc() is not in C90, use floor() or ceil() instead. */
17807 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17808 else
17809 rettv->vval.v_float = 0.0;
17810}
17811#endif
17812
Bram Moolenaar8299df92004-07-10 09:47:34 +000017813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 * "type(expr)" function
17815 */
17816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017817f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017818 typval_T *argvars;
17819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017821 int n;
17822
17823 switch (argvars[0].v_type)
17824 {
17825 case VAR_NUMBER: n = 0; break;
17826 case VAR_STRING: n = 1; break;
17827 case VAR_FUNC: n = 2; break;
17828 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017829 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017830#ifdef FEAT_FLOAT
17831 case VAR_FLOAT: n = 5; break;
17832#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017833 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17834 }
17835 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836}
17837
17838/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017839 * "undofile(name)" function
17840 */
17841 static void
17842f_undofile(argvars, rettv)
17843 typval_T *argvars;
17844 typval_T *rettv;
17845{
17846 rettv->v_type = VAR_STRING;
17847#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017848 {
17849 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17850
17851 if (ffname != NULL)
17852 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17853 vim_free(ffname);
17854 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017855#else
17856 rettv->vval.v_string = NULL;
17857#endif
17858}
17859
17860/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017861 * "undotree()" function
17862 */
17863 static void
17864f_undotree(argvars, rettv)
17865 typval_T *argvars UNUSED;
17866 typval_T *rettv;
17867{
17868 if (rettv_dict_alloc(rettv) == OK)
17869 {
17870 dict_T *dict = rettv->vval.v_dict;
17871 list_T *list;
17872
Bram Moolenaar730cde92010-06-27 05:18:54 +020017873 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017874 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017875 dict_add_nr_str(dict, "save_last",
17876 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017877 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17878 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017879 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017880
17881 list = list_alloc();
17882 if (list != NULL)
17883 {
17884 u_eval_tree(curbuf->b_u_oldhead, list);
17885 dict_add_list(dict, "entries", list);
17886 }
17887 }
17888}
17889
17890/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017891 * "values(dict)" function
17892 */
17893 static void
17894f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017895 typval_T *argvars;
17896 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017897{
17898 dict_list(argvars, rettv, 1);
17899}
17900
17901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 * "virtcol(string)" function
17903 */
17904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017905f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017906 typval_T *argvars;
17907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908{
17909 colnr_T vcol = 0;
17910 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017911 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017913 fp = var2fpos(&argvars[0], FALSE, &fnum);
17914 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17915 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 {
17917 getvvcol(curwin, fp, NULL, NULL, &vcol);
17918 ++vcol;
17919 }
17920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017921 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922}
17923
17924/*
17925 * "visualmode()" function
17926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017928f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017929 typval_T *argvars UNUSED;
17930 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931{
17932#ifdef FEAT_VISUAL
17933 char_u str[2];
17934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017935 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 str[0] = curbuf->b_visual_mode_eval;
17937 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939
17940 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017941 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943#endif
17944}
17945
17946/*
17947 * "winbufnr(nr)" function
17948 */
17949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017950f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017951 typval_T *argvars;
17952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953{
17954 win_T *wp;
17955
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017956 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961}
17962
17963/*
17964 * "wincol()" function
17965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017967f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970{
17971 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017972 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973}
17974
17975/*
17976 * "winheight(nr)" function
17977 */
17978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017979f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017980 typval_T *argvars;
17981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982{
17983 win_T *wp;
17984
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017985 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017989 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990}
17991
17992/*
17993 * "winline()" function
17994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017996f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017997 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999{
18000 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018001 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002}
18003
18004/*
18005 * "winnr()" function
18006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011{
18012 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018013
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018015 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018}
18019
18020/*
18021 * "winrestcmd()" function
18022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018024f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018025 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027{
18028#ifdef FEAT_WINDOWS
18029 win_T *wp;
18030 int winnr = 1;
18031 garray_T ga;
18032 char_u buf[50];
18033
18034 ga_init2(&ga, (int)sizeof(char), 70);
18035 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18036 {
18037 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18038 ga_concat(&ga, buf);
18039# ifdef FEAT_VERTSPLIT
18040 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18041 ga_concat(&ga, buf);
18042# endif
18043 ++winnr;
18044 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018045 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018047 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052}
18053
18054/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018055 * "winrestview()" function
18056 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018057 static void
18058f_winrestview(argvars, rettv)
18059 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018060 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018061{
18062 dict_T *dict;
18063
18064 if (argvars[0].v_type != VAR_DICT
18065 || (dict = argvars[0].vval.v_dict) == NULL)
18066 EMSG(_(e_invarg));
18067 else
18068 {
18069 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18070 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18071#ifdef FEAT_VIRTUALEDIT
18072 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18073#endif
18074 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018075 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018076
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018077 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018078#ifdef FEAT_DIFF
18079 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18080#endif
18081 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18082 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18083
18084 check_cursor();
18085 changed_cline_bef_curs();
18086 invalidate_botline();
18087 redraw_later(VALID);
18088
18089 if (curwin->w_topline == 0)
18090 curwin->w_topline = 1;
18091 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18092 curwin->w_topline = curbuf->b_ml.ml_line_count;
18093#ifdef FEAT_DIFF
18094 check_topfill(curwin, TRUE);
18095#endif
18096 }
18097}
18098
18099/*
18100 * "winsaveview()" function
18101 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018102 static void
18103f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018104 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018105 typval_T *rettv;
18106{
18107 dict_T *dict;
18108
Bram Moolenaara800b422010-06-27 01:15:55 +020018109 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018110 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018111 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018112
18113 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18114 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18115#ifdef FEAT_VIRTUALEDIT
18116 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18117#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018118 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018119 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18120
18121 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18122#ifdef FEAT_DIFF
18123 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18124#endif
18125 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18126 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18127}
18128
18129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 * "winwidth(nr)" function
18131 */
18132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018134 typval_T *argvars;
18135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136{
18137 win_T *wp;
18138
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018139 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 else
18143#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147#endif
18148}
18149
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018151 * "writefile()" function
18152 */
18153 static void
18154f_writefile(argvars, rettv)
18155 typval_T *argvars;
18156 typval_T *rettv;
18157{
18158 int binary = FALSE;
18159 char_u *fname;
18160 FILE *fd;
18161 listitem_T *li;
18162 char_u *s;
18163 int ret = 0;
18164 int c;
18165
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018166 if (check_restricted() || check_secure())
18167 return;
18168
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018169 if (argvars[0].v_type != VAR_LIST)
18170 {
18171 EMSG2(_(e_listarg), "writefile()");
18172 return;
18173 }
18174 if (argvars[0].vval.v_list == NULL)
18175 return;
18176
18177 if (argvars[2].v_type != VAR_UNKNOWN
18178 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18179 binary = TRUE;
18180
18181 /* Always open the file in binary mode, library functions have a mind of
18182 * their own about CR-LF conversion. */
18183 fname = get_tv_string(&argvars[1]);
18184 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18185 {
18186 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18187 ret = -1;
18188 }
18189 else
18190 {
18191 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18192 li = li->li_next)
18193 {
18194 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18195 {
18196 if (*s == '\n')
18197 c = putc(NUL, fd);
18198 else
18199 c = putc(*s, fd);
18200 if (c == EOF)
18201 {
18202 ret = -1;
18203 break;
18204 }
18205 }
18206 if (!binary || li->li_next != NULL)
18207 if (putc('\n', fd) == EOF)
18208 {
18209 ret = -1;
18210 break;
18211 }
18212 if (ret < 0)
18213 {
18214 EMSG(_(e_write));
18215 break;
18216 }
18217 }
18218 fclose(fd);
18219 }
18220
18221 rettv->vval.v_number = ret;
18222}
18223
18224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018226 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 */
18228 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018229var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018230 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018231 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018232 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018234 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018236 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237
Bram Moolenaara5525202006-03-02 22:52:09 +000018238 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018239 if (varp->v_type == VAR_LIST)
18240 {
18241 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018242 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018243 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018244 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018245
18246 l = varp->vval.v_list;
18247 if (l == NULL)
18248 return NULL;
18249
18250 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018251 pos.lnum = list_find_nr(l, 0L, &error);
18252 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018253 return NULL; /* invalid line number */
18254
18255 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018256 pos.col = list_find_nr(l, 1L, &error);
18257 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018258 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018259 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018260
18261 /* We accept "$" for the column number: last column. */
18262 li = list_find(l, 1L);
18263 if (li != NULL && li->li_tv.v_type == VAR_STRING
18264 && li->li_tv.vval.v_string != NULL
18265 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18266 pos.col = len + 1;
18267
Bram Moolenaara5525202006-03-02 22:52:09 +000018268 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018269 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018270 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018271 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018272
Bram Moolenaara5525202006-03-02 22:52:09 +000018273#ifdef FEAT_VIRTUALEDIT
18274 /* Get the virtual offset. Defaults to zero. */
18275 pos.coladd = list_find_nr(l, 2L, &error);
18276 if (error)
18277 pos.coladd = 0;
18278#endif
18279
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018280 return &pos;
18281 }
18282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018283 name = get_tv_string_chk(varp);
18284 if (name == NULL)
18285 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018286 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018288#ifdef FEAT_VISUAL
18289 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18290 {
18291 if (VIsual_active)
18292 return &VIsual;
18293 return &curwin->w_cursor;
18294 }
18295#endif
18296 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018298 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18300 return NULL;
18301 return pp;
18302 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018303
18304#ifdef FEAT_VIRTUALEDIT
18305 pos.coladd = 0;
18306#endif
18307
Bram Moolenaar477933c2007-07-17 14:32:23 +000018308 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018309 {
18310 pos.col = 0;
18311 if (name[1] == '0') /* "w0": first visible line */
18312 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018313 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018314 pos.lnum = curwin->w_topline;
18315 return &pos;
18316 }
18317 else if (name[1] == '$') /* "w$": last visible line */
18318 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018319 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018320 pos.lnum = curwin->w_botline - 1;
18321 return &pos;
18322 }
18323 }
18324 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018326 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 {
18328 pos.lnum = curbuf->b_ml.ml_line_count;
18329 pos.col = 0;
18330 }
18331 else
18332 {
18333 pos.lnum = curwin->w_cursor.lnum;
18334 pos.col = (colnr_T)STRLEN(ml_get_curline());
18335 }
18336 return &pos;
18337 }
18338 return NULL;
18339}
18340
18341/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018342 * Convert list in "arg" into a position and optional file number.
18343 * When "fnump" is NULL there is no file number, only 3 items.
18344 * Note that the column is passed on as-is, the caller may want to decrement
18345 * it to use 1 for the first column.
18346 * Return FAIL when conversion is not possible, doesn't check the position for
18347 * validity.
18348 */
18349 static int
18350list2fpos(arg, posp, fnump)
18351 typval_T *arg;
18352 pos_T *posp;
18353 int *fnump;
18354{
18355 list_T *l = arg->vval.v_list;
18356 long i = 0;
18357 long n;
18358
Bram Moolenaarbde35262006-07-23 20:12:24 +000018359 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18360 * when "fnump" isn't NULL and "coladd" is optional. */
18361 if (arg->v_type != VAR_LIST
18362 || l == NULL
18363 || l->lv_len < (fnump == NULL ? 2 : 3)
18364 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018365 return FAIL;
18366
18367 if (fnump != NULL)
18368 {
18369 n = list_find_nr(l, i++, NULL); /* fnum */
18370 if (n < 0)
18371 return FAIL;
18372 if (n == 0)
18373 n = curbuf->b_fnum; /* current buffer */
18374 *fnump = n;
18375 }
18376
18377 n = list_find_nr(l, i++, NULL); /* lnum */
18378 if (n < 0)
18379 return FAIL;
18380 posp->lnum = n;
18381
18382 n = list_find_nr(l, i++, NULL); /* col */
18383 if (n < 0)
18384 return FAIL;
18385 posp->col = n;
18386
18387#ifdef FEAT_VIRTUALEDIT
18388 n = list_find_nr(l, i, NULL);
18389 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018390 posp->coladd = 0;
18391 else
18392 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018393#endif
18394
18395 return OK;
18396}
18397
18398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 * Get the length of an environment variable name.
18400 * Advance "arg" to the first character after the name.
18401 * Return 0 for error.
18402 */
18403 static int
18404get_env_len(arg)
18405 char_u **arg;
18406{
18407 char_u *p;
18408 int len;
18409
18410 for (p = *arg; vim_isIDc(*p); ++p)
18411 ;
18412 if (p == *arg) /* no name found */
18413 return 0;
18414
18415 len = (int)(p - *arg);
18416 *arg = p;
18417 return len;
18418}
18419
18420/*
18421 * Get the length of the name of a function or internal variable.
18422 * "arg" is advanced to the first non-white character after the name.
18423 * Return 0 if something is wrong.
18424 */
18425 static int
18426get_id_len(arg)
18427 char_u **arg;
18428{
18429 char_u *p;
18430 int len;
18431
18432 /* Find the end of the name. */
18433 for (p = *arg; eval_isnamec(*p); ++p)
18434 ;
18435 if (p == *arg) /* no name found */
18436 return 0;
18437
18438 len = (int)(p - *arg);
18439 *arg = skipwhite(p);
18440
18441 return len;
18442}
18443
18444/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018445 * Get the length of the name of a variable or function.
18446 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018448 * Return -1 if curly braces expansion failed.
18449 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 * If the name contains 'magic' {}'s, expand them and return the
18451 * expanded name in an allocated string via 'alias' - caller must free.
18452 */
18453 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018454get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 char_u **arg;
18456 char_u **alias;
18457 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018458 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459{
18460 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 char_u *p;
18462 char_u *expr_start;
18463 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464
18465 *alias = NULL; /* default to no alias */
18466
18467 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18468 && (*arg)[2] == (int)KE_SNR)
18469 {
18470 /* hard coded <SNR>, already translated */
18471 *arg += 3;
18472 return get_id_len(arg) + 3;
18473 }
18474 len = eval_fname_script(*arg);
18475 if (len > 0)
18476 {
18477 /* literal "<SID>", "s:" or "<SNR>" */
18478 *arg += len;
18479 }
18480
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018484 p = find_name_end(*arg, &expr_start, &expr_end,
18485 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 if (expr_start != NULL)
18487 {
18488 char_u *temp_string;
18489
18490 if (!evaluate)
18491 {
18492 len += (int)(p - *arg);
18493 *arg = skipwhite(p);
18494 return len;
18495 }
18496
18497 /*
18498 * Include any <SID> etc in the expanded string:
18499 * Thus the -len here.
18500 */
18501 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18502 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018503 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 *alias = temp_string;
18505 *arg = skipwhite(p);
18506 return (int)STRLEN(temp_string);
18507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508
18509 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018510 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 EMSG2(_(e_invexpr2), *arg);
18512
18513 return len;
18514}
18515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018516/*
18517 * Find the end of a variable or function name, taking care of magic braces.
18518 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18519 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018520 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521 * Return a pointer to just after the name. Equal to "arg" if there is no
18522 * valid name.
18523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018525find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 char_u *arg;
18527 char_u **expr_start;
18528 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018529 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018531 int mb_nest = 0;
18532 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 char_u *p;
18534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018537 *expr_start = NULL;
18538 *expr_end = NULL;
18539 }
18540
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018541 /* Quick check for valid starting character. */
18542 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18543 return arg;
18544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018545 for (p = arg; *p != NUL
18546 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018547 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018548 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018549 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018550 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018552 if (*p == '\'')
18553 {
18554 /* skip over 'string' to avoid counting [ and ] inside it. */
18555 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18556 ;
18557 if (*p == NUL)
18558 break;
18559 }
18560 else if (*p == '"')
18561 {
18562 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18563 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18564 if (*p == '\\' && p[1] != NUL)
18565 ++p;
18566 if (*p == NUL)
18567 break;
18568 }
18569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 if (*p == '[')
18573 ++br_nest;
18574 else if (*p == ']')
18575 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018580 if (*p == '{')
18581 {
18582 mb_nest++;
18583 if (expr_start != NULL && *expr_start == NULL)
18584 *expr_start = p;
18585 }
18586 else if (*p == '}')
18587 {
18588 mb_nest--;
18589 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18590 *expr_end = p;
18591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 }
18594
18595 return p;
18596}
18597
18598/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018599 * Expands out the 'magic' {}'s in a variable/function name.
18600 * Note that this can call itself recursively, to deal with
18601 * constructs like foo{bar}{baz}{bam}
18602 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18603 * "in_start" ^
18604 * "expr_start" ^
18605 * "expr_end" ^
18606 * "in_end" ^
18607 *
18608 * Returns a new allocated string, which the caller must free.
18609 * Returns NULL for failure.
18610 */
18611 static char_u *
18612make_expanded_name(in_start, expr_start, expr_end, in_end)
18613 char_u *in_start;
18614 char_u *expr_start;
18615 char_u *expr_end;
18616 char_u *in_end;
18617{
18618 char_u c1;
18619 char_u *retval = NULL;
18620 char_u *temp_result;
18621 char_u *nextcmd = NULL;
18622
18623 if (expr_end == NULL || in_end == NULL)
18624 return NULL;
18625 *expr_start = NUL;
18626 *expr_end = NUL;
18627 c1 = *in_end;
18628 *in_end = NUL;
18629
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018630 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018631 if (temp_result != NULL && nextcmd == NULL)
18632 {
18633 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18634 + (in_end - expr_end) + 1));
18635 if (retval != NULL)
18636 {
18637 STRCPY(retval, in_start);
18638 STRCAT(retval, temp_result);
18639 STRCAT(retval, expr_end + 1);
18640 }
18641 }
18642 vim_free(temp_result);
18643
18644 *in_end = c1; /* put char back for error messages */
18645 *expr_start = '{';
18646 *expr_end = '}';
18647
18648 if (retval != NULL)
18649 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018650 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018651 if (expr_start != NULL)
18652 {
18653 /* Further expansion! */
18654 temp_result = make_expanded_name(retval, expr_start,
18655 expr_end, temp_result);
18656 vim_free(retval);
18657 retval = temp_result;
18658 }
18659 }
18660
18661 return retval;
18662}
18663
18664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018666 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 */
18668 static int
18669eval_isnamec(c)
18670 int c;
18671{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018672 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18673}
18674
18675/*
18676 * Return TRUE if character "c" can be used as the first character in a
18677 * variable or function name (excluding '{' and '}').
18678 */
18679 static int
18680eval_isnamec1(c)
18681 int c;
18682{
18683 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684}
18685
18686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 * Set number v: variable to "val".
18688 */
18689 void
18690set_vim_var_nr(idx, val)
18691 int idx;
18692 long val;
18693{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018694 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695}
18696
18697/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018698 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 */
18700 long
18701get_vim_var_nr(idx)
18702 int idx;
18703{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018704 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705}
18706
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018707/*
18708 * Get string v: variable value. Uses a static buffer, can only be used once.
18709 */
18710 char_u *
18711get_vim_var_str(idx)
18712 int idx;
18713{
18714 return get_tv_string(&vimvars[idx].vv_tv);
18715}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018716
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018718 * Get List v: variable value. Caller must take care of reference count when
18719 * needed.
18720 */
18721 list_T *
18722get_vim_var_list(idx)
18723 int idx;
18724{
18725 return vimvars[idx].vv_list;
18726}
18727
18728/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018729 * Set v:char to character "c".
18730 */
18731 void
18732set_vim_var_char(c)
18733 int c;
18734{
18735#ifdef FEAT_MBYTE
18736 char_u buf[MB_MAXBYTES];
18737#else
18738 char_u buf[2];
18739#endif
18740
18741#ifdef FEAT_MBYTE
18742 if (has_mbyte)
18743 buf[(*mb_char2bytes)(c, buf)] = NUL;
18744 else
18745#endif
18746 {
18747 buf[0] = c;
18748 buf[1] = NUL;
18749 }
18750 set_vim_var_string(VV_CHAR, buf, -1);
18751}
18752
18753/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018754 * Set v:count to "count" and v:count1 to "count1".
18755 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 */
18757 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018758set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 long count;
18760 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018761 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018763 if (set_prevcount)
18764 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018765 vimvars[VV_COUNT].vv_nr = count;
18766 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767}
18768
18769/*
18770 * Set string v: variable to a copy of "val".
18771 */
18772 void
18773set_vim_var_string(idx, val, len)
18774 int idx;
18775 char_u *val;
18776 int len; /* length of "val" to use or -1 (whole string) */
18777{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018778 /* Need to do this (at least) once, since we can't initialize a union.
18779 * Will always be invoked when "v:progname" is set. */
18780 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18781
Bram Moolenaare9a41262005-01-15 22:18:47 +000018782 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018784 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018786 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018788 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
18791/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018792 * Set List v: variable to "val".
18793 */
18794 void
18795set_vim_var_list(idx, val)
18796 int idx;
18797 list_T *val;
18798{
18799 list_unref(vimvars[idx].vv_list);
18800 vimvars[idx].vv_list = val;
18801 if (val != NULL)
18802 ++val->lv_refcount;
18803}
18804
18805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 * Set v:register if needed.
18807 */
18808 void
18809set_reg_var(c)
18810 int c;
18811{
18812 char_u regname;
18813
18814 if (c == 0 || c == ' ')
18815 regname = '"';
18816 else
18817 regname = c;
18818 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018819 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 set_vim_var_string(VV_REG, &regname, 1);
18821}
18822
18823/*
18824 * Get or set v:exception. If "oldval" == NULL, return the current value.
18825 * Otherwise, restore the value to "oldval" and return NULL.
18826 * Must always be called in pairs to save and restore v:exception! Does not
18827 * take care of memory allocations.
18828 */
18829 char_u *
18830v_exception(oldval)
18831 char_u *oldval;
18832{
18833 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018834 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835
Bram Moolenaare9a41262005-01-15 22:18:47 +000018836 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 return NULL;
18838}
18839
18840/*
18841 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18842 * Otherwise, restore the value to "oldval" and return NULL.
18843 * Must always be called in pairs to save and restore v:throwpoint! Does not
18844 * take care of memory allocations.
18845 */
18846 char_u *
18847v_throwpoint(oldval)
18848 char_u *oldval;
18849{
18850 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018851 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852
Bram Moolenaare9a41262005-01-15 22:18:47 +000018853 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 return NULL;
18855}
18856
18857#if defined(FEAT_AUTOCMD) || defined(PROTO)
18858/*
18859 * Set v:cmdarg.
18860 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18861 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18862 * Must always be called in pairs!
18863 */
18864 char_u *
18865set_cmdarg(eap, oldarg)
18866 exarg_T *eap;
18867 char_u *oldarg;
18868{
18869 char_u *oldval;
18870 char_u *newval;
18871 unsigned len;
18872
Bram Moolenaare9a41262005-01-15 22:18:47 +000018873 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018874 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018876 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018877 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018878 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 }
18880
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018881 if (eap->force_bin == FORCE_BIN)
18882 len = 6;
18883 else if (eap->force_bin == FORCE_NOBIN)
18884 len = 8;
18885 else
18886 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018887
18888 if (eap->read_edit)
18889 len += 7;
18890
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018891 if (eap->force_ff != 0)
18892 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18893# ifdef FEAT_MBYTE
18894 if (eap->force_enc != 0)
18895 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018896 if (eap->bad_char != 0)
18897 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018898# endif
18899
18900 newval = alloc(len + 1);
18901 if (newval == NULL)
18902 return NULL;
18903
18904 if (eap->force_bin == FORCE_BIN)
18905 sprintf((char *)newval, " ++bin");
18906 else if (eap->force_bin == FORCE_NOBIN)
18907 sprintf((char *)newval, " ++nobin");
18908 else
18909 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018910
18911 if (eap->read_edit)
18912 STRCAT(newval, " ++edit");
18913
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018914 if (eap->force_ff != 0)
18915 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18916 eap->cmd + eap->force_ff);
18917# ifdef FEAT_MBYTE
18918 if (eap->force_enc != 0)
18919 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18920 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018921 if (eap->bad_char == BAD_KEEP)
18922 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18923 else if (eap->bad_char == BAD_DROP)
18924 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18925 else if (eap->bad_char != 0)
18926 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018927# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018928 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018929 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930}
18931#endif
18932
18933/*
18934 * Get the value of internal variable "name".
18935 * Return OK or FAIL.
18936 */
18937 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018938get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 char_u *name;
18940 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018941 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018942 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943{
18944 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018945 typval_T *tv = NULL;
18946 typval_T atv;
18947 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949
18950 /* truncate the name, so that we can use strcmp() */
18951 cc = name[len];
18952 name[len] = NUL;
18953
18954 /*
18955 * Check for "b:changedtick".
18956 */
18957 if (STRCMP(name, "b:changedtick") == 0)
18958 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018959 atv.v_type = VAR_NUMBER;
18960 atv.vval.v_number = curbuf->b_changedtick;
18961 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 }
18963
18964 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 * Check for user-defined variables.
18966 */
18967 else
18968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018969 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 }
18973
Bram Moolenaare9a41262005-01-15 22:18:47 +000018974 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018976 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018977 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 ret = FAIL;
18979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018981 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
18983 name[len] = cc;
18984
18985 return ret;
18986}
18987
18988/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018989 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18990 * Also handle function call with Funcref variable: func(expr)
18991 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18992 */
18993 static int
18994handle_subscript(arg, rettv, evaluate, verbose)
18995 char_u **arg;
18996 typval_T *rettv;
18997 int evaluate; /* do more than finding the end */
18998 int verbose; /* give error messages */
18999{
19000 int ret = OK;
19001 dict_T *selfdict = NULL;
19002 char_u *s;
19003 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019004 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019005
19006 while (ret == OK
19007 && (**arg == '['
19008 || (**arg == '.' && rettv->v_type == VAR_DICT)
19009 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19010 && !vim_iswhite(*(*arg - 1)))
19011 {
19012 if (**arg == '(')
19013 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019014 /* need to copy the funcref so that we can clear rettv */
19015 functv = *rettv;
19016 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019017
19018 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019019 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019020 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019021 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19022 &len, evaluate, selfdict);
19023
19024 /* Clear the funcref afterwards, so that deleting it while
19025 * evaluating the arguments is possible (see test55). */
19026 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019027
19028 /* Stop the expression evaluation when immediately aborting on
19029 * error, or when an interrupt occurred or an exception was thrown
19030 * but not caught. */
19031 if (aborting())
19032 {
19033 if (ret == OK)
19034 clear_tv(rettv);
19035 ret = FAIL;
19036 }
19037 dict_unref(selfdict);
19038 selfdict = NULL;
19039 }
19040 else /* **arg == '[' || **arg == '.' */
19041 {
19042 dict_unref(selfdict);
19043 if (rettv->v_type == VAR_DICT)
19044 {
19045 selfdict = rettv->vval.v_dict;
19046 if (selfdict != NULL)
19047 ++selfdict->dv_refcount;
19048 }
19049 else
19050 selfdict = NULL;
19051 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19052 {
19053 clear_tv(rettv);
19054 ret = FAIL;
19055 }
19056 }
19057 }
19058 dict_unref(selfdict);
19059 return ret;
19060}
19061
19062/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019063 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019064 * value).
19065 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019067alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019068{
Bram Moolenaar33570922005-01-25 22:26:29 +000019069 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019070}
19071
19072/*
19073 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 * The string "s" must have been allocated, it is consumed.
19075 * Return NULL for out of memory, the variable otherwise.
19076 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019077 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 char_u *s;
19080{
Bram Moolenaar33570922005-01-25 22:26:29 +000019081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019083 rettv = alloc_tv();
19084 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019086 rettv->v_type = VAR_STRING;
19087 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 }
19089 else
19090 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019091 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092}
19093
19094/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019095 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019097 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100{
19101 if (varp != NULL)
19102 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019103 switch (varp->v_type)
19104 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019105 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019106 func_unref(varp->vval.v_string);
19107 /*FALLTHROUGH*/
19108 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019109 vim_free(varp->vval.v_string);
19110 break;
19111 case VAR_LIST:
19112 list_unref(varp->vval.v_list);
19113 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019114 case VAR_DICT:
19115 dict_unref(varp->vval.v_dict);
19116 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019117 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019118#ifdef FEAT_FLOAT
19119 case VAR_FLOAT:
19120#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019121 case VAR_UNKNOWN:
19122 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019123 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019124 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019125 break;
19126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 vim_free(varp);
19128 }
19129}
19130
19131/*
19132 * Free the memory for a variable value and set the value to NULL or 0.
19133 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019134 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137{
19138 if (varp != NULL)
19139 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019140 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019142 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019143 func_unref(varp->vval.v_string);
19144 /*FALLTHROUGH*/
19145 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019146 vim_free(varp->vval.v_string);
19147 varp->vval.v_string = NULL;
19148 break;
19149 case VAR_LIST:
19150 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019151 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019152 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019153 case VAR_DICT:
19154 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019155 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019156 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019157 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019158 varp->vval.v_number = 0;
19159 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019160#ifdef FEAT_FLOAT
19161 case VAR_FLOAT:
19162 varp->vval.v_float = 0.0;
19163 break;
19164#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165 case VAR_UNKNOWN:
19166 break;
19167 default:
19168 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019170 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 }
19172}
19173
19174/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 * Set the value of a variable to NULL without freeing items.
19176 */
19177 static void
19178init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019179 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180{
19181 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183}
19184
19185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 * Get the number value of a variable.
19187 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019188 * For incompatible types, return 0.
19189 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19190 * caller of incompatible types: it sets *denote to TRUE if "denote"
19191 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 */
19193 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019195 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019197 int error = FALSE;
19198
19199 return get_tv_number_chk(varp, &error); /* return 0L on error */
19200}
19201
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019202 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019203get_tv_number_chk(varp, denote)
19204 typval_T *varp;
19205 int *denote;
19206{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019207 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019209 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019211 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019212 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019213#ifdef FEAT_FLOAT
19214 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019215 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019216 break;
19217#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019218 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019219 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019220 break;
19221 case VAR_STRING:
19222 if (varp->vval.v_string != NULL)
19223 vim_str2nr(varp->vval.v_string, NULL, NULL,
19224 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019225 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019226 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019227 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019228 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019229 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019230 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019231 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019232 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019233 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019236 if (denote == NULL) /* useful for values that must be unsigned */
19237 n = -1;
19238 else
19239 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019240 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241}
19242
19243/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019244 * Get the lnum from the first argument.
19245 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019246 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 */
19248 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019249get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019250 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251{
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 linenr_T lnum;
19254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019255 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 if (lnum == 0) /* no valid number, try using line() */
19257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019258 rettv.v_type = VAR_NUMBER;
19259 f_line(argvars, &rettv);
19260 lnum = rettv.vval.v_number;
19261 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 }
19263 return lnum;
19264}
19265
19266/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019267 * Get the lnum from the first argument.
19268 * Also accepts "$", then "buf" is used.
19269 * Returns 0 on error.
19270 */
19271 static linenr_T
19272get_tv_lnum_buf(argvars, buf)
19273 typval_T *argvars;
19274 buf_T *buf;
19275{
19276 if (argvars[0].v_type == VAR_STRING
19277 && argvars[0].vval.v_string != NULL
19278 && argvars[0].vval.v_string[0] == '$'
19279 && buf != NULL)
19280 return buf->b_ml.ml_line_count;
19281 return get_tv_number_chk(&argvars[0], NULL);
19282}
19283
19284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 * Get the string value of a variable.
19286 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019287 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19288 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 * If the String variable has never been set, return an empty string.
19290 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019291 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19292 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 */
19294 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019295get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019296 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297{
19298 static char_u mybuf[NUMBUFLEN];
19299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019300 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301}
19302
19303 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019304get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019306 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019308 char_u *res = get_tv_string_buf_chk(varp, buf);
19309
19310 return res != NULL ? res : (char_u *)"";
19311}
19312
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019313 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019314get_tv_string_chk(varp)
19315 typval_T *varp;
19316{
19317 static char_u mybuf[NUMBUFLEN];
19318
19319 return get_tv_string_buf_chk(varp, mybuf);
19320}
19321
19322 static char_u *
19323get_tv_string_buf_chk(varp, buf)
19324 typval_T *varp;
19325 char_u *buf;
19326{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019327 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019329 case VAR_NUMBER:
19330 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19331 return buf;
19332 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019333 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019334 break;
19335 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019336 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019337 break;
19338 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019339 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019340 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019341#ifdef FEAT_FLOAT
19342 case VAR_FLOAT:
19343 EMSG(_("E806: using Float as a String"));
19344 break;
19345#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019346 case VAR_STRING:
19347 if (varp->vval.v_string != NULL)
19348 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019349 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019350 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019354 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355}
19356
19357/*
19358 * Find variable "name" in the list of variables.
19359 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019360 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019361 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019362 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019364 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019365find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371
Bram Moolenaara7043832005-01-21 11:56:39 +000019372 ht = find_var_ht(name, &varname);
19373 if (htp != NULL)
19374 *htp = ht;
19375 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019377 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378}
19379
19380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019381 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019382 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019384 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019385find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019386 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019387 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019388 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019389{
Bram Moolenaar33570922005-01-25 22:26:29 +000019390 hashitem_T *hi;
19391
19392 if (*varname == NUL)
19393 {
19394 /* Must be something like "s:", otherwise "ht" would be NULL. */
19395 switch (varname[-2])
19396 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019397 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 case 'g': return &globvars_var;
19399 case 'v': return &vimvars_var;
19400 case 'b': return &curbuf->b_bufvar;
19401 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019402#ifdef FEAT_WINDOWS
19403 case 't': return &curtab->tp_winvar;
19404#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019405 case 'l': return current_funccal == NULL
19406 ? NULL : &current_funccal->l_vars_var;
19407 case 'a': return current_funccal == NULL
19408 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 }
19410 return NULL;
19411 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019412
19413 hi = hash_find(ht, varname);
19414 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019415 {
19416 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019417 * worked find the variable again. Don't auto-load a script if it was
19418 * loaded already, otherwise it would be loaded every time when
19419 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019420 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019421 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019422 hi = hash_find(ht, varname);
19423 if (HASHITEM_EMPTY(hi))
19424 return NULL;
19425 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019427}
19428
19429/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019430 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019431 * Set "varname" to the start of name without ':'.
19432 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019433 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019434find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 char_u *name;
19436 char_u **varname;
19437{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019438 hashitem_T *hi;
19439
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 if (name[1] != ':')
19441 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019442 /* The name must not start with a colon or #. */
19443 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 return NULL;
19445 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019446
19447 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019448 hi = hash_find(&compat_hashtab, name);
19449 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019450 return &compat_hashtab;
19451
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019453 return &globvarht; /* global variable */
19454 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 }
19456 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019457 if (*name == 'g') /* global variable */
19458 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019459 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19460 */
19461 if (vim_strchr(name + 2, ':') != NULL
19462 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019463 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019465 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019468#ifdef FEAT_WINDOWS
19469 if (*name == 't') /* tab page variable */
19470 return &curtab->tp_vars.dv_hashtab;
19471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019472 if (*name == 'v') /* v: variable */
19473 return &vimvarht;
19474 if (*name == 'a' && current_funccal != NULL) /* function argument */
19475 return &current_funccal->l_avars.dv_hashtab;
19476 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19477 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 if (*name == 's' /* script variable */
19479 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19480 return &SCRIPT_VARS(current_SID);
19481 return NULL;
19482}
19483
19484/*
19485 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019486 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 * Returns NULL when it doesn't exist.
19488 */
19489 char_u *
19490get_var_value(name)
19491 char_u *name;
19492{
Bram Moolenaar33570922005-01-25 22:26:29 +000019493 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494
Bram Moolenaara7043832005-01-21 11:56:39 +000019495 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 if (v == NULL)
19497 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499}
19500
19501/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 * sourcing this script and when executing functions defined in the script.
19504 */
19505 void
19506new_script_vars(id)
19507 scid_T id;
19508{
Bram Moolenaara7043832005-01-21 11:56:39 +000019509 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 hashtab_T *ht;
19511 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019512
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19514 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019515 /* Re-allocating ga_data means that an ht_array pointing to
19516 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019517 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 for (i = 1; i <= ga_scripts.ga_len; ++i)
19519 {
19520 ht = &SCRIPT_VARS(i);
19521 if (ht->ht_mask == HT_INIT_SIZE - 1)
19522 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019523 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019525 }
19526
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 while (ga_scripts.ga_len < id)
19528 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019529 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019530 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019531 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 }
19534 }
19535}
19536
19537/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019538 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19539 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 */
19541 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019542init_var_dict(dict, dict_var)
19543 dict_T *dict;
19544 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545{
Bram Moolenaar33570922005-01-25 22:26:29 +000019546 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019547 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019548 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 dict_var->di_tv.vval.v_dict = dict;
19550 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019551 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019552 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19553 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554}
19555
19556/*
19557 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019558 * Frees all allocated variables and the value they contain.
19559 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 */
19561 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019562vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019563 hashtab_T *ht;
19564{
19565 vars_clear_ext(ht, TRUE);
19566}
19567
19568/*
19569 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19570 */
19571 static void
19572vars_clear_ext(ht, free_val)
19573 hashtab_T *ht;
19574 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575{
Bram Moolenaara7043832005-01-21 11:56:39 +000019576 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 hashitem_T *hi;
19578 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019581 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019582 for (hi = ht->ht_array; todo > 0; ++hi)
19583 {
19584 if (!HASHITEM_EMPTY(hi))
19585 {
19586 --todo;
19587
Bram Moolenaar33570922005-01-25 22:26:29 +000019588 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019589 * ht_array might change then. hash_clear() takes care of it
19590 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 v = HI2DI(hi);
19592 if (free_val)
19593 clear_tv(&v->di_tv);
19594 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19595 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019596 }
19597 }
19598 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019599 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601
Bram Moolenaara7043832005-01-21 11:56:39 +000019602/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 * Delete a variable from hashtab "ht" at item "hi".
19604 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019607delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019608 hashtab_T *ht;
19609 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610{
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019612
19613 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 clear_tv(&di->di_tv);
19615 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616}
19617
19618/*
19619 * List the value of one internal variable.
19620 */
19621 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019622list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019625 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019627 char_u *tofree;
19628 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019629 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019630
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019631 current_copyID += COPYID_INC;
19632 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019633 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019634 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019635 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636}
19637
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019639list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 char_u *prefix;
19641 char_u *name;
19642 int type;
19643 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019644 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645{
Bram Moolenaar31859182007-08-14 20:41:13 +000019646 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19647 msg_start();
19648 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 if (name != NULL) /* "a:" vars don't have a name stored */
19650 msg_puts(name);
19651 msg_putchar(' ');
19652 msg_advance(22);
19653 if (type == VAR_NUMBER)
19654 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019655 else if (type == VAR_FUNC)
19656 msg_putchar('*');
19657 else if (type == VAR_LIST)
19658 {
19659 msg_putchar('[');
19660 if (*string == '[')
19661 ++string;
19662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019663 else if (type == VAR_DICT)
19664 {
19665 msg_putchar('{');
19666 if (*string == '{')
19667 ++string;
19668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 else
19670 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019671
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019673
19674 if (type == VAR_FUNC)
19675 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019676 if (*first)
19677 {
19678 msg_clr_eos();
19679 *first = FALSE;
19680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681}
19682
19683/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 * If the variable already exists, the value is updated.
19686 * Otherwise the variable is created.
19687 */
19688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019689set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693{
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019697 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019699 ht = find_var_ht(name, &varname);
19700 if (ht == NULL || *varname == NUL)
19701 {
19702 EMSG2(_(e_illvar), name);
19703 return;
19704 }
19705 v = find_var_in_ht(ht, varname, TRUE);
19706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019708 {
19709 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19710 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19711 ? name[2] : name[0]))
19712 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019713 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019714 return;
19715 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019716 /* Don't allow hiding a function. When "v" is not NULL we migth be
19717 * assigning another function to the same var, the type is checked
19718 * below. */
19719 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019720 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019721 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019722 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723 return;
19724 }
19725 }
19726
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019730 if (var_check_ro(v->di_flags, name)
19731 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 return;
19733 if (v->di_tv.v_type != tv->v_type
19734 && !((v->di_tv.v_type == VAR_STRING
19735 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019737 || tv->v_type == VAR_NUMBER))
19738#ifdef FEAT_FLOAT
19739 && !((v->di_tv.v_type == VAR_NUMBER
19740 || v->di_tv.v_type == VAR_FLOAT)
19741 && (tv->v_type == VAR_NUMBER
19742 || tv->v_type == VAR_FLOAT))
19743#endif
19744 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019745 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019746 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747 return;
19748 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019749
19750 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019751 * Handle setting internal v: variables separately: we don't change
19752 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 */
19754 if (ht == &vimvarht)
19755 {
19756 if (v->di_tv.v_type == VAR_STRING)
19757 {
19758 vim_free(v->di_tv.vval.v_string);
19759 if (copy || tv->v_type != VAR_STRING)
19760 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19761 else
19762 {
19763 /* Take over the string to avoid an extra alloc/free. */
19764 v->di_tv.vval.v_string = tv->vval.v_string;
19765 tv->vval.v_string = NULL;
19766 }
19767 }
19768 else if (v->di_tv.v_type != VAR_NUMBER)
19769 EMSG2(_(e_intern2), "set_var()");
19770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019771 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019773 if (STRCMP(varname, "searchforward") == 0)
19774 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19775 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019776 return;
19777 }
19778
19779 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 }
19781 else /* add a new variable */
19782 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019783 /* Can't add "v:" variable. */
19784 if (ht == &vimvarht)
19785 {
19786 EMSG2(_(e_illvar), name);
19787 return;
19788 }
19789
Bram Moolenaar92124a32005-06-17 22:03:40 +000019790 /* Make sure the variable name is valid. */
19791 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019792 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19793 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019794 {
19795 EMSG2(_(e_illvar), varname);
19796 return;
19797 }
19798
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019799 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19800 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019801 if (v == NULL)
19802 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019803 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019804 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019806 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 return;
19808 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019809 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019811
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019812 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019814 else
19815 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019817 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019818 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820}
19821
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019822/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019823 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 * Also give an error message.
19825 */
19826 static int
19827var_check_ro(flags, name)
19828 int flags;
19829 char_u *name;
19830{
19831 if (flags & DI_FLAGS_RO)
19832 {
19833 EMSG2(_(e_readonlyvar), name);
19834 return TRUE;
19835 }
19836 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19837 {
19838 EMSG2(_(e_readonlysbx), name);
19839 return TRUE;
19840 }
19841 return FALSE;
19842}
19843
19844/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019845 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19846 * Also give an error message.
19847 */
19848 static int
19849var_check_fixed(flags, name)
19850 int flags;
19851 char_u *name;
19852{
19853 if (flags & DI_FLAGS_FIX)
19854 {
19855 EMSG2(_("E795: Cannot delete variable %s"), name);
19856 return TRUE;
19857 }
19858 return FALSE;
19859}
19860
19861/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019862 * Return TRUE if typeval "tv" is set to be locked (immutable).
19863 * Also give an error message, using "name".
19864 */
19865 static int
19866tv_check_lock(lock, name)
19867 int lock;
19868 char_u *name;
19869{
19870 if (lock & VAR_LOCKED)
19871 {
19872 EMSG2(_("E741: Value is locked: %s"),
19873 name == NULL ? (char_u *)_("Unknown") : name);
19874 return TRUE;
19875 }
19876 if (lock & VAR_FIXED)
19877 {
19878 EMSG2(_("E742: Cannot change value of %s"),
19879 name == NULL ? (char_u *)_("Unknown") : name);
19880 return TRUE;
19881 }
19882 return FALSE;
19883}
19884
19885/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019888 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019889 * It is OK for "from" and "to" to point to the same item. This is used to
19890 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019892 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019894 typval_T *from;
19895 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019897 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019898 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019899 switch (from->v_type)
19900 {
19901 case VAR_NUMBER:
19902 to->vval.v_number = from->vval.v_number;
19903 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019904#ifdef FEAT_FLOAT
19905 case VAR_FLOAT:
19906 to->vval.v_float = from->vval.v_float;
19907 break;
19908#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 case VAR_STRING:
19910 case VAR_FUNC:
19911 if (from->vval.v_string == NULL)
19912 to->vval.v_string = NULL;
19913 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019915 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019916 if (from->v_type == VAR_FUNC)
19917 func_ref(to->vval.v_string);
19918 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919 break;
19920 case VAR_LIST:
19921 if (from->vval.v_list == NULL)
19922 to->vval.v_list = NULL;
19923 else
19924 {
19925 to->vval.v_list = from->vval.v_list;
19926 ++to->vval.v_list->lv_refcount;
19927 }
19928 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019929 case VAR_DICT:
19930 if (from->vval.v_dict == NULL)
19931 to->vval.v_dict = NULL;
19932 else
19933 {
19934 to->vval.v_dict = from->vval.v_dict;
19935 ++to->vval.v_dict->dv_refcount;
19936 }
19937 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019938 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019940 break;
19941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942}
19943
19944/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019945 * Make a copy of an item.
19946 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019947 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19948 * reference to an already copied list/dict can be used.
19949 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019950 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019951 static int
19952item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019953 typval_T *from;
19954 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019955 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019956 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019957{
19958 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019959 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019960
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019962 {
19963 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019964 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019965 }
19966 ++recurse;
19967
19968 switch (from->v_type)
19969 {
19970 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019971#ifdef FEAT_FLOAT
19972 case VAR_FLOAT:
19973#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019974 case VAR_STRING:
19975 case VAR_FUNC:
19976 copy_tv(from, to);
19977 break;
19978 case VAR_LIST:
19979 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019980 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019981 if (from->vval.v_list == NULL)
19982 to->vval.v_list = NULL;
19983 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19984 {
19985 /* use the copy made earlier */
19986 to->vval.v_list = from->vval.v_list->lv_copylist;
19987 ++to->vval.v_list->lv_refcount;
19988 }
19989 else
19990 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19991 if (to->vval.v_list == NULL)
19992 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019993 break;
19994 case VAR_DICT:
19995 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019996 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019997 if (from->vval.v_dict == NULL)
19998 to->vval.v_dict = NULL;
19999 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20000 {
20001 /* use the copy made earlier */
20002 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20003 ++to->vval.v_dict->dv_refcount;
20004 }
20005 else
20006 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20007 if (to->vval.v_dict == NULL)
20008 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020009 break;
20010 default:
20011 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020012 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020013 }
20014 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020015 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020016}
20017
20018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 * ":echo expr1 ..." print each argument separated with a space, add a
20020 * newline at the end.
20021 * ":echon expr1 ..." print each argument plain.
20022 */
20023 void
20024ex_echo(eap)
20025 exarg_T *eap;
20026{
20027 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020028 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020029 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 char_u *p;
20031 int needclr = TRUE;
20032 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020033 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034
20035 if (eap->skip)
20036 ++emsg_skip;
20037 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20038 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020039 /* If eval1() causes an error message the text from the command may
20040 * still need to be cleared. E.g., "echo 22,44". */
20041 need_clr_eos = needclr;
20042
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 {
20046 /*
20047 * Report the invalid expression unless the expression evaluation
20048 * has been cancelled due to an aborting error, an interrupt, or an
20049 * exception.
20050 */
20051 if (!aborting())
20052 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020053 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 break;
20055 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020056 need_clr_eos = FALSE;
20057
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 if (!eap->skip)
20059 {
20060 if (atstart)
20061 {
20062 atstart = FALSE;
20063 /* Call msg_start() after eval1(), evaluating the expression
20064 * may cause a message to appear. */
20065 if (eap->cmdidx == CMD_echo)
20066 msg_start();
20067 }
20068 else if (eap->cmdidx == CMD_echo)
20069 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020070 current_copyID += COPYID_INC;
20071 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020072 if (p != NULL)
20073 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020075 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020077 if (*p != TAB && needclr)
20078 {
20079 /* remove any text still there from the command */
20080 msg_clr_eos();
20081 needclr = FALSE;
20082 }
20083 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 }
20085 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020086 {
20087#ifdef FEAT_MBYTE
20088 if (has_mbyte)
20089 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020090 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020091
20092 (void)msg_outtrans_len_attr(p, i, echo_attr);
20093 p += i - 1;
20094 }
20095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020097 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020100 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 arg = skipwhite(arg);
20104 }
20105 eap->nextcmd = check_nextcmd(arg);
20106
20107 if (eap->skip)
20108 --emsg_skip;
20109 else
20110 {
20111 /* remove text that may still be there from the command */
20112 if (needclr)
20113 msg_clr_eos();
20114 if (eap->cmdidx == CMD_echo)
20115 msg_end();
20116 }
20117}
20118
20119/*
20120 * ":echohl {name}".
20121 */
20122 void
20123ex_echohl(eap)
20124 exarg_T *eap;
20125{
20126 int id;
20127
20128 id = syn_name2id(eap->arg);
20129 if (id == 0)
20130 echo_attr = 0;
20131 else
20132 echo_attr = syn_id2attr(id);
20133}
20134
20135/*
20136 * ":execute expr1 ..." execute the result of an expression.
20137 * ":echomsg expr1 ..." Print a message
20138 * ":echoerr expr1 ..." Print an error
20139 * Each gets spaces around each argument and a newline at the end for
20140 * echo commands
20141 */
20142 void
20143ex_execute(eap)
20144 exarg_T *eap;
20145{
20146 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 int ret = OK;
20149 char_u *p;
20150 garray_T ga;
20151 int len;
20152 int save_did_emsg;
20153
20154 ga_init2(&ga, 1, 80);
20155
20156 if (eap->skip)
20157 ++emsg_skip;
20158 while (*arg != NUL && *arg != '|' && *arg != '\n')
20159 {
20160 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020161 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 {
20163 /*
20164 * Report the invalid expression unless the expression evaluation
20165 * has been cancelled due to an aborting error, an interrupt, or an
20166 * exception.
20167 */
20168 if (!aborting())
20169 EMSG2(_(e_invexpr2), p);
20170 ret = FAIL;
20171 break;
20172 }
20173
20174 if (!eap->skip)
20175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 len = (int)STRLEN(p);
20178 if (ga_grow(&ga, len + 2) == FAIL)
20179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 ret = FAIL;
20182 break;
20183 }
20184 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 ga.ga_len += len;
20188 }
20189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 arg = skipwhite(arg);
20192 }
20193
20194 if (ret != FAIL && ga.ga_data != NULL)
20195 {
20196 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020199 out_flush();
20200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 else if (eap->cmdidx == CMD_echoerr)
20202 {
20203 /* We don't want to abort following commands, restore did_emsg. */
20204 save_did_emsg = did_emsg;
20205 EMSG((char_u *)ga.ga_data);
20206 if (!force_abort)
20207 did_emsg = save_did_emsg;
20208 }
20209 else if (eap->cmdidx == CMD_execute)
20210 do_cmdline((char_u *)ga.ga_data,
20211 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20212 }
20213
20214 ga_clear(&ga);
20215
20216 if (eap->skip)
20217 --emsg_skip;
20218
20219 eap->nextcmd = check_nextcmd(arg);
20220}
20221
20222/*
20223 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20224 * "arg" points to the "&" or '+' when called, to "option" when returning.
20225 * Returns NULL when no option name found. Otherwise pointer to the char
20226 * after the option name.
20227 */
20228 static char_u *
20229find_option_end(arg, opt_flags)
20230 char_u **arg;
20231 int *opt_flags;
20232{
20233 char_u *p = *arg;
20234
20235 ++p;
20236 if (*p == 'g' && p[1] == ':')
20237 {
20238 *opt_flags = OPT_GLOBAL;
20239 p += 2;
20240 }
20241 else if (*p == 'l' && p[1] == ':')
20242 {
20243 *opt_flags = OPT_LOCAL;
20244 p += 2;
20245 }
20246 else
20247 *opt_flags = 0;
20248
20249 if (!ASCII_ISALPHA(*p))
20250 return NULL;
20251 *arg = p;
20252
20253 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20254 p += 4; /* termcap option */
20255 else
20256 while (ASCII_ISALPHA(*p))
20257 ++p;
20258 return p;
20259}
20260
20261/*
20262 * ":function"
20263 */
20264 void
20265ex_function(eap)
20266 exarg_T *eap;
20267{
20268 char_u *theline;
20269 int j;
20270 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 char_u *name = NULL;
20273 char_u *p;
20274 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020275 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 garray_T newargs;
20277 garray_T newlines;
20278 int varargs = FALSE;
20279 int mustend = FALSE;
20280 int flags = 0;
20281 ufunc_T *fp;
20282 int indent;
20283 int nesting;
20284 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 dictitem_T *v;
20286 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020287 static int func_nr = 0; /* number for nameless function */
20288 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020289 hashtab_T *ht;
20290 int todo;
20291 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020292 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293
20294 /*
20295 * ":function" without argument: list functions.
20296 */
20297 if (ends_excmd(*eap->arg))
20298 {
20299 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020301 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020302 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020303 {
20304 if (!HASHITEM_EMPTY(hi))
20305 {
20306 --todo;
20307 fp = HI2UF(hi);
20308 if (!isdigit(*fp->uf_name))
20309 list_func_head(fp, FALSE);
20310 }
20311 }
20312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 eap->nextcmd = check_nextcmd(eap->arg);
20314 return;
20315 }
20316
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020317 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020318 * ":function /pat": list functions matching pattern.
20319 */
20320 if (*eap->arg == '/')
20321 {
20322 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20323 if (!eap->skip)
20324 {
20325 regmatch_T regmatch;
20326
20327 c = *p;
20328 *p = NUL;
20329 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20330 *p = c;
20331 if (regmatch.regprog != NULL)
20332 {
20333 regmatch.rm_ic = p_ic;
20334
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020335 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020336 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20337 {
20338 if (!HASHITEM_EMPTY(hi))
20339 {
20340 --todo;
20341 fp = HI2UF(hi);
20342 if (!isdigit(*fp->uf_name)
20343 && vim_regexec(&regmatch, fp->uf_name, 0))
20344 list_func_head(fp, FALSE);
20345 }
20346 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020347 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020348 }
20349 }
20350 if (*p == '/')
20351 ++p;
20352 eap->nextcmd = check_nextcmd(p);
20353 return;
20354 }
20355
20356 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020357 * Get the function name. There are these situations:
20358 * func normal function name
20359 * "name" == func, "fudi.fd_dict" == NULL
20360 * dict.func new dictionary entry
20361 * "name" == NULL, "fudi.fd_dict" set,
20362 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20363 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020364 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020365 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20366 * dict.func existing dict entry that's not a Funcref
20367 * "name" == NULL, "fudi.fd_dict" set,
20368 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020371 name = trans_function_name(&p, eap->skip, 0, &fudi);
20372 paren = (vim_strchr(p, '(') != NULL);
20373 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 {
20375 /*
20376 * Return on an invalid expression in braces, unless the expression
20377 * evaluation has been cancelled due to an aborting error, an
20378 * interrupt, or an exception.
20379 */
20380 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020382 if (!eap->skip && fudi.fd_newkey != NULL)
20383 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020384 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 else
20388 eap->skip = TRUE;
20389 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020390
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 /* An error in a function call during evaluation of an expression in magic
20392 * braces should not cause the function not to be defined. */
20393 saved_did_emsg = did_emsg;
20394 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395
20396 /*
20397 * ":function func" with only function name: list function.
20398 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020399 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 {
20401 if (!ends_excmd(*skipwhite(p)))
20402 {
20403 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020404 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 }
20406 eap->nextcmd = check_nextcmd(p);
20407 if (eap->nextcmd != NULL)
20408 *p = NUL;
20409 if (!eap->skip && !got_int)
20410 {
20411 fp = find_func(name);
20412 if (fp != NULL)
20413 {
20414 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020415 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020417 if (FUNCLINE(fp, j) == NULL)
20418 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 msg_putchar('\n');
20420 msg_outnum((long)(j + 1));
20421 if (j < 9)
20422 msg_putchar(' ');
20423 if (j < 99)
20424 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020425 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 out_flush(); /* show a line at a time */
20427 ui_breakcheck();
20428 }
20429 if (!got_int)
20430 {
20431 msg_putchar('\n');
20432 msg_puts((char_u *)" endfunction");
20433 }
20434 }
20435 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020436 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020438 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 }
20440
20441 /*
20442 * ":function name(arg1, arg2)" Define function.
20443 */
20444 p = skipwhite(p);
20445 if (*p != '(')
20446 {
20447 if (!eap->skip)
20448 {
20449 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020450 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 }
20452 /* attempt to continue by skipping some text */
20453 if (vim_strchr(p, '(') != NULL)
20454 p = vim_strchr(p, '(');
20455 }
20456 p = skipwhite(p + 1);
20457
20458 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20459 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20460
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020461 if (!eap->skip)
20462 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020463 /* Check the name of the function. Unless it's a dictionary function
20464 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020465 if (name != NULL)
20466 arg = name;
20467 else
20468 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020469 if (arg != NULL && (fudi.fd_di == NULL
20470 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020471 {
20472 if (*arg == K_SPECIAL)
20473 j = 3;
20474 else
20475 j = 0;
20476 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20477 : eval_isnamec(arg[j])))
20478 ++j;
20479 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020480 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020481 }
20482 }
20483
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 /*
20485 * Isolate the arguments: "arg1, arg2, ...)"
20486 */
20487 while (*p != ')')
20488 {
20489 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20490 {
20491 varargs = TRUE;
20492 p += 3;
20493 mustend = TRUE;
20494 }
20495 else
20496 {
20497 arg = p;
20498 while (ASCII_ISALNUM(*p) || *p == '_')
20499 ++p;
20500 if (arg == p || isdigit(*arg)
20501 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20502 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20503 {
20504 if (!eap->skip)
20505 EMSG2(_("E125: Illegal argument: %s"), arg);
20506 break;
20507 }
20508 if (ga_grow(&newargs, 1) == FAIL)
20509 goto erret;
20510 c = *p;
20511 *p = NUL;
20512 arg = vim_strsave(arg);
20513 if (arg == NULL)
20514 goto erret;
20515 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20516 *p = c;
20517 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 if (*p == ',')
20519 ++p;
20520 else
20521 mustend = TRUE;
20522 }
20523 p = skipwhite(p);
20524 if (mustend && *p != ')')
20525 {
20526 if (!eap->skip)
20527 EMSG2(_(e_invarg2), eap->arg);
20528 break;
20529 }
20530 }
20531 ++p; /* skip the ')' */
20532
Bram Moolenaare9a41262005-01-15 22:18:47 +000020533 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 for (;;)
20535 {
20536 p = skipwhite(p);
20537 if (STRNCMP(p, "range", 5) == 0)
20538 {
20539 flags |= FC_RANGE;
20540 p += 5;
20541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020542 else if (STRNCMP(p, "dict", 4) == 0)
20543 {
20544 flags |= FC_DICT;
20545 p += 4;
20546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 else if (STRNCMP(p, "abort", 5) == 0)
20548 {
20549 flags |= FC_ABORT;
20550 p += 5;
20551 }
20552 else
20553 break;
20554 }
20555
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020556 /* When there is a line break use what follows for the function body.
20557 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20558 if (*p == '\n')
20559 line_arg = p + 1;
20560 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 EMSG(_(e_trailing));
20562
20563 /*
20564 * Read the body of the function, until ":endfunction" is found.
20565 */
20566 if (KeyTyped)
20567 {
20568 /* Check if the function already exists, don't let the user type the
20569 * whole function before telling him it doesn't work! For a script we
20570 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020571 if (!eap->skip && !eap->forceit)
20572 {
20573 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20574 EMSG(_(e_funcdict));
20575 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020576 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020579 if (!eap->skip && did_emsg)
20580 goto erret;
20581
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 msg_putchar('\n'); /* don't overwrite the function name */
20583 cmdline_row = msg_row;
20584 }
20585
20586 indent = 2;
20587 nesting = 0;
20588 for (;;)
20589 {
20590 msg_scroll = TRUE;
20591 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020592 sourcing_lnum_off = sourcing_lnum;
20593
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020594 if (line_arg != NULL)
20595 {
20596 /* Use eap->arg, split up in parts by line breaks. */
20597 theline = line_arg;
20598 p = vim_strchr(theline, '\n');
20599 if (p == NULL)
20600 line_arg += STRLEN(line_arg);
20601 else
20602 {
20603 *p = NUL;
20604 line_arg = p + 1;
20605 }
20606 }
20607 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 theline = getcmdline(':', 0L, indent);
20609 else
20610 theline = eap->getline(':', eap->cookie, indent);
20611 if (KeyTyped)
20612 lines_left = Rows - 1;
20613 if (theline == NULL)
20614 {
20615 EMSG(_("E126: Missing :endfunction"));
20616 goto erret;
20617 }
20618
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020619 /* Detect line continuation: sourcing_lnum increased more than one. */
20620 if (sourcing_lnum > sourcing_lnum_off + 1)
20621 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20622 else
20623 sourcing_lnum_off = 0;
20624
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 if (skip_until != NULL)
20626 {
20627 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20628 * don't check for ":endfunc". */
20629 if (STRCMP(theline, skip_until) == 0)
20630 {
20631 vim_free(skip_until);
20632 skip_until = NULL;
20633 }
20634 }
20635 else
20636 {
20637 /* skip ':' and blanks*/
20638 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20639 ;
20640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020641 /* Check for "endfunction". */
20642 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020644 if (line_arg == NULL)
20645 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 break;
20647 }
20648
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020649 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 * at "end". */
20651 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20652 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020653 else if (STRNCMP(p, "if", 2) == 0
20654 || STRNCMP(p, "wh", 2) == 0
20655 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 || STRNCMP(p, "try", 3) == 0)
20657 indent += 2;
20658
20659 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020660 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020662 if (*p == '!')
20663 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 p += eval_fname_script(p);
20665 if (ASCII_ISALPHA(*p))
20666 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020667 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 if (*skipwhite(p) == '(')
20669 {
20670 ++nesting;
20671 indent += 2;
20672 }
20673 }
20674 }
20675
20676 /* Check for ":append" or ":insert". */
20677 p = skip_range(p, NULL);
20678 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20679 || (p[0] == 'i'
20680 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20681 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20682 skip_until = vim_strsave((char_u *)".");
20683
20684 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20685 arg = skipwhite(skiptowhite(p));
20686 if (arg[0] == '<' && arg[1] =='<'
20687 && ((p[0] == 'p' && p[1] == 'y'
20688 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20689 || (p[0] == 'p' && p[1] == 'e'
20690 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20691 || (p[0] == 't' && p[1] == 'c'
20692 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20693 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20694 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020695 || (p[0] == 'm' && p[1] == 'z'
20696 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 ))
20698 {
20699 /* ":python <<" continues until a dot, like ":append" */
20700 p = skipwhite(arg + 2);
20701 if (*p == NUL)
20702 skip_until = vim_strsave((char_u *)".");
20703 else
20704 skip_until = vim_strsave(p);
20705 }
20706 }
20707
20708 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020709 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020710 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020711 if (line_arg == NULL)
20712 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020714 }
20715
20716 /* Copy the line to newly allocated memory. get_one_sourceline()
20717 * allocates 250 bytes per line, this saves 80% on average. The cost
20718 * is an extra alloc/free. */
20719 p = vim_strsave(theline);
20720 if (p != NULL)
20721 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020722 if (line_arg == NULL)
20723 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020724 theline = p;
20725 }
20726
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020727 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20728
20729 /* Add NULL lines for continuation lines, so that the line count is
20730 * equal to the index in the growarray. */
20731 while (sourcing_lnum_off-- > 0)
20732 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020733
20734 /* Check for end of eap->arg. */
20735 if (line_arg != NULL && *line_arg == NUL)
20736 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 }
20738
20739 /* Don't define the function when skipping commands or when an error was
20740 * detected. */
20741 if (eap->skip || did_emsg)
20742 goto erret;
20743
20744 /*
20745 * If there are no errors, add the function
20746 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020747 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020748 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020749 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020751 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020752 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020753 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020754 goto erret;
20755 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020756
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020757 fp = find_func(name);
20758 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020760 if (!eap->forceit)
20761 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020762 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 goto erret;
20764 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020765 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020766 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020767 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020768 name);
20769 goto erret;
20770 }
20771 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020772 ga_clear_strings(&(fp->uf_args));
20773 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020774 vim_free(name);
20775 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 }
20778 else
20779 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020780 char numbuf[20];
20781
20782 fp = NULL;
20783 if (fudi.fd_newkey == NULL && !eap->forceit)
20784 {
20785 EMSG(_(e_funcdict));
20786 goto erret;
20787 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020788 if (fudi.fd_di == NULL)
20789 {
20790 /* Can't add a function to a locked dictionary */
20791 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20792 goto erret;
20793 }
20794 /* Can't change an existing function if it is locked */
20795 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20796 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020797
20798 /* Give the function a sequential number. Can only be used with a
20799 * Funcref! */
20800 vim_free(name);
20801 sprintf(numbuf, "%d", ++func_nr);
20802 name = vim_strsave((char_u *)numbuf);
20803 if (name == NULL)
20804 goto erret;
20805 }
20806
20807 if (fp == NULL)
20808 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020809 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020810 {
20811 int slen, plen;
20812 char_u *scriptname;
20813
20814 /* Check that the autoload name matches the script name. */
20815 j = FAIL;
20816 if (sourcing_name != NULL)
20817 {
20818 scriptname = autoload_name(name);
20819 if (scriptname != NULL)
20820 {
20821 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020822 plen = (int)STRLEN(p);
20823 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020824 if (slen > plen && fnamecmp(p,
20825 sourcing_name + slen - plen) == 0)
20826 j = OK;
20827 vim_free(scriptname);
20828 }
20829 }
20830 if (j == FAIL)
20831 {
20832 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20833 goto erret;
20834 }
20835 }
20836
20837 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 if (fp == NULL)
20839 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020840
20841 if (fudi.fd_dict != NULL)
20842 {
20843 if (fudi.fd_di == NULL)
20844 {
20845 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020846 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020847 if (fudi.fd_di == NULL)
20848 {
20849 vim_free(fp);
20850 goto erret;
20851 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020852 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20853 {
20854 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020855 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020856 goto erret;
20857 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020858 }
20859 else
20860 /* overwrite existing dict entry */
20861 clear_tv(&fudi.fd_di->di_tv);
20862 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020863 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020864 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020865 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020866
20867 /* behave like "dict" was used */
20868 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020869 }
20870
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020872 STRCPY(fp->uf_name, name);
20873 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020875 fp->uf_args = newargs;
20876 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020877#ifdef FEAT_PROFILE
20878 fp->uf_tml_count = NULL;
20879 fp->uf_tml_total = NULL;
20880 fp->uf_tml_self = NULL;
20881 fp->uf_profiling = FALSE;
20882 if (prof_def_func())
20883 func_do_profile(fp);
20884#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020885 fp->uf_varargs = varargs;
20886 fp->uf_flags = flags;
20887 fp->uf_calls = 0;
20888 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020889 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890
20891erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 ga_clear_strings(&newargs);
20893 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020894ret_free:
20895 vim_free(skip_until);
20896 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899}
20900
20901/*
20902 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020903 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020905 * flags:
20906 * TFN_INT: internal function name OK
20907 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 * Advances "pp" to just after the function name (if no error).
20909 */
20910 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020911trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 char_u **pp;
20913 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020915 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020917 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 char_u *start;
20919 char_u *end;
20920 int lead;
20921 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020923 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020924
20925 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020928
20929 /* Check for hard coded <SNR>: already translated function ID (from a user
20930 * command). */
20931 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20932 && (*pp)[2] == (int)KE_SNR)
20933 {
20934 *pp += 3;
20935 len = get_id_len(pp) + 3;
20936 return vim_strnsave(start, len);
20937 }
20938
20939 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20940 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020942 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020944
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020945 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20946 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020947 if (end == start)
20948 {
20949 if (!skip)
20950 EMSG(_("E129: Function name required"));
20951 goto theend;
20952 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020953 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020954 {
20955 /*
20956 * Report an invalid expression in braces, unless the expression
20957 * evaluation has been cancelled due to an aborting error, an
20958 * interrupt, or an exception.
20959 */
20960 if (!aborting())
20961 {
20962 if (end != NULL)
20963 EMSG2(_(e_invarg2), start);
20964 }
20965 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020966 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020967 goto theend;
20968 }
20969
20970 if (lv.ll_tv != NULL)
20971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020972 if (fdp != NULL)
20973 {
20974 fdp->fd_dict = lv.ll_dict;
20975 fdp->fd_newkey = lv.ll_newkey;
20976 lv.ll_newkey = NULL;
20977 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020979 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20980 {
20981 name = vim_strsave(lv.ll_tv->vval.v_string);
20982 *pp = end;
20983 }
20984 else
20985 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020986 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20987 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020988 EMSG(_(e_funcref));
20989 else
20990 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020991 name = NULL;
20992 }
20993 goto theend;
20994 }
20995
20996 if (lv.ll_name == NULL)
20997 {
20998 /* Error found, but continue after the function name. */
20999 *pp = end;
21000 goto theend;
21001 }
21002
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021003 /* Check if the name is a Funcref. If so, use the value. */
21004 if (lv.ll_exp_name != NULL)
21005 {
21006 len = (int)STRLEN(lv.ll_exp_name);
21007 name = deref_func_name(lv.ll_exp_name, &len);
21008 if (name == lv.ll_exp_name)
21009 name = NULL;
21010 }
21011 else
21012 {
21013 len = (int)(end - *pp);
21014 name = deref_func_name(*pp, &len);
21015 if (name == *pp)
21016 name = NULL;
21017 }
21018 if (name != NULL)
21019 {
21020 name = vim_strsave(name);
21021 *pp = end;
21022 goto theend;
21023 }
21024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021025 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021026 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021027 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021028 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21029 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21030 {
21031 /* When there was "s:" already or the name expanded to get a
21032 * leading "s:" then remove it. */
21033 lv.ll_name += 2;
21034 len -= 2;
21035 lead = 2;
21036 }
21037 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021038 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021039 {
21040 if (lead == 2) /* skip over "s:" */
21041 lv.ll_name += 2;
21042 len = (int)(end - lv.ll_name);
21043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021044
21045 /*
21046 * Copy the function name to allocated memory.
21047 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21048 * Accept <SNR>123_name() outside a script.
21049 */
21050 if (skip)
21051 lead = 0; /* do nothing */
21052 else if (lead > 0)
21053 {
21054 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021055 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21056 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021057 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021058 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021059 if (current_SID <= 0)
21060 {
21061 EMSG(_(e_usingsid));
21062 goto theend;
21063 }
21064 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21065 lead += (int)STRLEN(sid_buf);
21066 }
21067 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021068 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021069 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021070 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021071 goto theend;
21072 }
21073 name = alloc((unsigned)(len + lead + 1));
21074 if (name != NULL)
21075 {
21076 if (lead > 0)
21077 {
21078 name[0] = K_SPECIAL;
21079 name[1] = KS_EXTRA;
21080 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021081 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021082 STRCPY(name + 3, sid_buf);
21083 }
21084 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21085 name[len + lead] = NUL;
21086 }
21087 *pp = end;
21088
21089theend:
21090 clear_lval(&lv);
21091 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092}
21093
21094/*
21095 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21096 * Return 2 if "p" starts with "s:".
21097 * Return 0 otherwise.
21098 */
21099 static int
21100eval_fname_script(p)
21101 char_u *p;
21102{
21103 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21104 || STRNICMP(p + 1, "SNR>", 4) == 0))
21105 return 5;
21106 if (p[0] == 's' && p[1] == ':')
21107 return 2;
21108 return 0;
21109}
21110
21111/*
21112 * Return TRUE if "p" starts with "<SID>" or "s:".
21113 * Only works if eval_fname_script() returned non-zero for "p"!
21114 */
21115 static int
21116eval_fname_sid(p)
21117 char_u *p;
21118{
21119 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21120}
21121
21122/*
21123 * List the head of the function: "name(arg1, arg2)".
21124 */
21125 static void
21126list_func_head(fp, indent)
21127 ufunc_T *fp;
21128 int indent;
21129{
21130 int j;
21131
21132 msg_start();
21133 if (indent)
21134 MSG_PUTS(" ");
21135 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021136 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 {
21138 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021139 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 }
21141 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021142 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021144 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 {
21146 if (j)
21147 MSG_PUTS(", ");
21148 msg_puts(FUNCARG(fp, j));
21149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021150 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 {
21152 if (j)
21153 MSG_PUTS(", ");
21154 MSG_PUTS("...");
21155 }
21156 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021157 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021158 if (p_verbose > 0)
21159 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160}
21161
21162/*
21163 * Find a function by name, return pointer to it in ufuncs.
21164 * Return NULL for unknown function.
21165 */
21166 static ufunc_T *
21167find_func(name)
21168 char_u *name;
21169{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021170 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021172 hi = hash_find(&func_hashtab, name);
21173 if (!HASHITEM_EMPTY(hi))
21174 return HI2UF(hi);
21175 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176}
21177
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021178#if defined(EXITFREE) || defined(PROTO)
21179 void
21180free_all_functions()
21181{
21182 hashitem_T *hi;
21183
21184 /* Need to start all over every time, because func_free() may change the
21185 * hash table. */
21186 while (func_hashtab.ht_used > 0)
21187 for (hi = func_hashtab.ht_array; ; ++hi)
21188 if (!HASHITEM_EMPTY(hi))
21189 {
21190 func_free(HI2UF(hi));
21191 break;
21192 }
21193}
21194#endif
21195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021196/*
21197 * Return TRUE if a function "name" exists.
21198 */
21199 static int
21200function_exists(name)
21201 char_u *name;
21202{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021203 char_u *nm = name;
21204 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021205 int n = FALSE;
21206
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021207 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021208 nm = skipwhite(nm);
21209
21210 /* Only accept "funcname", "funcname ", "funcname (..." and
21211 * "funcname(...", not "funcname!...". */
21212 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021213 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021214 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021215 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021216 else
21217 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021218 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021219 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021220 return n;
21221}
21222
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021223/*
21224 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021225 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021226 */
21227 static int
21228builtin_function(name)
21229 char_u *name;
21230{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021231 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21232 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021233}
21234
Bram Moolenaar05159a02005-02-26 23:04:13 +000021235#if defined(FEAT_PROFILE) || defined(PROTO)
21236/*
21237 * Start profiling function "fp".
21238 */
21239 static void
21240func_do_profile(fp)
21241 ufunc_T *fp;
21242{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021243 int len = fp->uf_lines.ga_len;
21244
21245 if (len == 0)
21246 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021247 fp->uf_tm_count = 0;
21248 profile_zero(&fp->uf_tm_self);
21249 profile_zero(&fp->uf_tm_total);
21250 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021251 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021252 if (fp->uf_tml_total == NULL)
21253 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021254 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021255 if (fp->uf_tml_self == NULL)
21256 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
21259 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21260 || fp->uf_tml_self == NULL)
21261 return; /* out of memory */
21262
21263 fp->uf_profiling = TRUE;
21264}
21265
21266/*
21267 * Dump the profiling results for all functions in file "fd".
21268 */
21269 void
21270func_dump_profile(fd)
21271 FILE *fd;
21272{
21273 hashitem_T *hi;
21274 int todo;
21275 ufunc_T *fp;
21276 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021277 ufunc_T **sorttab;
21278 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021279
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021280 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021281 if (todo == 0)
21282 return; /* nothing to dump */
21283
Bram Moolenaar73830342005-02-28 22:48:19 +000021284 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21285
Bram Moolenaar05159a02005-02-26 23:04:13 +000021286 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21287 {
21288 if (!HASHITEM_EMPTY(hi))
21289 {
21290 --todo;
21291 fp = HI2UF(hi);
21292 if (fp->uf_profiling)
21293 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021294 if (sorttab != NULL)
21295 sorttab[st_len++] = fp;
21296
Bram Moolenaar05159a02005-02-26 23:04:13 +000021297 if (fp->uf_name[0] == K_SPECIAL)
21298 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21299 else
21300 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21301 if (fp->uf_tm_count == 1)
21302 fprintf(fd, "Called 1 time\n");
21303 else
21304 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21305 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21306 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21307 fprintf(fd, "\n");
21308 fprintf(fd, "count total (s) self (s)\n");
21309
21310 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21311 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021312 if (FUNCLINE(fp, i) == NULL)
21313 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021314 prof_func_line(fd, fp->uf_tml_count[i],
21315 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021316 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21317 }
21318 fprintf(fd, "\n");
21319 }
21320 }
21321 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021322
21323 if (sorttab != NULL && st_len > 0)
21324 {
21325 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21326 prof_total_cmp);
21327 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21328 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21329 prof_self_cmp);
21330 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21331 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021332
21333 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021334}
Bram Moolenaar73830342005-02-28 22:48:19 +000021335
21336 static void
21337prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21338 FILE *fd;
21339 ufunc_T **sorttab;
21340 int st_len;
21341 char *title;
21342 int prefer_self; /* when equal print only self time */
21343{
21344 int i;
21345 ufunc_T *fp;
21346
21347 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21348 fprintf(fd, "count total (s) self (s) function\n");
21349 for (i = 0; i < 20 && i < st_len; ++i)
21350 {
21351 fp = sorttab[i];
21352 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21353 prefer_self);
21354 if (fp->uf_name[0] == K_SPECIAL)
21355 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21356 else
21357 fprintf(fd, " %s()\n", fp->uf_name);
21358 }
21359 fprintf(fd, "\n");
21360}
21361
21362/*
21363 * Print the count and times for one function or function line.
21364 */
21365 static void
21366prof_func_line(fd, count, total, self, prefer_self)
21367 FILE *fd;
21368 int count;
21369 proftime_T *total;
21370 proftime_T *self;
21371 int prefer_self; /* when equal print only self time */
21372{
21373 if (count > 0)
21374 {
21375 fprintf(fd, "%5d ", count);
21376 if (prefer_self && profile_equal(total, self))
21377 fprintf(fd, " ");
21378 else
21379 fprintf(fd, "%s ", profile_msg(total));
21380 if (!prefer_self && profile_equal(total, self))
21381 fprintf(fd, " ");
21382 else
21383 fprintf(fd, "%s ", profile_msg(self));
21384 }
21385 else
21386 fprintf(fd, " ");
21387}
21388
21389/*
21390 * Compare function for total time sorting.
21391 */
21392 static int
21393#ifdef __BORLANDC__
21394_RTLENTRYF
21395#endif
21396prof_total_cmp(s1, s2)
21397 const void *s1;
21398 const void *s2;
21399{
21400 ufunc_T *p1, *p2;
21401
21402 p1 = *(ufunc_T **)s1;
21403 p2 = *(ufunc_T **)s2;
21404 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21405}
21406
21407/*
21408 * Compare function for self time sorting.
21409 */
21410 static int
21411#ifdef __BORLANDC__
21412_RTLENTRYF
21413#endif
21414prof_self_cmp(s1, s2)
21415 const void *s1;
21416 const void *s2;
21417{
21418 ufunc_T *p1, *p2;
21419
21420 p1 = *(ufunc_T **)s1;
21421 p2 = *(ufunc_T **)s2;
21422 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21423}
21424
Bram Moolenaar05159a02005-02-26 23:04:13 +000021425#endif
21426
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021427/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021428 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021429 * Return TRUE if a package was loaded.
21430 */
21431 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021432script_autoload(name, reload)
21433 char_u *name;
21434 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021435{
21436 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021437 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021438 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021439 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021440
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021441 /* Return quickly when autoload disabled. */
21442 if (no_autoload)
21443 return FALSE;
21444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021445 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021446 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021447 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021448 return FALSE;
21449
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021450 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021451
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021452 /* Find the name in the list of previously loaded package names. Skip
21453 * "autoload/", it's always the same. */
21454 for (i = 0; i < ga_loaded.ga_len; ++i)
21455 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21456 break;
21457 if (!reload && i < ga_loaded.ga_len)
21458 ret = FALSE; /* was loaded already */
21459 else
21460 {
21461 /* Remember the name if it wasn't loaded already. */
21462 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21463 {
21464 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21465 tofree = NULL;
21466 }
21467
21468 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021469 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021470 ret = TRUE;
21471 }
21472
21473 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021474 return ret;
21475}
21476
21477/*
21478 * Return the autoload script name for a function or variable name.
21479 * Returns NULL when out of memory.
21480 */
21481 static char_u *
21482autoload_name(name)
21483 char_u *name;
21484{
21485 char_u *p;
21486 char_u *scriptname;
21487
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021488 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021489 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21490 if (scriptname == NULL)
21491 return FALSE;
21492 STRCPY(scriptname, "autoload/");
21493 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021494 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021495 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021496 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021497 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021498 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021499}
21500
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21502
21503/*
21504 * Function given to ExpandGeneric() to obtain the list of user defined
21505 * function names.
21506 */
21507 char_u *
21508get_user_func_name(xp, idx)
21509 expand_T *xp;
21510 int idx;
21511{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021512 static long_u done;
21513 static hashitem_T *hi;
21514 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515
21516 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021518 done = 0;
21519 hi = func_hashtab.ht_array;
21520 }
21521 if (done < func_hashtab.ht_used)
21522 {
21523 if (done++ > 0)
21524 ++hi;
21525 while (HASHITEM_EMPTY(hi))
21526 ++hi;
21527 fp = HI2UF(hi);
21528
21529 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21530 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531
21532 cat_func_name(IObuff, fp);
21533 if (xp->xp_context != EXPAND_USER_FUNC)
21534 {
21535 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021536 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 STRCAT(IObuff, ")");
21538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 return IObuff;
21540 }
21541 return NULL;
21542}
21543
21544#endif /* FEAT_CMDL_COMPL */
21545
21546/*
21547 * Copy the function name of "fp" to buffer "buf".
21548 * "buf" must be able to hold the function name plus three bytes.
21549 * Takes care of script-local function names.
21550 */
21551 static void
21552cat_func_name(buf, fp)
21553 char_u *buf;
21554 ufunc_T *fp;
21555{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021556 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 {
21558 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021559 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 }
21561 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021562 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563}
21564
21565/*
21566 * ":delfunction {name}"
21567 */
21568 void
21569ex_delfunction(eap)
21570 exarg_T *eap;
21571{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021572 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 char_u *p;
21574 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021575 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576
21577 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578 name = trans_function_name(&p, eap->skip, 0, &fudi);
21579 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021581 {
21582 if (fudi.fd_dict != NULL && !eap->skip)
21583 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 if (!ends_excmd(*skipwhite(p)))
21587 {
21588 vim_free(name);
21589 EMSG(_(e_trailing));
21590 return;
21591 }
21592 eap->nextcmd = check_nextcmd(p);
21593 if (eap->nextcmd != NULL)
21594 *p = NUL;
21595
21596 if (!eap->skip)
21597 fp = find_func(name);
21598 vim_free(name);
21599
21600 if (!eap->skip)
21601 {
21602 if (fp == NULL)
21603 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021604 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 return;
21606 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 {
21609 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21610 return;
21611 }
21612
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021615 /* Delete the dict item that refers to the function, it will
21616 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021617 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 else
21620 func_free(fp);
21621 }
21622}
21623
21624/*
21625 * Free a function and remove it from the list of functions.
21626 */
21627 static void
21628func_free(fp)
21629 ufunc_T *fp;
21630{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021631 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632
21633 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021634 ga_clear_strings(&(fp->uf_args));
21635 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021636#ifdef FEAT_PROFILE
21637 vim_free(fp->uf_tml_count);
21638 vim_free(fp->uf_tml_total);
21639 vim_free(fp->uf_tml_self);
21640#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021641
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021642 /* remove the function from the function hashtable */
21643 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21644 if (HASHITEM_EMPTY(hi))
21645 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021646 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647 hash_remove(&func_hashtab, hi);
21648
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649 vim_free(fp);
21650}
21651
21652/*
21653 * Unreference a Function: decrement the reference count and free it when it
21654 * becomes zero. Only for numbered functions.
21655 */
21656 static void
21657func_unref(name)
21658 char_u *name;
21659{
21660 ufunc_T *fp;
21661
21662 if (name != NULL && isdigit(*name))
21663 {
21664 fp = find_func(name);
21665 if (fp == NULL)
21666 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021667 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021668 {
21669 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021670 * when "uf_calls" becomes zero. */
21671 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672 func_free(fp);
21673 }
21674 }
21675}
21676
21677/*
21678 * Count a reference to a Function.
21679 */
21680 static void
21681func_ref(name)
21682 char_u *name;
21683{
21684 ufunc_T *fp;
21685
21686 if (name != NULL && isdigit(*name))
21687 {
21688 fp = find_func(name);
21689 if (fp == NULL)
21690 EMSG2(_(e_intern2), "func_ref()");
21691 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021692 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 }
21694}
21695
21696/*
21697 * Call a user function.
21698 */
21699 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021700call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 ufunc_T *fp; /* pointer to function */
21702 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021703 typval_T *argvars; /* arguments */
21704 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 linenr_T firstline; /* first line of range */
21706 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708{
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 char_u *save_sourcing_name;
21710 linenr_T save_sourcing_lnum;
21711 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021712 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021713 int save_did_emsg;
21714 static int depth = 0;
21715 dictitem_T *v;
21716 int fixvar_idx = 0; /* index in fixvar[] */
21717 int i;
21718 int ai;
21719 char_u numbuf[NUMBUFLEN];
21720 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021721#ifdef FEAT_PROFILE
21722 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021723 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725
21726 /* If depth of calling is getting too high, don't execute the function */
21727 if (depth >= p_mfd)
21728 {
21729 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021730 rettv->v_type = VAR_NUMBER;
21731 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 return;
21733 }
21734 ++depth;
21735
21736 line_breakcheck(); /* check for CTRL-C hit */
21737
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021738 fc = (funccall_T *)alloc(sizeof(funccall_T));
21739 fc->caller = current_funccal;
21740 current_funccal = fc;
21741 fc->func = fp;
21742 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021743 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021744 fc->linenr = 0;
21745 fc->returned = FALSE;
21746 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021748 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21749 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021752 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21754 * each argument variable and saves a lot of time.
21755 */
21756 /*
21757 * Init l: variables.
21758 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021759 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021760 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021761 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021762 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21763 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021764 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021765 name = v->di_key;
21766 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021768 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021770 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 v->di_tv.vval.v_dict = selfdict;
21772 ++selfdict->dv_refcount;
21773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021774
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 /*
21776 * Init a: variables.
21777 * Set a:0 to "argcount".
21778 * Set a:000 to a list with room for the "..." arguments.
21779 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021780 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21781 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021782 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021783 /* Use "name" to avoid a warning from some compiler that checks the
21784 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021785 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021786 name = v->di_key;
21787 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021789 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021791 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021792 v->di_tv.vval.v_list = &fc->l_varlist;
21793 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21794 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21795 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021796
21797 /*
21798 * Set a:firstline to "firstline" and a:lastline to "lastline".
21799 * Set a:name to named arguments.
21800 * Set a:N to the "..." arguments.
21801 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021802 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021804 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 (varnumber_T)lastline);
21806 for (i = 0; i < argcount; ++i)
21807 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021808 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 if (ai < 0)
21810 /* named argument a:name */
21811 name = FUNCARG(fp, i);
21812 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021814 /* "..." argument a:1, a:2, etc. */
21815 sprintf((char *)numbuf, "%d", ai + 1);
21816 name = numbuf;
21817 }
21818 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21819 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021820 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21822 }
21823 else
21824 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021825 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21826 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 if (v == NULL)
21828 break;
21829 v->di_flags = DI_FLAGS_RO;
21830 }
21831 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021832 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021833
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021834 /* Note: the values are copied directly to avoid alloc/free.
21835 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021836 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021837 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021838
21839 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21840 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021841 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21842 fc->l_listitems[ai].li_tv = argvars[i];
21843 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021844 }
21845 }
21846
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 /* Don't redraw while executing the function. */
21848 ++RedrawingDisabled;
21849 save_sourcing_name = sourcing_name;
21850 save_sourcing_lnum = sourcing_lnum;
21851 sourcing_lnum = 1;
21852 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021853 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 if (sourcing_name != NULL)
21855 {
21856 if (save_sourcing_name != NULL
21857 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21858 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21859 else
21860 STRCPY(sourcing_name, "function ");
21861 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21862
21863 if (p_verbose >= 12)
21864 {
21865 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021866 verbose_enter_scroll();
21867
Bram Moolenaar555b2802005-05-19 21:08:39 +000021868 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 if (p_verbose >= 14)
21870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021872 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021873 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021874 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875
21876 msg_puts((char_u *)"(");
21877 for (i = 0; i < argcount; ++i)
21878 {
21879 if (i > 0)
21880 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021881 if (argvars[i].v_type == VAR_NUMBER)
21882 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 else
21884 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021885 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21886 if (s != NULL)
21887 {
21888 trunc_string(s, buf, MSG_BUF_CLEN);
21889 msg_puts(buf);
21890 vim_free(tofree);
21891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 }
21893 }
21894 msg_puts((char_u *)")");
21895 }
21896 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021897
21898 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 --no_wait_return;
21900 }
21901 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021902#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021903 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021904 {
21905 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21906 func_do_profile(fp);
21907 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021908 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021909 {
21910 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021911 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021912 profile_zero(&fp->uf_tm_children);
21913 }
21914 script_prof_save(&wait_start);
21915 }
21916#endif
21917
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021919 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 save_did_emsg = did_emsg;
21921 did_emsg = FALSE;
21922
21923 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021924 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21926
21927 --RedrawingDisabled;
21928
21929 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021930 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021932 clear_tv(rettv);
21933 rettv->v_type = VAR_NUMBER;
21934 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935 }
21936
Bram Moolenaar05159a02005-02-26 23:04:13 +000021937#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021938 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021939 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021940 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021941 profile_end(&call_start);
21942 profile_sub_wait(&wait_start, &call_start);
21943 profile_add(&fp->uf_tm_total, &call_start);
21944 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021945 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021946 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021947 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21948 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021949 }
21950 }
21951#endif
21952
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 /* when being verbose, mention the return value */
21954 if (p_verbose >= 12)
21955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021957 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021960 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021961 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021962 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021963 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021966 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021967 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021968 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021969 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021970
Bram Moolenaar555b2802005-05-19 21:08:39 +000021971 /* The value may be very long. Skip the middle part, so that we
21972 * have some idea how it starts and ends. smsg() would always
21973 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021974 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021975 if (s != NULL)
21976 {
21977 trunc_string(s, buf, MSG_BUF_CLEN);
21978 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21979 vim_free(tofree);
21980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 }
21982 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021983
21984 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 --no_wait_return;
21986 }
21987
21988 vim_free(sourcing_name);
21989 sourcing_name = save_sourcing_name;
21990 sourcing_lnum = save_sourcing_lnum;
21991 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021992#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021993 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021994 script_prof_restore(&wait_start);
21995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996
21997 if (p_verbose >= 12 && sourcing_name != NULL)
21998 {
21999 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022000 verbose_enter_scroll();
22001
Bram Moolenaar555b2802005-05-19 21:08:39 +000022002 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022004
22005 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 --no_wait_return;
22007 }
22008
22009 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022010 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022012
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022013 /* If the a:000 list and the l: and a: dicts are not referenced we can
22014 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022015 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22016 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22017 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22018 {
22019 free_funccal(fc, FALSE);
22020 }
22021 else
22022 {
22023 hashitem_T *hi;
22024 listitem_T *li;
22025 int todo;
22026
22027 /* "fc" is still in use. This can happen when returning "a:000" or
22028 * assigning "l:" to a global variable.
22029 * Link "fc" in the list for garbage collection later. */
22030 fc->caller = previous_funccal;
22031 previous_funccal = fc;
22032
22033 /* Make a copy of the a: variables, since we didn't do that above. */
22034 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22035 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22036 {
22037 if (!HASHITEM_EMPTY(hi))
22038 {
22039 --todo;
22040 v = HI2DI(hi);
22041 copy_tv(&v->di_tv, &v->di_tv);
22042 }
22043 }
22044
22045 /* Make a copy of the a:000 items, since we didn't do that above. */
22046 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22047 copy_tv(&li->li_tv, &li->li_tv);
22048 }
22049}
22050
22051/*
22052 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022053 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022054 */
22055 static int
22056can_free_funccal(fc, copyID)
22057 funccall_T *fc;
22058 int copyID;
22059{
22060 return (fc->l_varlist.lv_copyID != copyID
22061 && fc->l_vars.dv_copyID != copyID
22062 && fc->l_avars.dv_copyID != copyID);
22063}
22064
22065/*
22066 * Free "fc" and what it contains.
22067 */
22068 static void
22069free_funccal(fc, free_val)
22070 funccall_T *fc;
22071 int free_val; /* a: vars were allocated */
22072{
22073 listitem_T *li;
22074
22075 /* The a: variables typevals may not have been allocated, only free the
22076 * allocated variables. */
22077 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22078
22079 /* free all l: variables */
22080 vars_clear(&fc->l_vars.dv_hashtab);
22081
22082 /* Free the a:000 variables if they were allocated. */
22083 if (free_val)
22084 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22085 clear_tv(&li->li_tv);
22086
22087 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088}
22089
22090/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022091 * Add a number variable "name" to dict "dp" with value "nr".
22092 */
22093 static void
22094add_nr_var(dp, v, name, nr)
22095 dict_T *dp;
22096 dictitem_T *v;
22097 char *name;
22098 varnumber_T nr;
22099{
22100 STRCPY(v->di_key, name);
22101 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22102 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22103 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022104 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 v->di_tv.vval.v_number = nr;
22106}
22107
22108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 * ":return [expr]"
22110 */
22111 void
22112ex_return(eap)
22113 exarg_T *eap;
22114{
22115 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 int returning = FALSE;
22118
22119 if (current_funccal == NULL)
22120 {
22121 EMSG(_("E133: :return not inside a function"));
22122 return;
22123 }
22124
22125 if (eap->skip)
22126 ++emsg_skip;
22127
22128 eap->nextcmd = NULL;
22129 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022130 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 {
22132 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022135 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 }
22137 /* It's safer to return also on error. */
22138 else if (!eap->skip)
22139 {
22140 /*
22141 * Return unless the expression evaluation has been cancelled due to an
22142 * aborting error, an interrupt, or an exception.
22143 */
22144 if (!aborting())
22145 returning = do_return(eap, FALSE, TRUE, NULL);
22146 }
22147
22148 /* When skipping or the return gets pending, advance to the next command
22149 * in this line (!returning). Otherwise, ignore the rest of the line.
22150 * Following lines will be ignored by get_func_line(). */
22151 if (returning)
22152 eap->nextcmd = NULL;
22153 else if (eap->nextcmd == NULL) /* no argument */
22154 eap->nextcmd = check_nextcmd(arg);
22155
22156 if (eap->skip)
22157 --emsg_skip;
22158}
22159
22160/*
22161 * Return from a function. Possibly makes the return pending. Also called
22162 * for a pending return at the ":endtry" or after returning from an extra
22163 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022165 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 * FALSE when the return gets pending.
22167 */
22168 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022169do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 exarg_T *eap;
22171 int reanimate;
22172 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174{
22175 int idx;
22176 struct condstack *cstack = eap->cstack;
22177
22178 if (reanimate)
22179 /* Undo the return. */
22180 current_funccal->returned = FALSE;
22181
22182 /*
22183 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22184 * not in its finally clause (which then is to be executed next) is found.
22185 * In this case, make the ":return" pending for execution at the ":endtry".
22186 * Otherwise, return normally.
22187 */
22188 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22189 if (idx >= 0)
22190 {
22191 cstack->cs_pending[idx] = CSTP_RETURN;
22192
22193 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022194 /* A pending return again gets pending. "rettv" points to an
22195 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022197 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 else
22199 {
22200 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022201 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022203 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 {
22207 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022208 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 else
22211 EMSG(_(e_outofmem));
22212 }
22213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022214 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215
22216 if (reanimate)
22217 {
22218 /* The pending return value could be overwritten by a ":return"
22219 * without argument in a finally clause; reset the default
22220 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022221 current_funccal->rettv->v_type = VAR_NUMBER;
22222 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 }
22224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022225 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 }
22227 else
22228 {
22229 current_funccal->returned = TRUE;
22230
22231 /* If the return is carried out now, store the return value. For
22232 * a return immediately after reanimation, the value is already
22233 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022234 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022236 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022237 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 }
22241 }
22242
22243 return idx < 0;
22244}
22245
22246/*
22247 * Free the variable with a pending return value.
22248 */
22249 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022250discard_pending_return(rettv)
22251 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252{
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254}
22255
22256/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022257 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 * is an allocated string. Used by report_pending() for verbose messages.
22259 */
22260 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261get_return_cmd(rettv)
22262 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022264 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022265 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022266 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022268 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022269 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022270 if (s == NULL)
22271 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022272
22273 STRCPY(IObuff, ":return ");
22274 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22275 if (STRLEN(s) + 8 >= IOSIZE)
22276 STRCPY(IObuff + IOSIZE - 4, "...");
22277 vim_free(tofree);
22278 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279}
22280
22281/*
22282 * Get next function line.
22283 * Called by do_cmdline() to get the next line.
22284 * Returns allocated string, or NULL for end of function.
22285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 char_u *
22287get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022288 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022290 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291{
Bram Moolenaar33570922005-01-25 22:26:29 +000022292 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022293 ufunc_T *fp = fcp->func;
22294 char_u *retval;
22295 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296
22297 /* If breakpoints have been added/deleted need to check for it. */
22298 if (fcp->dbg_tick != debug_tick)
22299 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022300 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 sourcing_lnum);
22302 fcp->dbg_tick = debug_tick;
22303 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022304#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022305 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022306 func_line_end(cookie);
22307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308
Bram Moolenaar05159a02005-02-26 23:04:13 +000022309 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022310 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22311 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 retval = NULL;
22313 else
22314 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022315 /* Skip NULL lines (continuation lines). */
22316 while (fcp->linenr < gap->ga_len
22317 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22318 ++fcp->linenr;
22319 if (fcp->linenr >= gap->ga_len)
22320 retval = NULL;
22321 else
22322 {
22323 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22324 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022325#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022326 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022327 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022328#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 }
22331
22332 /* Did we encounter a breakpoint? */
22333 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22334 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022335 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022337 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 sourcing_lnum);
22339 fcp->dbg_tick = debug_tick;
22340 }
22341
22342 return retval;
22343}
22344
Bram Moolenaar05159a02005-02-26 23:04:13 +000022345#if defined(FEAT_PROFILE) || defined(PROTO)
22346/*
22347 * Called when starting to read a function line.
22348 * "sourcing_lnum" must be correct!
22349 * When skipping lines it may not actually be executed, but we won't find out
22350 * until later and we need to store the time now.
22351 */
22352 void
22353func_line_start(cookie)
22354 void *cookie;
22355{
22356 funccall_T *fcp = (funccall_T *)cookie;
22357 ufunc_T *fp = fcp->func;
22358
22359 if (fp->uf_profiling && sourcing_lnum >= 1
22360 && sourcing_lnum <= fp->uf_lines.ga_len)
22361 {
22362 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022363 /* Skip continuation lines. */
22364 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22365 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022366 fp->uf_tml_execed = FALSE;
22367 profile_start(&fp->uf_tml_start);
22368 profile_zero(&fp->uf_tml_children);
22369 profile_get_wait(&fp->uf_tml_wait);
22370 }
22371}
22372
22373/*
22374 * Called when actually executing a function line.
22375 */
22376 void
22377func_line_exec(cookie)
22378 void *cookie;
22379{
22380 funccall_T *fcp = (funccall_T *)cookie;
22381 ufunc_T *fp = fcp->func;
22382
22383 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22384 fp->uf_tml_execed = TRUE;
22385}
22386
22387/*
22388 * Called when done with a function line.
22389 */
22390 void
22391func_line_end(cookie)
22392 void *cookie;
22393{
22394 funccall_T *fcp = (funccall_T *)cookie;
22395 ufunc_T *fp = fcp->func;
22396
22397 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22398 {
22399 if (fp->uf_tml_execed)
22400 {
22401 ++fp->uf_tml_count[fp->uf_tml_idx];
22402 profile_end(&fp->uf_tml_start);
22403 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022404 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022405 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22406 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022407 }
22408 fp->uf_tml_idx = -1;
22409 }
22410}
22411#endif
22412
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413/*
22414 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022415 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 */
22417 int
22418func_has_ended(cookie)
22419 void *cookie;
22420{
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422
22423 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22424 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022425 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 || fcp->returned);
22427}
22428
22429/*
22430 * return TRUE if cookie indicates a function which "abort"s on errors.
22431 */
22432 int
22433func_has_abort(cookie)
22434 void *cookie;
22435{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022436 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437}
22438
22439#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22440typedef enum
22441{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022442 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22443 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22444 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445} var_flavour_T;
22446
22447static var_flavour_T var_flavour __ARGS((char_u *varname));
22448
22449 static var_flavour_T
22450var_flavour(varname)
22451 char_u *varname;
22452{
22453 char_u *p = varname;
22454
22455 if (ASCII_ISUPPER(*p))
22456 {
22457 while (*(++p))
22458 if (ASCII_ISLOWER(*p))
22459 return VAR_FLAVOUR_SESSION;
22460 return VAR_FLAVOUR_VIMINFO;
22461 }
22462 else
22463 return VAR_FLAVOUR_DEFAULT;
22464}
22465#endif
22466
22467#if defined(FEAT_VIMINFO) || defined(PROTO)
22468/*
22469 * Restore global vars that start with a capital from the viminfo file
22470 */
22471 int
22472read_viminfo_varlist(virp, writing)
22473 vir_T *virp;
22474 int writing;
22475{
22476 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022477 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022478 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479
22480 if (!writing && (find_viminfo_parameter('!') != NULL))
22481 {
22482 tab = vim_strchr(virp->vir_line + 1, '\t');
22483 if (tab != NULL)
22484 {
22485 *tab++ = '\0'; /* isolate the variable name */
22486 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022487 type = VAR_STRING;
22488#ifdef FEAT_FLOAT
22489 else if (*tab == 'F')
22490 type = VAR_FLOAT;
22491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492
22493 tab = vim_strchr(tab, '\t');
22494 if (tab != NULL)
22495 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022496 tv.v_type = type;
22497 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022498 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022500#ifdef FEAT_FLOAT
22501 else if (type == VAR_FLOAT)
22502 (void)string2float(tab + 1, &tv.vval.v_float);
22503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022505 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022506 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022507 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022508 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 }
22510 }
22511 }
22512
22513 return viminfo_readline(virp);
22514}
22515
22516/*
22517 * Write global vars that start with a capital to the viminfo file
22518 */
22519 void
22520write_viminfo_varlist(fp)
22521 FILE *fp;
22522{
Bram Moolenaar33570922005-01-25 22:26:29 +000022523 hashitem_T *hi;
22524 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022525 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022526 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022527 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022528 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022529 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530
22531 if (find_viminfo_parameter('!') == NULL)
22532 return;
22533
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022534 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022535
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022536 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022539 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022541 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 this_var = HI2DI(hi);
22543 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022544 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022546 {
22547 case VAR_STRING: s = "STR"; break;
22548 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022549#ifdef FEAT_FLOAT
22550 case VAR_FLOAT: s = "FLO"; break;
22551#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022552 default: continue;
22553 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022555 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022556 if (p != NULL)
22557 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022558 vim_free(tofree);
22559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 }
22561 }
22562}
22563#endif
22564
22565#if defined(FEAT_SESSION) || defined(PROTO)
22566 int
22567store_session_globals(fd)
22568 FILE *fd;
22569{
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 hashitem_T *hi;
22571 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022572 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 char_u *p, *t;
22574
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022575 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022578 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022580 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022581 this_var = HI2DI(hi);
22582 if ((this_var->di_tv.v_type == VAR_NUMBER
22583 || this_var->di_tv.v_type == VAR_STRING)
22584 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022585 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022586 /* Escape special characters with a backslash. Turn a LF and
22587 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022589 (char_u *)"\\\"\n\r");
22590 if (p == NULL) /* out of memory */
22591 break;
22592 for (t = p; *t != NUL; ++t)
22593 if (*t == '\n')
22594 *t = 'n';
22595 else if (*t == '\r')
22596 *t = 'r';
22597 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 this_var->di_key,
22599 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22600 : ' ',
22601 p,
22602 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22603 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022604 || put_eol(fd) == FAIL)
22605 {
22606 vim_free(p);
22607 return FAIL;
22608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 vim_free(p);
22610 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022611#ifdef FEAT_FLOAT
22612 else if (this_var->di_tv.v_type == VAR_FLOAT
22613 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22614 {
22615 float_T f = this_var->di_tv.vval.v_float;
22616 int sign = ' ';
22617
22618 if (f < 0)
22619 {
22620 f = -f;
22621 sign = '-';
22622 }
22623 if ((fprintf(fd, "let %s = %c&%f",
22624 this_var->di_key, sign, f) < 0)
22625 || put_eol(fd) == FAIL)
22626 return FAIL;
22627 }
22628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 }
22630 }
22631 return OK;
22632}
22633#endif
22634
Bram Moolenaar661b1822005-07-28 22:36:45 +000022635/*
22636 * Display script name where an item was last set.
22637 * Should only be invoked when 'verbose' is non-zero.
22638 */
22639 void
22640last_set_msg(scriptID)
22641 scid_T scriptID;
22642{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022643 char_u *p;
22644
Bram Moolenaar661b1822005-07-28 22:36:45 +000022645 if (scriptID != 0)
22646 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022647 p = home_replace_save(NULL, get_scriptname(scriptID));
22648 if (p != NULL)
22649 {
22650 verbose_enter();
22651 MSG_PUTS(_("\n\tLast set from "));
22652 MSG_PUTS(p);
22653 vim_free(p);
22654 verbose_leave();
22655 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022656 }
22657}
22658
Bram Moolenaard812df62008-11-09 12:46:09 +000022659/*
22660 * List v:oldfiles in a nice way.
22661 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022662 void
22663ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022664 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022665{
22666 list_T *l = vimvars[VV_OLDFILES].vv_list;
22667 listitem_T *li;
22668 int nr = 0;
22669
22670 if (l == NULL)
22671 msg((char_u *)_("No old files"));
22672 else
22673 {
22674 msg_start();
22675 msg_scroll = TRUE;
22676 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22677 {
22678 msg_outnum((long)++nr);
22679 MSG_PUTS(": ");
22680 msg_outtrans(get_tv_string(&li->li_tv));
22681 msg_putchar('\n');
22682 out_flush(); /* output one line at a time */
22683 ui_breakcheck();
22684 }
22685 /* Assume "got_int" was set to truncate the listing. */
22686 got_int = FALSE;
22687
22688#ifdef FEAT_BROWSE_CMD
22689 if (cmdmod.browse)
22690 {
22691 quit_more = FALSE;
22692 nr = prompt_for_number(FALSE);
22693 msg_starthere();
22694 if (nr > 0)
22695 {
22696 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22697 (long)nr);
22698
22699 if (p != NULL)
22700 {
22701 p = expand_env_save(p);
22702 eap->arg = p;
22703 eap->cmdidx = CMD_edit;
22704 cmdmod.browse = FALSE;
22705 do_exedit(eap, NULL);
22706 vim_free(p);
22707 }
22708 }
22709 }
22710#endif
22711 }
22712}
22713
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714#endif /* FEAT_EVAL */
22715
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022717#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718
22719#ifdef WIN3264
22720/*
22721 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22722 */
22723static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22724static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22725static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22726
22727/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022728 * Get the short path (8.3) for the filename in "fnamep".
22729 * Only works for a valid file name.
22730 * When the path gets longer "fnamep" is changed and the allocated buffer
22731 * is put in "bufp".
22732 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22733 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 */
22735 static int
22736get_short_pathname(fnamep, bufp, fnamelen)
22737 char_u **fnamep;
22738 char_u **bufp;
22739 int *fnamelen;
22740{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022741 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 char_u *newbuf;
22743
22744 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 l = GetShortPathName(*fnamep, *fnamep, len);
22746 if (l > len - 1)
22747 {
22748 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022749 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 newbuf = vim_strnsave(*fnamep, l);
22751 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753
22754 vim_free(*bufp);
22755 *fnamep = *bufp = newbuf;
22756
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022757 /* Really should always succeed, as the buffer is big enough. */
22758 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 }
22760
22761 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022762 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763}
22764
22765/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022766 * Get the short path (8.3) for the filename in "fname". The converted
22767 * path is returned in "bufp".
22768 *
22769 * Some of the directories specified in "fname" may not exist. This function
22770 * will shorten the existing directories at the beginning of the path and then
22771 * append the remaining non-existing path.
22772 *
22773 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022774 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022775 * bufp - Pointer to an allocated buffer for the filename.
22776 * fnamelen - Length of the filename pointed to by fname
22777 *
22778 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 */
22780 static int
22781shortpath_for_invalid_fname(fname, bufp, fnamelen)
22782 char_u **fname;
22783 char_u **bufp;
22784 int *fnamelen;
22785{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022786 char_u *short_fname, *save_fname, *pbuf_unused;
22787 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022789 int old_len, len;
22790 int new_len, sfx_len;
22791 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792
22793 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022794 old_len = *fnamelen;
22795 save_fname = vim_strnsave(*fname, old_len);
22796 pbuf_unused = NULL;
22797 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022799 endp = save_fname + old_len - 1; /* Find the end of the copy */
22800 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022802 /*
22803 * Try shortening the supplied path till it succeeds by removing one
22804 * directory at a time from the tail of the path.
22805 */
22806 len = 0;
22807 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022809 /* go back one path-separator */
22810 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22811 --endp;
22812 if (endp <= save_fname)
22813 break; /* processed the complete path */
22814
22815 /*
22816 * Replace the path separator with a NUL and try to shorten the
22817 * resulting path.
22818 */
22819 ch = *endp;
22820 *endp = 0;
22821 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022822 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022823 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22824 {
22825 retval = FAIL;
22826 goto theend;
22827 }
22828 *endp = ch; /* preserve the string */
22829
22830 if (len > 0)
22831 break; /* successfully shortened the path */
22832
22833 /* failed to shorten the path. Skip the path separator */
22834 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 }
22836
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022837 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022839 /*
22840 * Succeeded in shortening the path. Now concatenate the shortened
22841 * path with the remaining path at the tail.
22842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022844 /* Compute the length of the new path. */
22845 sfx_len = (int)(save_endp - endp) + 1;
22846 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022848 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022850 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022852 /* There is not enough space in the currently allocated string,
22853 * copy it to a buffer big enough. */
22854 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022856 {
22857 retval = FAIL;
22858 goto theend;
22859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 }
22861 else
22862 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022863 /* Transfer short_fname to the main buffer (it's big enough),
22864 * unless get_short_pathname() did its work in-place. */
22865 *fname = *bufp = save_fname;
22866 if (short_fname != save_fname)
22867 vim_strncpy(save_fname, short_fname, len);
22868 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022870
22871 /* concat the not-shortened part of the path */
22872 vim_strncpy(*fname + len, endp, sfx_len);
22873 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022875
22876theend:
22877 vim_free(pbuf_unused);
22878 vim_free(save_fname);
22879
22880 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881}
22882
22883/*
22884 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022885 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 */
22887 static int
22888shortpath_for_partial(fnamep, bufp, fnamelen)
22889 char_u **fnamep;
22890 char_u **bufp;
22891 int *fnamelen;
22892{
22893 int sepcount, len, tflen;
22894 char_u *p;
22895 char_u *pbuf, *tfname;
22896 int hasTilde;
22897
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022898 /* Count up the path separators from the RHS.. so we know which part
22899 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022901 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 if (vim_ispathsep(*p))
22903 ++sepcount;
22904
22905 /* Need full path first (use expand_env() to remove a "~/") */
22906 hasTilde = (**fnamep == '~');
22907 if (hasTilde)
22908 pbuf = tfname = expand_env_save(*fnamep);
22909 else
22910 pbuf = tfname = FullName_save(*fnamep, FALSE);
22911
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022912 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022914 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916
22917 if (len == 0)
22918 {
22919 /* Don't have a valid filename, so shorten the rest of the
22920 * path if we can. This CAN give us invalid 8.3 filenames, but
22921 * there's not a lot of point in guessing what it might be.
22922 */
22923 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022924 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 }
22927
22928 /* Count the paths backward to find the beginning of the desired string. */
22929 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022930 {
22931#ifdef FEAT_MBYTE
22932 if (has_mbyte)
22933 p -= mb_head_off(tfname, p);
22934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 if (vim_ispathsep(*p))
22936 {
22937 if (sepcount == 0 || (hasTilde && sepcount == 1))
22938 break;
22939 else
22940 sepcount --;
22941 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 if (hasTilde)
22944 {
22945 --p;
22946 if (p >= tfname)
22947 *p = '~';
22948 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 }
22951 else
22952 ++p;
22953
22954 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22955 vim_free(*bufp);
22956 *fnamelen = (int)STRLEN(p);
22957 *bufp = pbuf;
22958 *fnamep = p;
22959
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022960 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961}
22962#endif /* WIN3264 */
22963
22964/*
22965 * Adjust a filename, according to a string of modifiers.
22966 * *fnamep must be NUL terminated when called. When returning, the length is
22967 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022968 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 * When there is an error, *fnamep is set to NULL.
22970 */
22971 int
22972modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22973 char_u *src; /* string with modifiers */
22974 int *usedlen; /* characters after src that are used */
22975 char_u **fnamep; /* file name so far */
22976 char_u **bufp; /* buffer for allocated file name or NULL */
22977 int *fnamelen; /* length of fnamep */
22978{
22979 int valid = 0;
22980 char_u *tail;
22981 char_u *s, *p, *pbuf;
22982 char_u dirname[MAXPATHL];
22983 int c;
22984 int has_fullname = 0;
22985#ifdef WIN3264
22986 int has_shortname = 0;
22987#endif
22988
22989repeat:
22990 /* ":p" - full path/file_name */
22991 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22992 {
22993 has_fullname = 1;
22994
22995 valid |= VALID_PATH;
22996 *usedlen += 2;
22997
22998 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22999 if ((*fnamep)[0] == '~'
23000#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23001 && ((*fnamep)[1] == '/'
23002# ifdef BACKSLASH_IN_FILENAME
23003 || (*fnamep)[1] == '\\'
23004# endif
23005 || (*fnamep)[1] == NUL)
23006
23007#endif
23008 )
23009 {
23010 *fnamep = expand_env_save(*fnamep);
23011 vim_free(*bufp); /* free any allocated file name */
23012 *bufp = *fnamep;
23013 if (*fnamep == NULL)
23014 return -1;
23015 }
23016
23017 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023018 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 {
23020 if (vim_ispathsep(*p)
23021 && p[1] == '.'
23022 && (p[2] == NUL
23023 || vim_ispathsep(p[2])
23024 || (p[2] == '.'
23025 && (p[3] == NUL || vim_ispathsep(p[3])))))
23026 break;
23027 }
23028
23029 /* FullName_save() is slow, don't use it when not needed. */
23030 if (*p != NUL || !vim_isAbsName(*fnamep))
23031 {
23032 *fnamep = FullName_save(*fnamep, *p != NUL);
23033 vim_free(*bufp); /* free any allocated file name */
23034 *bufp = *fnamep;
23035 if (*fnamep == NULL)
23036 return -1;
23037 }
23038
23039 /* Append a path separator to a directory. */
23040 if (mch_isdir(*fnamep))
23041 {
23042 /* Make room for one or two extra characters. */
23043 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23044 vim_free(*bufp); /* free any allocated file name */
23045 *bufp = *fnamep;
23046 if (*fnamep == NULL)
23047 return -1;
23048 add_pathsep(*fnamep);
23049 }
23050 }
23051
23052 /* ":." - path relative to the current directory */
23053 /* ":~" - path relative to the home directory */
23054 /* ":8" - shortname path - postponed till after */
23055 while (src[*usedlen] == ':'
23056 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23057 {
23058 *usedlen += 2;
23059 if (c == '8')
23060 {
23061#ifdef WIN3264
23062 has_shortname = 1; /* Postpone this. */
23063#endif
23064 continue;
23065 }
23066 pbuf = NULL;
23067 /* Need full path first (use expand_env() to remove a "~/") */
23068 if (!has_fullname)
23069 {
23070 if (c == '.' && **fnamep == '~')
23071 p = pbuf = expand_env_save(*fnamep);
23072 else
23073 p = pbuf = FullName_save(*fnamep, FALSE);
23074 }
23075 else
23076 p = *fnamep;
23077
23078 has_fullname = 0;
23079
23080 if (p != NULL)
23081 {
23082 if (c == '.')
23083 {
23084 mch_dirname(dirname, MAXPATHL);
23085 s = shorten_fname(p, dirname);
23086 if (s != NULL)
23087 {
23088 *fnamep = s;
23089 if (pbuf != NULL)
23090 {
23091 vim_free(*bufp); /* free any allocated file name */
23092 *bufp = pbuf;
23093 pbuf = NULL;
23094 }
23095 }
23096 }
23097 else
23098 {
23099 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23100 /* Only replace it when it starts with '~' */
23101 if (*dirname == '~')
23102 {
23103 s = vim_strsave(dirname);
23104 if (s != NULL)
23105 {
23106 *fnamep = s;
23107 vim_free(*bufp);
23108 *bufp = s;
23109 }
23110 }
23111 }
23112 vim_free(pbuf);
23113 }
23114 }
23115
23116 tail = gettail(*fnamep);
23117 *fnamelen = (int)STRLEN(*fnamep);
23118
23119 /* ":h" - head, remove "/file_name", can be repeated */
23120 /* Don't remove the first "/" or "c:\" */
23121 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23122 {
23123 valid |= VALID_HEAD;
23124 *usedlen += 2;
23125 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023126 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023127 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 *fnamelen = (int)(tail - *fnamep);
23129#ifdef VMS
23130 if (*fnamelen > 0)
23131 *fnamelen += 1; /* the path separator is part of the path */
23132#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023133 if (*fnamelen == 0)
23134 {
23135 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23136 p = vim_strsave((char_u *)".");
23137 if (p == NULL)
23138 return -1;
23139 vim_free(*bufp);
23140 *bufp = *fnamep = tail = p;
23141 *fnamelen = 1;
23142 }
23143 else
23144 {
23145 while (tail > s && !after_pathsep(s, tail))
23146 mb_ptr_back(*fnamep, tail);
23147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 }
23149
23150 /* ":8" - shortname */
23151 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23152 {
23153 *usedlen += 2;
23154#ifdef WIN3264
23155 has_shortname = 1;
23156#endif
23157 }
23158
23159#ifdef WIN3264
23160 /* Check shortname after we have done 'heads' and before we do 'tails'
23161 */
23162 if (has_shortname)
23163 {
23164 pbuf = NULL;
23165 /* Copy the string if it is shortened by :h */
23166 if (*fnamelen < (int)STRLEN(*fnamep))
23167 {
23168 p = vim_strnsave(*fnamep, *fnamelen);
23169 if (p == 0)
23170 return -1;
23171 vim_free(*bufp);
23172 *bufp = *fnamep = p;
23173 }
23174
23175 /* Split into two implementations - makes it easier. First is where
23176 * there isn't a full name already, second is where there is.
23177 */
23178 if (!has_fullname && !vim_isAbsName(*fnamep))
23179 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023180 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 return -1;
23182 }
23183 else
23184 {
23185 int l;
23186
23187 /* Simple case, already have the full-name
23188 * Nearly always shorter, so try first time. */
23189 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023190 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 return -1;
23192
23193 if (l == 0)
23194 {
23195 /* Couldn't find the filename.. search the paths.
23196 */
23197 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023198 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 return -1;
23200 }
23201 *fnamelen = l;
23202 }
23203 }
23204#endif /* WIN3264 */
23205
23206 /* ":t" - tail, just the basename */
23207 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23208 {
23209 *usedlen += 2;
23210 *fnamelen -= (int)(tail - *fnamep);
23211 *fnamep = tail;
23212 }
23213
23214 /* ":e" - extension, can be repeated */
23215 /* ":r" - root, without extension, can be repeated */
23216 while (src[*usedlen] == ':'
23217 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23218 {
23219 /* find a '.' in the tail:
23220 * - for second :e: before the current fname
23221 * - otherwise: The last '.'
23222 */
23223 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23224 s = *fnamep - 2;
23225 else
23226 s = *fnamep + *fnamelen - 1;
23227 for ( ; s > tail; --s)
23228 if (s[0] == '.')
23229 break;
23230 if (src[*usedlen + 1] == 'e') /* :e */
23231 {
23232 if (s > tail)
23233 {
23234 *fnamelen += (int)(*fnamep - (s + 1));
23235 *fnamep = s + 1;
23236#ifdef VMS
23237 /* cut version from the extension */
23238 s = *fnamep + *fnamelen - 1;
23239 for ( ; s > *fnamep; --s)
23240 if (s[0] == ';')
23241 break;
23242 if (s > *fnamep)
23243 *fnamelen = s - *fnamep;
23244#endif
23245 }
23246 else if (*fnamep <= tail)
23247 *fnamelen = 0;
23248 }
23249 else /* :r */
23250 {
23251 if (s > tail) /* remove one extension */
23252 *fnamelen = (int)(s - *fnamep);
23253 }
23254 *usedlen += 2;
23255 }
23256
23257 /* ":s?pat?foo?" - substitute */
23258 /* ":gs?pat?foo?" - global substitute */
23259 if (src[*usedlen] == ':'
23260 && (src[*usedlen + 1] == 's'
23261 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23262 {
23263 char_u *str;
23264 char_u *pat;
23265 char_u *sub;
23266 int sep;
23267 char_u *flags;
23268 int didit = FALSE;
23269
23270 flags = (char_u *)"";
23271 s = src + *usedlen + 2;
23272 if (src[*usedlen + 1] == 'g')
23273 {
23274 flags = (char_u *)"g";
23275 ++s;
23276 }
23277
23278 sep = *s++;
23279 if (sep)
23280 {
23281 /* find end of pattern */
23282 p = vim_strchr(s, sep);
23283 if (p != NULL)
23284 {
23285 pat = vim_strnsave(s, (int)(p - s));
23286 if (pat != NULL)
23287 {
23288 s = p + 1;
23289 /* find end of substitution */
23290 p = vim_strchr(s, sep);
23291 if (p != NULL)
23292 {
23293 sub = vim_strnsave(s, (int)(p - s));
23294 str = vim_strnsave(*fnamep, *fnamelen);
23295 if (sub != NULL && str != NULL)
23296 {
23297 *usedlen = (int)(p + 1 - src);
23298 s = do_string_sub(str, pat, sub, flags);
23299 if (s != NULL)
23300 {
23301 *fnamep = s;
23302 *fnamelen = (int)STRLEN(s);
23303 vim_free(*bufp);
23304 *bufp = s;
23305 didit = TRUE;
23306 }
23307 }
23308 vim_free(sub);
23309 vim_free(str);
23310 }
23311 vim_free(pat);
23312 }
23313 }
23314 /* after using ":s", repeat all the modifiers */
23315 if (didit)
23316 goto repeat;
23317 }
23318 }
23319
23320 return valid;
23321}
23322
23323/*
23324 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23325 * "flags" can be "g" to do a global substitute.
23326 * Returns an allocated string, NULL for error.
23327 */
23328 char_u *
23329do_string_sub(str, pat, sub, flags)
23330 char_u *str;
23331 char_u *pat;
23332 char_u *sub;
23333 char_u *flags;
23334{
23335 int sublen;
23336 regmatch_T regmatch;
23337 int i;
23338 int do_all;
23339 char_u *tail;
23340 garray_T ga;
23341 char_u *ret;
23342 char_u *save_cpo;
23343
23344 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23345 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023346 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347
23348 ga_init2(&ga, 1, 200);
23349
23350 do_all = (flags[0] == 'g');
23351
23352 regmatch.rm_ic = p_ic;
23353 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23354 if (regmatch.regprog != NULL)
23355 {
23356 tail = str;
23357 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23358 {
23359 /*
23360 * Get some space for a temporary buffer to do the substitution
23361 * into. It will contain:
23362 * - The text up to where the match is.
23363 * - The substituted text.
23364 * - The text after the match.
23365 */
23366 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23367 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23368 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23369 {
23370 ga_clear(&ga);
23371 break;
23372 }
23373
23374 /* copy the text up to where the match is */
23375 i = (int)(regmatch.startp[0] - tail);
23376 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23377 /* add the substituted text */
23378 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23379 + ga.ga_len + i, TRUE, TRUE, FALSE);
23380 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 /* avoid getting stuck on a match with an empty string */
23382 if (tail == regmatch.endp[0])
23383 {
23384 if (*tail == NUL)
23385 break;
23386 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23387 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 }
23389 else
23390 {
23391 tail = regmatch.endp[0];
23392 if (*tail == NUL)
23393 break;
23394 }
23395 if (!do_all)
23396 break;
23397 }
23398
23399 if (ga.ga_data != NULL)
23400 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23401
23402 vim_free(regmatch.regprog);
23403 }
23404
23405 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23406 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023407 if (p_cpo == empty_option)
23408 p_cpo = save_cpo;
23409 else
23410 /* Darn, evaluating {sub} expression changed the value. */
23411 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412
23413 return ret;
23414}
23415
23416#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */