blob: a819373460f30e4f83591b24488f46b708dbe8af [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 Moolenaar33570922005-01-25 22:26:29 +0000721static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000723static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000725static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000726static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000728static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200729#ifdef FEAT_FLOAT
730static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
737static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200740static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200741static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000751static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000754static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000756static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000757static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static int get_env_len __ARGS((char_u **arg));
759static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000761static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
762#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
763#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
764 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000765static 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 +0000766static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000767static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000768static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
769static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static typval_T *alloc_tv __ARGS((void));
771static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static void init_tv __ARGS((typval_T *varp));
773static long get_tv_number __ARGS((typval_T *varp));
774static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000775static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static char_u *get_tv_string __ARGS((typval_T *varp));
777static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000778static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000780static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
782static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
783static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000784static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
785static 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 +0000786static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
787static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000788static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000789static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000790static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
792static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
793static int eval_fname_script __ARGS((char_u *p));
794static int eval_fname_sid __ARGS((char_u *p));
795static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static ufunc_T *find_func __ARGS((char_u *name));
797static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000798static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799#ifdef FEAT_PROFILE
800static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000801static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
802static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
803static int
804# ifdef __BORLANDC__
805 _RTLENTRYF
806# endif
807 prof_total_cmp __ARGS((const void *s1, const void *s2));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000814static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000815static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000816static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static void func_free __ARGS((ufunc_T *fp));
818static void func_unref __ARGS((char_u *name));
819static void func_ref __ARGS((char_u *name));
820static 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 +0000821static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
822static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000824static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
825static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000826static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000827static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000828static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200830
831#ifdef EBCDIC
832static int compare_func_name __ARGS((const void *s1, const void *s2));
833static void sortFunctions __ARGS(());
834#endif
835
836
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000837/* Character used as separated in autoload function/variable names. */
838#define AUTOLOAD_CHAR '#'
839
Bram Moolenaar33570922005-01-25 22:26:29 +0000840/*
841 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000842 */
843 void
844eval_init()
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 int i;
847 struct vimvar *p;
848
849 init_var_dict(&globvardict, &globvars_var);
850 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000851 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000852 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000853
854 for (i = 0; i < VV_LEN; ++i)
855 {
856 p = &vimvars[i];
857 STRCPY(p->vv_di.di_key, p->vv_name);
858 if (p->vv_flags & VV_RO)
859 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
860 else if (p->vv_flags & VV_RO_SBX)
861 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
862 else
863 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000864
865 /* add to v: scope dict, unless the value is not always available */
866 if (p->vv_type != VAR_UNKNOWN)
867 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000869 /* add to compat scope dict */
870 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000872 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200873
874#ifdef EBCDIC
875 /*
876 * Sort the function table, to enable binary sort.
877 */
878 sortFunctions();
879#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000880}
881
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000882#if defined(EXITFREE) || defined(PROTO)
883 void
884eval_clear()
885{
886 int i;
887 struct vimvar *p;
888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000894 vim_free(p->vv_str);
895 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000896 }
897 else if (p->vv_di.di_tv.v_type == VAR_LIST)
898 {
899 list_unref(p->vv_list);
900 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 }
903 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000904 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 hash_clear(&compat_hashtab);
906
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908
909 /* global variables */
910 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000912 /* autoloaded script names */
913 ga_clear_strings(&ga_loaded);
914
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200915 /* script-local variables */
916 for (i = 1; i <= ga_scripts.ga_len; ++i)
917 {
918 vars_clear(&SCRIPT_VARS(i));
919 vim_free(SCRIPT_SV(i));
920 }
921 ga_clear(&ga_scripts);
922
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000923 /* unreferenced lists and dicts */
924 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000925
926 /* functions */
927 free_all_functions();
928 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929}
930#endif
931
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 * Return the name of the executed function.
934 */
935 char_u *
936func_name(cookie)
937 void *cookie;
938{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000939 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940}
941
942/*
943 * Return the address holding the next breakpoint line for a funccall cookie.
944 */
945 linenr_T *
946func_breakpoint(cookie)
947 void *cookie;
948{
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the debug tick for a funccall cookie.
954 */
955 int *
956func_dbg_tick(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the nesting level for a funccall cookie.
964 */
965 int
966func_level(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000973funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000975/* pointer to list of previously used funccal, still around because some
976 * item in it is still being used. */
977funccall_T *previous_funccal = NULL;
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979/*
980 * Return TRUE when a function was ended by a ":return" command.
981 */
982 int
983current_func_returned()
984{
985 return current_funccal->returned;
986}
987
988
989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 * Set an internal variable to a string value. Creates the variable if it does
991 * not already exist.
992 */
993 void
994set_internal_string_var(name, value)
995 char_u *name;
996 char_u *value;
997{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000998 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000999 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 val = vim_strsave(value);
1002 if (val != NULL)
1003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001004 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001007 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001008 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
1010 }
1011}
1012
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001015static char_u *redir_endp = NULL;
1016static char_u *redir_varname = NULL;
1017
1018/*
1019 * Start recording command output to a variable
1020 * Returns OK if successfully completed the setup. FAIL otherwise.
1021 */
1022 int
1023var_redir_start(name, append)
1024 char_u *name;
1025 int append; /* append to an existing variable */
1026{
1027 int save_emsg;
1028 int err;
1029 typval_T tv;
1030
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001031 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001032 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 {
1034 EMSG(_(e_invarg));
1035 return FAIL;
1036 }
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 redir_varname = vim_strsave(name);
1040 if (redir_varname == NULL)
1041 return FAIL;
1042
1043 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1044 if (redir_lval == NULL)
1045 {
1046 var_redir_stop();
1047 return FAIL;
1048 }
1049
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001050 /* The output is stored in growarray "redir_ga" until redirection ends. */
1051 ga_init2(&redir_ga, (int)sizeof(char), 500);
1052
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001054 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1055 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1057 {
1058 if (redir_endp != NULL && *redir_endp != NUL)
1059 /* Trailing characters are present after the variable name */
1060 EMSG(_(e_trailing));
1061 else
1062 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001063 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 var_redir_stop();
1065 return FAIL;
1066 }
1067
1068 /* check if we can write to the variable: set it to or append an empty
1069 * string */
1070 save_emsg = did_emsg;
1071 did_emsg = FALSE;
1072 tv.v_type = VAR_STRING;
1073 tv.vval.v_string = (char_u *)"";
1074 if (append)
1075 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1076 else
1077 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1078 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001079 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 if (err)
1081 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001082 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 var_redir_stop();
1084 return FAIL;
1085 }
1086 if (redir_lval->ll_newkey != NULL)
1087 {
1088 /* Dictionary item was created, don't do it again. */
1089 vim_free(redir_lval->ll_newkey);
1090 redir_lval->ll_newkey = NULL;
1091 }
1092
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097 * Append "value[value_len]" to the variable set by var_redir_start().
1098 * The actual appending is postponed until redirection ends, because the value
1099 * appended may in fact be the string we write to, changing it may cause freed
1100 * memory to be used:
1101 * :redir => foo
1102 * :let foo
1103 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 */
1105 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 if (redir_lval == NULL)
1113 return;
1114
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 {
1122 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 }
1125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127}
1128
1129/*
1130 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001131 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 */
1133 void
1134var_redir_stop()
1135{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 typval_T tv;
1137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_lval != NULL)
1139 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 /* If there was no error: assign the text to the variable. */
1141 if (redir_endp != NULL)
1142 {
1143 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1144 tv.v_type = VAR_STRING;
1145 tv.vval.v_string = redir_ga.ga_data;
1146 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1147 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* free the collected output */
1150 vim_free(redir_ga.ga_data);
1151 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 clear_lval(redir_lval);
1154 vim_free(redir_lval);
1155 redir_lval = NULL;
1156 }
1157 vim_free(redir_varname);
1158 redir_varname = NULL;
1159}
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161# if defined(FEAT_MBYTE) || defined(PROTO)
1162 int
1163eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1164 char_u *enc_from;
1165 char_u *enc_to;
1166 char_u *fname_from;
1167 char_u *fname_to;
1168{
1169 int err = FALSE;
1170
1171 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1172 set_vim_var_string(VV_CC_TO, enc_to, -1);
1173 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1174 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1175 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1176 err = TRUE;
1177 set_vim_var_string(VV_CC_FROM, NULL, -1);
1178 set_vim_var_string(VV_CC_TO, NULL, -1);
1179 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1180 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1181
1182 if (err)
1183 return FAIL;
1184 return OK;
1185}
1186# endif
1187
1188# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1189 int
1190eval_printexpr(fname, args)
1191 char_u *fname;
1192 char_u *args;
1193{
1194 int err = FALSE;
1195
1196 set_vim_var_string(VV_FNAME_IN, fname, -1);
1197 set_vim_var_string(VV_CMDARG, args, -1);
1198 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1199 err = TRUE;
1200 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1201 set_vim_var_string(VV_CMDARG, NULL, -1);
1202
1203 if (err)
1204 {
1205 mch_remove(fname);
1206 return FAIL;
1207 }
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_DIFF) || defined(PROTO)
1213 void
1214eval_diff(origfile, newfile, outfile)
1215 char_u *origfile;
1216 char_u *newfile;
1217 char_u *outfile;
1218{
1219 int err = FALSE;
1220
1221 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1222 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1223 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1224 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1225 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1226 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1227 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1228}
1229
1230 void
1231eval_patch(origfile, difffile, outfile)
1232 char_u *origfile;
1233 char_u *difffile;
1234 char_u *outfile;
1235{
1236 int err;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246# endif
1247
1248/*
1249 * Top level evaluation function, returning a boolean.
1250 * Sets "error" to TRUE if there was an error.
1251 * Return TRUE or FALSE.
1252 */
1253 int
1254eval_to_bool(arg, error, nextcmd, skip)
1255 char_u *arg;
1256 int *error;
1257 char_u **nextcmd;
1258 int skip; /* only parse, don't execute */
1259{
Bram Moolenaar33570922005-01-25 22:26:29 +00001260 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 int retval = FALSE;
1262
1263 if (skip)
1264 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 else
1268 {
1269 *error = FALSE;
1270 if (!skip)
1271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 }
1276 if (skip)
1277 --emsg_skip;
1278
1279 return retval;
1280}
1281
1282/*
1283 * Top level evaluation function, returning a string. If "skip" is TRUE,
1284 * only parsing to "nextcmd" is done, without reporting errors. Return
1285 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1286 */
1287 char_u *
1288eval_to_string_skip(arg, nextcmd, skip)
1289 char_u *arg;
1290 char_u **nextcmd;
1291 int skip; /* only parse, don't execute */
1292{
Bram Moolenaar33570922005-01-25 22:26:29 +00001293 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 char_u *retval;
1295
1296 if (skip)
1297 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001298 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 retval = NULL;
1300 else
1301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 retval = vim_strsave(get_tv_string(&tv));
1303 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 if (skip)
1306 --emsg_skip;
1307
1308 return retval;
1309}
1310
1311/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001312 * Skip over an expression at "*pp".
1313 * Return FAIL for an error, OK otherwise.
1314 */
1315 int
1316skip_expr(pp)
1317 char_u **pp;
1318{
Bram Moolenaar33570922005-01-25 22:26:29 +00001319 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320
1321 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323}
1324
1325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001327 * When "convert" is TRUE convert a List into a sequence of lines and convert
1328 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * Return pointer to allocated memory, or NULL for failure.
1330 */
1331 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 char_u *arg;
1334 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001339 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001340#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 retval = NULL;
1346 else
1347 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001349 {
1350 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001351 if (tv.vval.v_list != NULL)
1352 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 ga_append(&ga, NUL);
1354 retval = (char_u *)ga.ga_data;
1355 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356#ifdef FEAT_FLOAT
1357 else if (convert && tv.v_type == VAR_FLOAT)
1358 {
1359 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1360 retval = vim_strsave(numbuf);
1361 }
1362#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 else
1364 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367
1368 return retval;
1369}
1370
1371/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001372 * Call eval_to_string() without using current local variables and using
1373 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 char_u *arg;
1378 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381 char_u *retval;
1382 void *save_funccalp;
1383
1384 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 if (use_sandbox)
1386 ++sandbox;
1387 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001388 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389 if (use_sandbox)
1390 --sandbox;
1391 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 restore_funccal(save_funccalp);
1393 return retval;
1394}
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396/*
1397 * Top level evaluation function, returning a number.
1398 * Evaluates "expr" silently.
1399 * Returns -1 for an error.
1400 */
1401 int
1402eval_to_number(expr)
1403 char_u *expr;
1404{
Bram Moolenaar33570922005-01-25 22:26:29 +00001405 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001407 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 ++emsg_off;
1410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = -1;
1413 else
1414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001415 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 --emsg_off;
1419
1420 return retval;
1421}
1422
Bram Moolenaara40058a2005-07-11 22:42:07 +00001423/*
1424 * Prepare v: variable "idx" to be used.
1425 * Save the current typeval in "save_tv".
1426 * When not used yet add the variable to the v: hashtable.
1427 */
1428 static void
1429prepare_vimvar(idx, save_tv)
1430 int idx;
1431 typval_T *save_tv;
1432{
1433 *save_tv = vimvars[idx].vv_tv;
1434 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1435 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1436}
1437
1438/*
1439 * Restore v: variable "idx" to typeval "save_tv".
1440 * When no longer defined, remove the variable from the v: hashtable.
1441 */
1442 static void
1443restore_vimvar(idx, save_tv)
1444 int idx;
1445 typval_T *save_tv;
1446{
1447 hashitem_T *hi;
1448
Bram Moolenaara40058a2005-07-11 22:42:07 +00001449 vimvars[idx].vv_tv = *save_tv;
1450 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1451 {
1452 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1453 if (HASHITEM_EMPTY(hi))
1454 EMSG2(_(e_intern2), "restore_vimvar()");
1455 else
1456 hash_remove(&vimvarht, hi);
1457 }
1458}
1459
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001460#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001461/*
1462 * Evaluate an expression to a list with suggestions.
1463 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001464 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001465 */
1466 list_T *
1467eval_spell_expr(badword, expr)
1468 char_u *badword;
1469 char_u *expr;
1470{
1471 typval_T save_val;
1472 typval_T rettv;
1473 list_T *list = NULL;
1474 char_u *p = skipwhite(expr);
1475
1476 /* Set "v:val" to the bad word. */
1477 prepare_vimvar(VV_VAL, &save_val);
1478 vimvars[VV_VAL].vv_type = VAR_STRING;
1479 vimvars[VV_VAL].vv_str = badword;
1480 if (p_verbose == 0)
1481 ++emsg_off;
1482
1483 if (eval1(&p, &rettv, TRUE) == OK)
1484 {
1485 if (rettv.v_type != VAR_LIST)
1486 clear_tv(&rettv);
1487 else
1488 list = rettv.vval.v_list;
1489 }
1490
1491 if (p_verbose == 0)
1492 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 restore_vimvar(VV_VAL, &save_val);
1494
1495 return list;
1496}
1497
1498/*
1499 * "list" is supposed to contain two items: a word and a number. Return the
1500 * word in "pp" and the number as the return value.
1501 * Return -1 if anything isn't right.
1502 * Used to get the good word and score from the eval_spell_expr() result.
1503 */
1504 int
1505get_spellword(list, pp)
1506 list_T *list;
1507 char_u **pp;
1508{
1509 listitem_T *li;
1510
1511 li = list->lv_first;
1512 if (li == NULL)
1513 return -1;
1514 *pp = get_tv_string(&li->li_tv);
1515
1516 li = li->li_next;
1517 if (li == NULL)
1518 return -1;
1519 return get_tv_number(&li->li_tv);
1520}
1521#endif
1522
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001523/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001524 * Top level evaluation function.
1525 * Returns an allocated typval_T with the result.
1526 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001527 */
1528 typval_T *
1529eval_expr(arg, nextcmd)
1530 char_u *arg;
1531 char_u **nextcmd;
1532{
1533 typval_T *tv;
1534
1535 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001537 {
1538 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 }
1541
1542 return tv;
1543}
1544
1545
Bram Moolenaar4f688582007-07-24 12:34:30 +00001546#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1547 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001549 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001550 * Uses argv[argc] for the function arguments. Only Number and String
1551 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001552 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554 static int
1555call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 char_u *func;
1557 int argc;
1558 char_u **argv;
1559 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
Bram Moolenaar33570922005-01-25 22:26:29 +00001562 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 long n;
1564 int len;
1565 int i;
1566 int doesrange;
1567 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001570 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 for (i = 0; i < argc; i++)
1575 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001576 /* Pass a NULL or empty argument as an empty string */
1577 if (argv[i] == NULL || *argv[i] == NUL)
1578 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001579 argvars[i].v_type = VAR_STRING;
1580 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001581 continue;
1582 }
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 /* Recognize a number argument, the others must be strings. */
1585 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1586 if (len != 0 && len == (int)STRLEN(argv[i]))
1587 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001588 argvars[i].v_type = VAR_NUMBER;
1589 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 else
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 }
1597
1598 if (safe)
1599 {
1600 save_funccalp = save_funccal();
1601 ++sandbox;
1602 }
1603
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1605 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (safe)
1609 {
1610 --sandbox;
1611 restore_funccal(save_funccalp);
1612 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 vim_free(argvars);
1614
1615 if (ret == FAIL)
1616 clear_tv(rettv);
1617
1618 return ret;
1619}
1620
Bram Moolenaar4f688582007-07-24 12:34:30 +00001621# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001623 * Call vimL function "func" and return the result as a string.
1624 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 * Uses argv[argc] for the function arguments.
1626 */
1627 void *
1628call_func_retstr(func, argc, argv, safe)
1629 char_u *func;
1630 int argc;
1631 char_u **argv;
1632 int safe; /* use the sandbox */
1633{
1634 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001635 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636
1637 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1638 return NULL;
1639
1640 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return retval;
1643}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001644# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645
Bram Moolenaar4f688582007-07-24 12:34:30 +00001646# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001648 * Call vimL function "func" and return the result as a number.
1649 * Returns -1 when calling the function fails.
1650 * Uses argv[argc] for the function arguments.
1651 */
1652 long
1653call_func_retnr(func, argc, argv, safe)
1654 char_u *func;
1655 int argc;
1656 char_u **argv;
1657 int safe; /* use the sandbox */
1658{
1659 typval_T rettv;
1660 long retval;
1661
1662 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1663 return -1;
1664
1665 retval = get_tv_number_chk(&rettv, NULL);
1666 clear_tv(&rettv);
1667 return retval;
1668}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001669# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001670
1671/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001672 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001674 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 */
1676 void *
1677call_func_retlist(func, argc, argv, safe)
1678 char_u *func;
1679 int argc;
1680 char_u **argv;
1681 int safe; /* use the sandbox */
1682{
1683 typval_T rettv;
1684
1685 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1686 return NULL;
1687
1688 if (rettv.v_type != VAR_LIST)
1689 {
1690 clear_tv(&rettv);
1691 return NULL;
1692 }
1693
1694 return rettv.vval.v_list;
1695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697
Bram Moolenaar4f688582007-07-24 12:34:30 +00001698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699/*
1700 * Save the current function call pointer, and set it to NULL.
1701 * Used when executing autocommands and for ":source".
1702 */
1703 void *
1704save_funccal()
1705{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001706 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 current_funccal = NULL;
1709 return (void *)fc;
1710}
1711
1712 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713restore_funccal(vfc)
1714 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001716 funccall_T *fc = (funccall_T *)vfc;
1717
1718 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719}
1720
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721#if defined(FEAT_PROFILE) || defined(PROTO)
1722/*
1723 * Prepare profiling for entering a child or something else that is not
1724 * counted for the script/function itself.
1725 * Should always be called in pair with prof_child_exit().
1726 */
1727 void
1728prof_child_enter(tm)
1729 proftime_T *tm; /* place to store waittime */
1730{
1731 funccall_T *fc = current_funccal;
1732
1733 if (fc != NULL && fc->func->uf_profiling)
1734 profile_start(&fc->prof_child);
1735 script_prof_save(tm);
1736}
1737
1738/*
1739 * Take care of time spent in a child.
1740 * Should always be called after prof_child_enter().
1741 */
1742 void
1743prof_child_exit(tm)
1744 proftime_T *tm; /* where waittime was stored */
1745{
1746 funccall_T *fc = current_funccal;
1747
1748 if (fc != NULL && fc->func->uf_profiling)
1749 {
1750 profile_end(&fc->prof_child);
1751 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1752 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1753 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1754 }
1755 script_prof_restore(tm);
1756}
1757#endif
1758
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#ifdef FEAT_FOLDING
1761/*
1762 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1763 * it in "*cp". Doesn't give error messages.
1764 */
1765 int
1766eval_foldexpr(arg, cp)
1767 char_u *arg;
1768 int *cp;
1769{
Bram Moolenaar33570922005-01-25 22:26:29 +00001770 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 int retval;
1772 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001773 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1774 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001777 if (use_sandbox)
1778 ++sandbox;
1779 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 retval = 0;
1783 else
1784 {
1785 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786 if (tv.v_type == VAR_NUMBER)
1787 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001788 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval = 0;
1790 else
1791 {
1792 /* If the result is a string, check if there is a non-digit before
1793 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (!VIM_ISDIGIT(*s) && *s != '-')
1796 *cp = *s++;
1797 retval = atol((char *)s);
1798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 }
1801 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001802 if (use_sandbox)
1803 --sandbox;
1804 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806 return retval;
1807}
1808#endif
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 * ":let" list all variable values
1812 * ":let var1 var2" list variable values
1813 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814 * ":let var += expr" assignment command.
1815 * ":let var -= expr" assignment command.
1816 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 */
1819 void
1820ex_let(eap)
1821 exarg_T *eap;
1822{
1823 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001825 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 int var_count = 0;
1828 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001830 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001831 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaardb552d602006-03-23 22:59:57 +00001833 argend = skip_var_list(arg, &var_count, &semicolon);
1834 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001836 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1837 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001841 /*
1842 * ":let" without "=": list variables
1843 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 if (*arg == '[')
1845 EMSG(_(e_invarg));
1846 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001848 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001852 list_glob_vars(&first);
1853 list_buf_vars(&first);
1854 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001855#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001857#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 list_script_vars(&first);
1859 list_func_vars(&first);
1860 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 eap->nextcmd = check_nextcmd(arg);
1863 }
1864 else
1865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 op[0] = '=';
1867 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001868 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869 {
1870 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1871 op[0] = expr[-1]; /* +=, -= or .= */
1872 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (eap->skip)
1876 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (eap->skip)
1879 {
1880 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 --emsg_skip;
1883 }
1884 else if (i != FAIL)
1885 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 }
1891}
1892
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893/*
1894 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1895 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 * When "nextchars" is not NULL it points to a string with characters that
1897 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1898 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 * Returns OK or FAIL;
1900 */
1901 static int
1902ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1903 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 int copy; /* copy values from "tv", don't move */
1906 int semicolon; /* from skip_var_list() */
1907 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909{
1910 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 listitem_T *item;
1914 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915
1916 if (*arg != '[')
1917 {
1918 /*
1919 * ":let var = expr" or ":for var in list"
1920 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 return FAIL;
1923 return OK;
1924 }
1925
1926 /*
1927 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1928 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001929 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 {
1931 EMSG(_(e_listreq));
1932 return FAIL;
1933 }
1934
1935 i = list_len(l);
1936 if (semicolon == 0 && var_count < i)
1937 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001938 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 return FAIL;
1940 }
1941 if (var_count - semicolon > i)
1942 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001943 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 return FAIL;
1945 }
1946
1947 item = l->lv_first;
1948 while (*arg != ']')
1949 {
1950 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 item = item->li_next;
1953 if (arg == NULL)
1954 return FAIL;
1955
1956 arg = skipwhite(arg);
1957 if (*arg == ';')
1958 {
1959 /* Put the rest of the list (may be empty) in the var after ';'.
1960 * Create a new list for this. */
1961 l = list_alloc();
1962 if (l == NULL)
1963 return FAIL;
1964 while (item != NULL)
1965 {
1966 list_append_tv(l, &item->li_tv);
1967 item = item->li_next;
1968 }
1969
1970 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001971 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 ltv.vval.v_list = l;
1973 l->lv_refcount = 1;
1974
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1976 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 clear_tv(&ltv);
1978 if (arg == NULL)
1979 return FAIL;
1980 break;
1981 }
1982 else if (*arg != ',' && *arg != ']')
1983 {
1984 EMSG2(_(e_intern2), "ex_let_vars()");
1985 return FAIL;
1986 }
1987 }
1988
1989 return OK;
1990}
1991
1992/*
1993 * Skip over assignable variable "var" or list of variables "[var, var]".
1994 * Used for ":let varvar = expr" and ":for varvar in expr".
1995 * For "[var, var]" increment "*var_count" for each variable.
1996 * for "[var, var; var]" set "semicolon".
1997 * Return NULL for an error.
1998 */
1999 static char_u *
2000skip_var_list(arg, var_count, semicolon)
2001 char_u *arg;
2002 int *var_count;
2003 int *semicolon;
2004{
2005 char_u *p, *s;
2006
2007 if (*arg == '[')
2008 {
2009 /* "[var, var]": find the matching ']'. */
2010 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002011 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 {
2013 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2014 s = skip_var_one(p);
2015 if (s == p)
2016 {
2017 EMSG2(_(e_invarg2), p);
2018 return NULL;
2019 }
2020 ++*var_count;
2021
2022 p = skipwhite(s);
2023 if (*p == ']')
2024 break;
2025 else if (*p == ';')
2026 {
2027 if (*semicolon == 1)
2028 {
2029 EMSG(_("Double ; in list of variables"));
2030 return NULL;
2031 }
2032 *semicolon = 1;
2033 }
2034 else if (*p != ',')
2035 {
2036 EMSG2(_(e_invarg2), p);
2037 return NULL;
2038 }
2039 }
2040 return p + 1;
2041 }
2042 else
2043 return skip_var_one(arg);
2044}
2045
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002047 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002048 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 static char_u *
2051skip_var_one(arg)
2052 char_u *arg;
2053{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002054 if (*arg == '@' && arg[1] != NUL)
2055 return arg + 2;
2056 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2057 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058}
2059
Bram Moolenaara7043832005-01-21 11:56:39 +00002060/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002061 * List variables for hashtab "ht" with prefix "prefix".
2062 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002065list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002066 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002069 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002070{
Bram Moolenaar33570922005-01-25 22:26:29 +00002071 hashitem_T *hi;
2072 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 int todo;
2074
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002075 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2077 {
2078 if (!HASHITEM_EMPTY(hi))
2079 {
2080 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 di = HI2DI(hi);
2082 if (empty || di->di_tv.v_type != VAR_STRING
2083 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002084 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 }
2086 }
2087}
2088
2089/*
2090 * List global variables.
2091 */
2092 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002093list_glob_vars(first)
2094 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002095{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097}
2098
2099/*
2100 * List buffer variables.
2101 */
2102 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103list_buf_vars(first)
2104 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 char_u numbuf[NUMBUFLEN];
2107
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2109 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110
2111 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2113 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114}
2115
2116/*
2117 * List window variables.
2118 */
2119 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120list_win_vars(first)
2121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2124 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002125}
2126
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002127#ifdef FEAT_WINDOWS
2128/*
2129 * List tab page variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_tab_vars(first)
2133 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2136 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002137}
2138#endif
2139
Bram Moolenaara7043832005-01-21 11:56:39 +00002140/*
2141 * List Vim variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_vim_vars(first)
2145 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148}
2149
2150/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002151 * List script-local variables, if there is a script.
2152 */
2153 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_script_vars(first)
2155 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002156{
2157 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2159 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002160}
2161
2162/*
2163 * List function variables, if there is a function.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_func_vars(first)
2167 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168{
2169 if (current_funccal != NULL)
2170 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172}
2173
2174/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 * List variables in "arg".
2176 */
2177 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 exarg_T *eap;
2180 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
2183 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 char_u *name_start;
2187 char_u *arg_subsc;
2188 char_u *tofree;
2189 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190
2191 while (!ends_excmd(*arg) && !got_int)
2192 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002195 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2197 {
2198 emsg_severe = TRUE;
2199 EMSG(_(e_trailing));
2200 break;
2201 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 /* get_name_len() takes care of expanding curly braces */
2206 name_start = name = arg;
2207 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2208 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 /* This is mainly to keep test 49 working: when expanding
2211 * curly braces fails overrule the exception error message. */
2212 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 emsg_severe = TRUE;
2215 EMSG2(_(e_invarg2), arg);
2216 break;
2217 }
2218 error = TRUE;
2219 }
2220 else
2221 {
2222 if (tofree != NULL)
2223 name = tofree;
2224 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 else
2227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 /* handle d.key, l[idx], f(expr) */
2229 arg_subsc = arg;
2230 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002231 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 case 'g': list_glob_vars(first); break;
2239 case 'b': list_buf_vars(first); break;
2240 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 case 'v': list_vim_vars(first); break;
2245 case 's': list_script_vars(first); break;
2246 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 default:
2248 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 }
2251 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 {
2253 char_u numbuf[NUMBUFLEN];
2254 char_u *tf;
2255 int c;
2256 char_u *s;
2257
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002258 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 c = *arg;
2260 *arg = NUL;
2261 list_one_var_a((char_u *)"",
2262 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 tv.v_type,
2264 s == NULL ? (char_u *)"" : s,
2265 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 *arg = c;
2267 vim_free(tf);
2268 }
2269 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
2272 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273
2274 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276
2277 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279
2280 return arg;
2281}
2282
2283/*
2284 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2285 * Returns a pointer to the char just after the var name.
2286 * Returns NULL if there is an error.
2287 */
2288 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002291 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002293 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002294 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295{
2296 int c1;
2297 char_u *name;
2298 char_u *p;
2299 char_u *arg_end = NULL;
2300 int len;
2301 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303
2304 /*
2305 * ":let $VAR = expr": Set environment variable.
2306 */
2307 if (*arg == '$')
2308 {
2309 /* Find the end of the name. */
2310 ++arg;
2311 name = arg;
2312 len = get_env_len(&arg);
2313 if (len == 0)
2314 EMSG2(_(e_invarg2), name - 1);
2315 else
2316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 if (op != NULL && (*op == '+' || *op == '-'))
2318 EMSG2(_(e_letwrong), op);
2319 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002320 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 EMSG(_(e_letunexp));
2322 else
2323 {
2324 c1 = name[len];
2325 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 p = get_tv_string_chk(tv);
2327 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 {
2329 int mustfree = FALSE;
2330 char_u *s = vim_getenv(name, &mustfree);
2331
2332 if (s != NULL)
2333 {
2334 p = tofree = concat_str(s, p);
2335 if (mustfree)
2336 vim_free(s);
2337 }
2338 }
2339 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 if (STRICMP(name, "HOME") == 0)
2343 init_homedir();
2344 else if (didset_vim && STRICMP(name, "VIM") == 0)
2345 didset_vim = FALSE;
2346 else if (didset_vimruntime
2347 && STRICMP(name, "VIMRUNTIME") == 0)
2348 didset_vimruntime = FALSE;
2349 arg_end = arg;
2350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 }
2354 }
2355 }
2356
2357 /*
2358 * ":let &option = expr": Set option value.
2359 * ":let &l:option = expr": Set local option value.
2360 * ":let &g:option = expr": Set global option value.
2361 */
2362 else if (*arg == '&')
2363 {
2364 /* Find the end of the name. */
2365 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002366 if (p == NULL || (endchars != NULL
2367 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002368 EMSG(_(e_letunexp));
2369 else
2370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 long n;
2372 int opt_type;
2373 long numval;
2374 char_u *stringval = NULL;
2375 char_u *s;
2376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 c1 = *p;
2378 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379
2380 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 s = get_tv_string_chk(tv); /* != NULL if number or string */
2382 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 {
2384 opt_type = get_option_value(arg, &numval,
2385 &stringval, opt_flags);
2386 if ((opt_type == 1 && *op == '.')
2387 || (opt_type == 0 && *op != '.'))
2388 EMSG2(_(e_letwrong), op);
2389 else
2390 {
2391 if (opt_type == 1) /* number */
2392 {
2393 if (*op == '+')
2394 n = numval + n;
2395 else
2396 n = numval - n;
2397 }
2398 else if (opt_type == 0 && stringval != NULL) /* string */
2399 {
2400 s = concat_str(stringval, s);
2401 vim_free(stringval);
2402 stringval = s;
2403 }
2404 }
2405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 if (s != NULL)
2407 {
2408 set_option_value(arg, n, s, opt_flags);
2409 arg_end = p;
2410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 }
2414 }
2415
2416 /*
2417 * ":let @r = expr": Set register contents.
2418 */
2419 else if (*arg == '@')
2420 {
2421 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 if (op != NULL && (*op == '+' || *op == '-'))
2423 EMSG2(_(e_letwrong), op);
2424 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002425 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 EMSG(_(e_letunexp));
2427 else
2428 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002429 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 char_u *s;
2431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 p = get_tv_string_chk(tv);
2433 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002435 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 if (s != NULL)
2437 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002438 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_free(s);
2440 }
2441 }
2442 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 arg_end = arg + 1;
2446 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002447 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
2449 }
2450
2451 /*
2452 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002455 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002459 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2463 EMSG(_(e_letunexp));
2464 else
2465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 arg_end = p;
2468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
2472
2473 else
2474 EMSG2(_(e_invarg2), arg);
2475
2476 return arg_end;
2477}
2478
2479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2481 */
2482 static int
2483check_changedtick(arg)
2484 char_u *arg;
2485{
2486 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2487 {
2488 EMSG2(_(e_readonlyvar), arg);
2489 return TRUE;
2490 }
2491 return FALSE;
2492}
2493
2494/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 * Get an lval: variable, Dict item or List item that can be assigned a value
2496 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2497 * "name.key", "name.key[expr]" etc.
2498 * Indexing only works if "name" is an existing List or Dictionary.
2499 * "name" points to the start of the name.
2500 * If "rettv" is not NULL it points to the value to be assigned.
2501 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2502 * wrong; must end in space or cmd separator.
2503 *
2504 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002505 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 */
2508 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002509get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002511 typval_T *rettv;
2512 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 int unlet;
2514 int skip;
2515 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 char_u *expr_start, *expr_end;
2520 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 dictitem_T *v;
2522 typval_T var1;
2523 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532
2533 if (skip)
2534 {
2535 /* When skipping just find the end of the name. */
2536 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002537 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 }
2539
2540 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002541 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (expr_start != NULL)
2543 {
2544 /* Don't expand the name when we already know there is an error. */
2545 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2546 && *p != '[' && *p != '.')
2547 {
2548 EMSG(_(e_trailing));
2549 return NULL;
2550 }
2551
2552 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2553 if (lp->ll_exp_name == NULL)
2554 {
2555 /* Report an invalid expression in braces, unless the
2556 * expression evaluation has been cancelled due to an
2557 * aborting error, an interrupt, or an exception. */
2558 if (!aborting() && !quiet)
2559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002560 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 EMSG2(_(e_invarg2), name);
2562 return NULL;
2563 }
2564 }
2565 lp->ll_name = lp->ll_exp_name;
2566 }
2567 else
2568 lp->ll_name = name;
2569
2570 /* Without [idx] or .key we are done. */
2571 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2572 return p;
2573
2574 cc = *p;
2575 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002576 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (v == NULL && !quiet)
2578 EMSG2(_(e_undefvar), lp->ll_name);
2579 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 if (v == NULL)
2581 return NULL;
2582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /*
2584 * Loop until no more [idx] or .key is following.
2585 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2590 && !(lp->ll_tv->v_type == VAR_DICT
2591 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (!quiet)
2594 EMSG(_("E689: Can only index a List or Dictionary"));
2595 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!quiet)
2600 EMSG(_("E708: [:] must come last"));
2601 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002603
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 len = -1;
2605 if (*p == '.')
2606 {
2607 key = p + 1;
2608 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2609 ;
2610 if (len == 0)
2611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_(e_emptykey));
2614 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 }
2616 p = key + len;
2617 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002618 else
2619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 if (*p == ':')
2623 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 else
2625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 empty1 = FALSE;
2627 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002629 if (get_tv_string_chk(&var1) == NULL)
2630 {
2631 /* not a number or string */
2632 clear_tv(&var1);
2633 return NULL;
2634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 }
2636
2637 /* Optionally get the second index [ :expr]. */
2638 if (*p == ':')
2639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002643 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 if (!empty1)
2645 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (rettv != NULL && (rettv->v_type != VAR_LIST
2649 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!quiet)
2652 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 if (!empty1)
2654 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = skipwhite(p + 1);
2658 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 else
2661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var2) == NULL)
2670 {
2671 /* not a number or string */
2672 if (!empty1)
2673 clear_tv(&var1);
2674 clear_tv(&var2);
2675 return NULL;
2676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
2694 /* Skip to past ']'. */
2695 ++p;
2696 }
2697
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 {
2700 if (len == -1)
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (*key == NUL)
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
2707 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 lp->ll_list = NULL;
2713 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002714 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002717 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002721 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (len == -1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
2726 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (len == -1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 p = NULL;
2734 break;
2735 }
2736 if (len == -1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
2740 else
2741 {
2742 /*
2743 * Get the number and item for the only or first index of the List.
2744 */
2745 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
2748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002749 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 clear_tv(&var1);
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_list = lp->ll_tv->vval.v_list;
2754 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2755 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002757 if (lp->ll_n1 < 0)
2758 {
2759 lp->ll_n1 = 0;
2760 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2761 }
2762 }
2763 if (lp->ll_li == NULL)
2764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769
2770 /*
2771 * May need to find the item or absolute index for the second
2772 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 * When no index given: "lp->ll_empty2" is TRUE.
2774 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002778 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 }
2787
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2789 if (lp->ll_n1 < 0)
2790 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2791 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002797 }
2798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 return p;
2800}
2801
2802/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 */
2805 static void
2806clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808{
2809 vim_free(lp->ll_exp_name);
2810 vim_free(lp->ll_newkey);
2811}
2812
2813/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002814 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 */
2818 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002820 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002824 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825{
2826 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 listitem_T *ri;
2828 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829
2830 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002831 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 cc = *endp;
2835 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 if (op != NULL && *op != '=')
2837 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002838 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002839
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002840 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002841 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002842 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 {
2844 if (tv_op(&tv, rettv, op) == OK)
2845 set_var(lp->ll_name, &tv, FALSE);
2846 clear_tv(&tv);
2847 }
2848 }
2849 else
2850 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002854 else if (tv_check_lock(lp->ll_newkey == NULL
2855 ? lp->ll_tv->v_lock
2856 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2857 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 else if (lp->ll_range)
2859 {
2860 /*
2861 * Assign the List values to the list items.
2862 */
2863 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002864 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 if (op != NULL && *op != '=')
2866 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2867 else
2868 {
2869 clear_tv(&lp->ll_li->li_tv);
2870 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2871 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 ri = ri->li_next;
2873 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2874 break;
2875 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002878 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 ri = NULL;
2881 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 lp->ll_li = lp->ll_li->li_next;
2885 ++lp->ll_n1;
2886 }
2887 if (ri != NULL)
2888 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 else if (lp->ll_empty2
2890 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 : lp->ll_n1 != lp->ll_n2)
2892 EMSG(_("E711: List value has not enough items"));
2893 }
2894 else
2895 {
2896 /*
2897 * Assign to a List or Dictionary item.
2898 */
2899 if (lp->ll_newkey != NULL)
2900 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901 if (op != NULL && *op != '=')
2902 {
2903 EMSG2(_(e_letwrong), op);
2904 return;
2905 }
2906
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002908 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 if (di == NULL)
2910 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002911 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2912 {
2913 vim_free(di);
2914 return;
2915 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 else if (op != NULL && *op != '=')
2919 {
2920 tv_op(lp->ll_tv, rettv, op);
2921 return;
2922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002923 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 /*
2927 * Assign the value to the variable or list item.
2928 */
2929 if (copy)
2930 copy_tv(rettv, lp->ll_tv);
2931 else
2932 {
2933 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002934 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 }
2937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938}
2939
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2942 * Returns OK or FAIL.
2943 */
2944 static int
2945tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 typval_T *tv1;
2947 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 char_u *op;
2949{
2950 long n;
2951 char_u numbuf[NUMBUFLEN];
2952 char_u *s;
2953
2954 /* Can't do anything with a Funcref or a Dict on the right. */
2955 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2956 {
2957 switch (tv1->v_type)
2958 {
2959 case VAR_DICT:
2960 case VAR_FUNC:
2961 break;
2962
2963 case VAR_LIST:
2964 if (*op != '+' || tv2->v_type != VAR_LIST)
2965 break;
2966 /* List += List */
2967 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2968 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2969 return OK;
2970
2971 case VAR_NUMBER:
2972 case VAR_STRING:
2973 if (tv2->v_type == VAR_LIST)
2974 break;
2975 if (*op == '+' || *op == '-')
2976 {
2977 /* nr += nr or nr -= nr*/
2978 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979#ifdef FEAT_FLOAT
2980 if (tv2->v_type == VAR_FLOAT)
2981 {
2982 float_T f = n;
2983
2984 if (*op == '+')
2985 f += tv2->vval.v_float;
2986 else
2987 f -= tv2->vval.v_float;
2988 clear_tv(tv1);
2989 tv1->v_type = VAR_FLOAT;
2990 tv1->vval.v_float = f;
2991 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002993#endif
2994 {
2995 if (*op == '+')
2996 n += get_tv_number(tv2);
2997 else
2998 n -= get_tv_number(tv2);
2999 clear_tv(tv1);
3000 tv1->v_type = VAR_NUMBER;
3001 tv1->vval.v_number = n;
3002 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 }
3004 else
3005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 if (tv2->v_type == VAR_FLOAT)
3007 break;
3008
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 /* str .= str */
3010 s = get_tv_string(tv1);
3011 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3012 clear_tv(tv1);
3013 tv1->v_type = VAR_STRING;
3014 tv1->vval.v_string = s;
3015 }
3016 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017
3018#ifdef FEAT_FLOAT
3019 case VAR_FLOAT:
3020 {
3021 float_T f;
3022
3023 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3024 && tv2->v_type != VAR_NUMBER
3025 && tv2->v_type != VAR_STRING))
3026 break;
3027 if (tv2->v_type == VAR_FLOAT)
3028 f = tv2->vval.v_float;
3029 else
3030 f = get_tv_number(tv2);
3031 if (*op == '+')
3032 tv1->vval.v_float += f;
3033 else
3034 tv1->vval.v_float -= f;
3035 }
3036 return OK;
3037#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 }
3039 }
3040
3041 EMSG2(_(e_letwrong), op);
3042 return FAIL;
3043}
3044
3045/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046 * Add a watcher to a list.
3047 */
3048 static void
3049list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003050 list_T *l;
3051 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052{
3053 lw->lw_next = l->lv_watch;
3054 l->lv_watch = lw;
3055}
3056
3057/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003058 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059 * No warning when it isn't found...
3060 */
3061 static void
3062list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003063 list_T *l;
3064 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065{
Bram Moolenaar33570922005-01-25 22:26:29 +00003066 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003067
3068 lwp = &l->lv_watch;
3069 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3070 {
3071 if (lw == lwrem)
3072 {
3073 *lwp = lw->lw_next;
3074 break;
3075 }
3076 lwp = &lw->lw_next;
3077 }
3078}
3079
3080/*
3081 * Just before removing an item from a list: advance watchers to the next
3082 * item.
3083 */
3084 static void
3085list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003086 list_T *l;
3087 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088{
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090
3091 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3092 if (lw->lw_item == item)
3093 lw->lw_item = item->li_next;
3094}
3095
3096/*
3097 * Evaluate the expression used in a ":for var in expr" command.
3098 * "arg" points to "var".
3099 * Set "*errp" to TRUE for an error, FALSE otherwise;
3100 * Return a pointer that holds the info. Null when there is an error.
3101 */
3102 void *
3103eval_for_line(arg, errp, nextcmdp, skip)
3104 char_u *arg;
3105 int *errp;
3106 char_u **nextcmdp;
3107 int skip;
3108{
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 typval_T tv;
3112 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113
3114 *errp = TRUE; /* default: there is an error */
3115
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 if (fi == NULL)
3118 return NULL;
3119
3120 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3121 if (expr == NULL)
3122 return fi;
3123
3124 expr = skipwhite(expr);
3125 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3126 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003127 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128 return fi;
3129 }
3130
3131 if (skip)
3132 ++emsg_skip;
3133 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3134 {
3135 *errp = FALSE;
3136 if (!skip)
3137 {
3138 l = tv.vval.v_list;
3139 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003140 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003142 clear_tv(&tv);
3143 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003144 else
3145 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003146 /* No need to increment the refcount, it's already set for the
3147 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 fi->fi_list = l;
3149 list_add_watch(l, &fi->fi_lw);
3150 fi->fi_lw.lw_item = l->lv_first;
3151 }
3152 }
3153 }
3154 if (skip)
3155 --emsg_skip;
3156
3157 return fi;
3158}
3159
3160/*
3161 * Use the first item in a ":for" list. Advance to the next.
3162 * Assign the values to the variable (list). "arg" points to the first one.
3163 * Return TRUE when a valid item was found, FALSE when at end of list or
3164 * something wrong.
3165 */
3166 int
3167next_for_item(fi_void, arg)
3168 void *fi_void;
3169 char_u *arg;
3170{
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174
3175 item = fi->fi_lw.lw_item;
3176 if (item == NULL)
3177 result = FALSE;
3178 else
3179 {
3180 fi->fi_lw.lw_item = item->li_next;
3181 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3182 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3183 }
3184 return result;
3185}
3186
3187/*
3188 * Free the structure used to store info used by ":for".
3189 */
3190 void
3191free_for_info(fi_void)
3192 void *fi_void;
3193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003196 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 list_unref(fi->fi_list);
3200 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 vim_free(fi);
3202}
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3205
3206 void
3207set_context_for_expression(xp, arg, cmdidx)
3208 expand_T *xp;
3209 char_u *arg;
3210 cmdidx_T cmdidx;
3211{
3212 int got_eq = FALSE;
3213 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 if (cmdidx == CMD_let)
3217 {
3218 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003219 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 {
3221 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003222 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 {
3224 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 if (vim_iswhite(*p))
3227 break;
3228 }
3229 return;
3230 }
3231 }
3232 else
3233 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3234 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 while ((xp->xp_pattern = vim_strpbrk(arg,
3236 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3237 {
3238 c = *xp->xp_pattern;
3239 if (c == '&')
3240 {
3241 c = xp->xp_pattern[1];
3242 if (c == '&')
3243 {
3244 ++xp->xp_pattern;
3245 xp->xp_context = cmdidx != CMD_let || got_eq
3246 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3247 }
3248 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003251 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3252 xp->xp_pattern += 2;
3253
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256 else if (c == '$')
3257 {
3258 /* environment variable */
3259 xp->xp_context = EXPAND_ENV_VARS;
3260 }
3261 else if (c == '=')
3262 {
3263 got_eq = TRUE;
3264 xp->xp_context = EXPAND_EXPRESSION;
3265 }
3266 else if (c == '<'
3267 && xp->xp_context == EXPAND_FUNCTIONS
3268 && vim_strchr(xp->xp_pattern, '(') == NULL)
3269 {
3270 /* Function name can start with "<SNR>" */
3271 break;
3272 }
3273 else if (cmdidx != CMD_let || got_eq)
3274 {
3275 if (c == '"') /* string */
3276 {
3277 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3278 if (c == '\\' && xp->xp_pattern[1] != NUL)
3279 ++xp->xp_pattern;
3280 xp->xp_context = EXPAND_NOTHING;
3281 }
3282 else if (c == '\'') /* literal string */
3283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003284 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3286 /* skip */ ;
3287 xp->xp_context = EXPAND_NOTHING;
3288 }
3289 else if (c == '|')
3290 {
3291 if (xp->xp_pattern[1] == '|')
3292 {
3293 ++xp->xp_pattern;
3294 xp->xp_context = EXPAND_EXPRESSION;
3295 }
3296 else
3297 xp->xp_context = EXPAND_COMMANDS;
3298 }
3299 else
3300 xp->xp_context = EXPAND_EXPRESSION;
3301 }
3302 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 /* Doesn't look like something valid, expand as an expression
3304 * anyway. */
3305 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 arg = xp->xp_pattern;
3307 if (*arg != NUL)
3308 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3309 /* skip */ ;
3310 }
3311 xp->xp_pattern = arg;
3312}
3313
3314#endif /* FEAT_CMDL_COMPL */
3315
3316/*
3317 * ":1,25call func(arg1, arg2)" function call.
3318 */
3319 void
3320ex_call(eap)
3321 exarg_T *eap;
3322{
3323 char_u *arg = eap->arg;
3324 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003326 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 linenr_T lnum;
3330 int doesrange;
3331 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003334 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003335 if (fudi.fd_newkey != NULL)
3336 {
3337 /* Still need to give an error message for missing key. */
3338 EMSG2(_(e_dictkey), fudi.fd_newkey);
3339 vim_free(fudi.fd_newkey);
3340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003341 if (tofree == NULL)
3342 return;
3343
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003344 /* Increase refcount on dictionary, it could get deleted when evaluating
3345 * the arguments. */
3346 if (fudi.fd_dict != NULL)
3347 ++fudi.fd_dict->dv_refcount;
3348
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003349 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003350 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003351 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaar532c7802005-01-27 14:44:31 +00003353 /* Skip white space to allow ":call func ()". Not good, but required for
3354 * backward compatibility. */
3355 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003356 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 if (*startarg != '(')
3359 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003360 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 goto end;
3362 }
3363
3364 /*
3365 * When skipping, evaluate the function once, to find the end of the
3366 * arguments.
3367 * When the function takes a range, this is discovered after the first
3368 * call, and the loop is broken.
3369 */
3370 if (eap->skip)
3371 {
3372 ++emsg_skip;
3373 lnum = eap->line2; /* do it once, also with an invalid range */
3374 }
3375 else
3376 lnum = eap->line1;
3377 for ( ; lnum <= eap->line2; ++lnum)
3378 {
3379 if (!eap->skip && eap->addr_count > 0)
3380 {
3381 curwin->w_cursor.lnum = lnum;
3382 curwin->w_cursor.col = 0;
3383 }
3384 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003385 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003386 eap->line1, eap->line2, &doesrange,
3387 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
3389 failed = TRUE;
3390 break;
3391 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003392
3393 /* Handle a function returning a Funcref, Dictionary or List. */
3394 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3395 {
3396 failed = TRUE;
3397 break;
3398 }
3399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003400 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (doesrange || eap->skip)
3402 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003405 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (aborting())
3409 break;
3410 }
3411 if (eap->skip)
3412 --emsg_skip;
3413
3414 if (!failed)
3415 {
3416 /* Check for trailing illegal characters and a following command. */
3417 if (!ends_excmd(*arg))
3418 {
3419 emsg_severe = TRUE;
3420 EMSG(_(e_trailing));
3421 }
3422 else
3423 eap->nextcmd = check_nextcmd(arg);
3424 }
3425
3426end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429}
3430
3431/*
3432 * ":unlet[!] var1 ... " command.
3433 */
3434 void
3435ex_unlet(eap)
3436 exarg_T *eap;
3437{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003438 ex_unletlock(eap, eap->arg, 0);
3439}
3440
3441/*
3442 * ":lockvar" and ":unlockvar" commands
3443 */
3444 void
3445ex_lockvar(eap)
3446 exarg_T *eap;
3447{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003449 int deep = 2;
3450
3451 if (eap->forceit)
3452 deep = -1;
3453 else if (vim_isdigit(*arg))
3454 {
3455 deep = getdigits(&arg);
3456 arg = skipwhite(arg);
3457 }
3458
3459 ex_unletlock(eap, arg, deep);
3460}
3461
3462/*
3463 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3464 */
3465 static void
3466ex_unletlock(eap, argstart, deep)
3467 exarg_T *eap;
3468 char_u *argstart;
3469 int deep;
3470{
3471 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003474 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 do
3477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003479 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3480 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003481 if (lv.ll_name == NULL)
3482 error = TRUE; /* error but continue parsing */
3483 if (name_end == NULL || (!vim_iswhite(*name_end)
3484 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003486 if (name_end != NULL)
3487 {
3488 emsg_severe = TRUE;
3489 EMSG(_(e_trailing));
3490 }
3491 if (!(eap->skip || error))
3492 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 break;
3494 }
3495
3496 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003497 {
3498 if (eap->cmdidx == CMD_unlet)
3499 {
3500 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3501 error = TRUE;
3502 }
3503 else
3504 {
3505 if (do_lock_var(&lv, name_end, deep,
3506 eap->cmdidx == CMD_lockvar) == FAIL)
3507 error = TRUE;
3508 }
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003511 if (!eap->skip)
3512 clear_lval(&lv);
3513
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 arg = skipwhite(name_end);
3515 } while (!ends_excmd(*arg));
3516
3517 eap->nextcmd = check_nextcmd(arg);
3518}
3519
Bram Moolenaar8c711452005-01-14 21:53:12 +00003520 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003521do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003522 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003523 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003524 int forceit;
3525{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003526 int ret = OK;
3527 int cc;
3528
3529 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003531 cc = *name_end;
3532 *name_end = NUL;
3533
3534 /* Normal name or expanded name. */
3535 if (check_changedtick(lp->ll_name))
3536 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003537 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003540 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003541 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3542 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003543 else if (lp->ll_range)
3544 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003545 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546
3547 /* Delete a range of List items. */
3548 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3549 {
3550 li = lp->ll_li->li_next;
3551 listitem_remove(lp->ll_list, lp->ll_li);
3552 lp->ll_li = li;
3553 ++lp->ll_n1;
3554 }
3555 }
3556 else
3557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 /* unlet a List item. */
3560 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003563 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 }
3565
3566 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003567}
3568
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569/*
3570 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 */
3573 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 hashtab_T *ht;
3579 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003581 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 ht = find_var_ht(name, &varname);
3584 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 hi = hash_find(ht, varname);
3587 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003588 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003589 di = HI2DI(hi);
3590 if (var_check_fixed(di->di_flags, name)
3591 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003592 return FAIL;
3593 delete_var(ht, hi);
3594 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 if (forceit)
3598 return OK;
3599 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 return FAIL;
3601}
3602
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603/*
3604 * Lock or unlock variable indicated by "lp".
3605 * "deep" is the levels to go (-1 for unlimited);
3606 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3607 */
3608 static int
3609do_lock_var(lp, name_end, deep, lock)
3610 lval_T *lp;
3611 char_u *name_end;
3612 int deep;
3613 int lock;
3614{
3615 int ret = OK;
3616 int cc;
3617 dictitem_T *di;
3618
3619 if (deep == 0) /* nothing to do */
3620 return OK;
3621
3622 if (lp->ll_tv == NULL)
3623 {
3624 cc = *name_end;
3625 *name_end = NUL;
3626
3627 /* Normal name or expanded name. */
3628 if (check_changedtick(lp->ll_name))
3629 ret = FAIL;
3630 else
3631 {
3632 di = find_var(lp->ll_name, NULL);
3633 if (di == NULL)
3634 ret = FAIL;
3635 else
3636 {
3637 if (lock)
3638 di->di_flags |= DI_FLAGS_LOCK;
3639 else
3640 di->di_flags &= ~DI_FLAGS_LOCK;
3641 item_lock(&di->di_tv, deep, lock);
3642 }
3643 }
3644 *name_end = cc;
3645 }
3646 else if (lp->ll_range)
3647 {
3648 listitem_T *li = lp->ll_li;
3649
3650 /* (un)lock a range of List items. */
3651 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3652 {
3653 item_lock(&li->li_tv, deep, lock);
3654 li = li->li_next;
3655 ++lp->ll_n1;
3656 }
3657 }
3658 else if (lp->ll_list != NULL)
3659 /* (un)lock a List item. */
3660 item_lock(&lp->ll_li->li_tv, deep, lock);
3661 else
3662 /* un(lock) a Dictionary item. */
3663 item_lock(&lp->ll_di->di_tv, deep, lock);
3664
3665 return ret;
3666}
3667
3668/*
3669 * Lock or unlock an item. "deep" is nr of levels to go.
3670 */
3671 static void
3672item_lock(tv, deep, lock)
3673 typval_T *tv;
3674 int deep;
3675 int lock;
3676{
3677 static int recurse = 0;
3678 list_T *l;
3679 listitem_T *li;
3680 dict_T *d;
3681 hashitem_T *hi;
3682 int todo;
3683
3684 if (recurse >= DICT_MAXNEST)
3685 {
3686 EMSG(_("E743: variable nested too deep for (un)lock"));
3687 return;
3688 }
3689 if (deep == 0)
3690 return;
3691 ++recurse;
3692
3693 /* lock/unlock the item itself */
3694 if (lock)
3695 tv->v_lock |= VAR_LOCKED;
3696 else
3697 tv->v_lock &= ~VAR_LOCKED;
3698
3699 switch (tv->v_type)
3700 {
3701 case VAR_LIST:
3702 if ((l = tv->vval.v_list) != NULL)
3703 {
3704 if (lock)
3705 l->lv_lock |= VAR_LOCKED;
3706 else
3707 l->lv_lock &= ~VAR_LOCKED;
3708 if (deep < 0 || deep > 1)
3709 /* recursive: lock/unlock the items the List contains */
3710 for (li = l->lv_first; li != NULL; li = li->li_next)
3711 item_lock(&li->li_tv, deep - 1, lock);
3712 }
3713 break;
3714 case VAR_DICT:
3715 if ((d = tv->vval.v_dict) != NULL)
3716 {
3717 if (lock)
3718 d->dv_lock |= VAR_LOCKED;
3719 else
3720 d->dv_lock &= ~VAR_LOCKED;
3721 if (deep < 0 || deep > 1)
3722 {
3723 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003724 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3726 {
3727 if (!HASHITEM_EMPTY(hi))
3728 {
3729 --todo;
3730 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3731 }
3732 }
3733 }
3734 }
3735 }
3736 --recurse;
3737}
3738
Bram Moolenaara40058a2005-07-11 22:42:07 +00003739/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003740 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3741 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003742 */
3743 static int
3744tv_islocked(tv)
3745 typval_T *tv;
3746{
3747 return (tv->v_lock & VAR_LOCKED)
3748 || (tv->v_type == VAR_LIST
3749 && tv->vval.v_list != NULL
3750 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3751 || (tv->v_type == VAR_DICT
3752 && tv->vval.v_dict != NULL
3753 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3754}
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3757/*
3758 * Delete all "menutrans_" variables.
3759 */
3760 void
3761del_menutrans_vars()
3762{
Bram Moolenaar33570922005-01-25 22:26:29 +00003763 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
Bram Moolenaar33570922005-01-25 22:26:29 +00003766 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003767 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003769 {
3770 if (!HASHITEM_EMPTY(hi))
3771 {
3772 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3774 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 }
3776 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003777 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778}
3779#endif
3780
3781#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3782
3783/*
3784 * Local string buffer for the next two functions to store a variable name
3785 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3786 * get_user_var_name().
3787 */
3788
3789static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3790
3791static char_u *varnamebuf = NULL;
3792static int varnamebuflen = 0;
3793
3794/*
3795 * Function to concatenate a prefix and a variable name.
3796 */
3797 static char_u *
3798cat_prefix_varname(prefix, name)
3799 int prefix;
3800 char_u *name;
3801{
3802 int len;
3803
3804 len = (int)STRLEN(name) + 3;
3805 if (len > varnamebuflen)
3806 {
3807 vim_free(varnamebuf);
3808 len += 10; /* some additional space */
3809 varnamebuf = alloc(len);
3810 if (varnamebuf == NULL)
3811 {
3812 varnamebuflen = 0;
3813 return NULL;
3814 }
3815 varnamebuflen = len;
3816 }
3817 *varnamebuf = prefix;
3818 varnamebuf[1] = ':';
3819 STRCPY(varnamebuf + 2, name);
3820 return varnamebuf;
3821}
3822
3823/*
3824 * Function given to ExpandGeneric() to obtain the list of user defined
3825 * (global/buffer/window/built-in) variable names.
3826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 char_u *
3828get_user_var_name(xp, idx)
3829 expand_T *xp;
3830 int idx;
3831{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003832 static long_u gdone;
3833 static long_u bdone;
3834 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003835#ifdef FEAT_WINDOWS
3836 static long_u tdone;
3837#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003838 static int vidx;
3839 static hashitem_T *hi;
3840 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
3842 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003843 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003845#ifdef FEAT_WINDOWS
3846 tdone = 0;
3847#endif
3848 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003849
3850 /* Global variables */
3851 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003853 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003855 else
3856 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 while (HASHITEM_EMPTY(hi))
3858 ++hi;
3859 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3860 return cat_prefix_varname('g', hi->hi_key);
3861 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003863
3864 /* b: variables */
3865 ht = &curbuf->b_vars.dv_hashtab;
3866 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003868 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003870 else
3871 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003872 while (HASHITEM_EMPTY(hi))
3873 ++hi;
3874 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003878 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return (char_u *)"b:changedtick";
3880 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003881
3882 /* w: variables */
3883 ht = &curwin->w_vars.dv_hashtab;
3884 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003886 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003887 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003888 else
3889 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003890 while (HASHITEM_EMPTY(hi))
3891 ++hi;
3892 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003894
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895#ifdef FEAT_WINDOWS
3896 /* t: variables */
3897 ht = &curtab->tp_vars.dv_hashtab;
3898 if (tdone < ht->ht_used)
3899 {
3900 if (tdone++ == 0)
3901 hi = ht->ht_array;
3902 else
3903 ++hi;
3904 while (HASHITEM_EMPTY(hi))
3905 ++hi;
3906 return cat_prefix_varname('t', hi->hi_key);
3907 }
3908#endif
3909
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 /* v: variables */
3911 if (vidx < VV_LEN)
3912 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 vim_free(varnamebuf);
3915 varnamebuf = NULL;
3916 varnamebuflen = 0;
3917 return NULL;
3918}
3919
3920#endif /* FEAT_CMDL_COMPL */
3921
3922/*
3923 * types for expressions.
3924 */
3925typedef enum
3926{
3927 TYPE_UNKNOWN = 0
3928 , TYPE_EQUAL /* == */
3929 , TYPE_NEQUAL /* != */
3930 , TYPE_GREATER /* > */
3931 , TYPE_GEQUAL /* >= */
3932 , TYPE_SMALLER /* < */
3933 , TYPE_SEQUAL /* <= */
3934 , TYPE_MATCH /* =~ */
3935 , TYPE_NOMATCH /* !~ */
3936} exptype_T;
3937
3938/*
3939 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3942 */
3943
3944/*
3945 * Handle zero level expression.
3946 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003948 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * Return OK or FAIL.
3950 */
3951 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 char_u **nextcmd;
3956 int evaluate;
3957{
3958 int ret;
3959 char_u *p;
3960
3961 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 if (ret == FAIL || !ends_excmd(*p))
3964 {
3965 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 /*
3968 * Report the invalid expression unless the expression evaluation has
3969 * been cancelled due to an aborting error, an interrupt, or an
3970 * exception.
3971 */
3972 if (!aborting())
3973 EMSG2(_(e_invexpr2), arg);
3974 ret = FAIL;
3975 }
3976 if (nextcmd != NULL)
3977 *nextcmd = check_nextcmd(p);
3978
3979 return ret;
3980}
3981
3982/*
3983 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003984 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 *
3986 * "arg" must point to the first non-white of the expression.
3987 * "arg" is advanced to the next non-white after the recognized expression.
3988 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003989 * Note: "rettv.v_lock" is not set.
3990 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 * Return OK or FAIL.
3992 */
3993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003994eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 int evaluate;
3998{
3999 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
4002 /*
4003 * Get the first variable.
4004 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return FAIL;
4007
4008 if ((*arg)[0] == '?')
4009 {
4010 result = FALSE;
4011 if (evaluate)
4012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 int error = FALSE;
4014
4015 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 if (error)
4019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021
4022 /*
4023 * Get the second variable.
4024 */
4025 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return FAIL;
4028
4029 /*
4030 * Check for the ":".
4031 */
4032 if ((*arg)[0] != ':')
4033 {
4034 EMSG(_("E109: Missing ':' after '?'"));
4035 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return FAIL;
4038 }
4039
4040 /*
4041 * Get the third variable.
4042 */
4043 *arg = skipwhite(*arg + 1);
4044 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4045 {
4046 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return FAIL;
4049 }
4050 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053
4054 return OK;
4055}
4056
4057/*
4058 * Handle first level expression:
4059 * expr2 || expr2 || expr2 logical OR
4060 *
4061 * "arg" must point to the first non-white of the expression.
4062 * "arg" is advanced to the next non-white after the recognized expression.
4063 *
4064 * Return OK or FAIL.
4065 */
4066 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 int evaluate;
4071{
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 long result;
4074 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004075 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 /*
4078 * Get the first variable.
4079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return FAIL;
4082
4083 /*
4084 * Repeat until there is no following "||".
4085 */
4086 first = TRUE;
4087 result = FALSE;
4088 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4089 {
4090 if (evaluate && first)
4091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 if (error)
4096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 first = FALSE;
4098 }
4099
4100 /*
4101 * Get the second variable.
4102 */
4103 *arg = skipwhite(*arg + 2);
4104 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4105 return FAIL;
4106
4107 /*
4108 * Compute the result.
4109 */
4110 if (evaluate && !result)
4111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 if (error)
4116 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 if (evaluate)
4119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 rettv->v_type = VAR_NUMBER;
4121 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 }
4124
4125 return OK;
4126}
4127
4128/*
4129 * Handle second level expression:
4130 * expr3 && expr3 && expr3 logical AND
4131 *
4132 * "arg" must point to the first non-white of the expression.
4133 * "arg" is advanced to the next non-white after the recognized expression.
4134 *
4135 * Return OK or FAIL.
4136 */
4137 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 int evaluate;
4142{
Bram Moolenaar33570922005-01-25 22:26:29 +00004143 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 long result;
4145 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004146 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /*
4149 * Get the first variable.
4150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 return FAIL;
4153
4154 /*
4155 * Repeat until there is no following "&&".
4156 */
4157 first = TRUE;
4158 result = TRUE;
4159 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4160 {
4161 if (evaluate && first)
4162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 if (error)
4167 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 first = FALSE;
4169 }
4170
4171 /*
4172 * Get the second variable.
4173 */
4174 *arg = skipwhite(*arg + 2);
4175 if (eval4(arg, &var2, evaluate && result) == FAIL)
4176 return FAIL;
4177
4178 /*
4179 * Compute the result.
4180 */
4181 if (evaluate && result)
4182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 if (evaluate)
4190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 rettv->v_type = VAR_NUMBER;
4192 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 }
4195
4196 return OK;
4197}
4198
4199/*
4200 * Handle third level expression:
4201 * var1 == var2
4202 * var1 =~ var2
4203 * var1 != var2
4204 * var1 !~ var2
4205 * var1 > var2
4206 * var1 >= var2
4207 * var1 < var2
4208 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004209 * var1 is var2
4210 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 *
4212 * "arg" must point to the first non-white of the expression.
4213 * "arg" is advanced to the next non-white after the recognized expression.
4214 *
4215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int evaluate;
4222{
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 char_u *p;
4225 int i;
4226 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004227 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int len = 2;
4229 long n1, n2;
4230 char_u *s1, *s2;
4231 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4232 regmatch_T regmatch;
4233 int ic;
4234 char_u *save_cpo;
4235
4236 /*
4237 * Get the first variable.
4238 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FAIL;
4241
4242 p = *arg;
4243 switch (p[0])
4244 {
4245 case '=': if (p[1] == '=')
4246 type = TYPE_EQUAL;
4247 else if (p[1] == '~')
4248 type = TYPE_MATCH;
4249 break;
4250 case '!': if (p[1] == '=')
4251 type = TYPE_NEQUAL;
4252 else if (p[1] == '~')
4253 type = TYPE_NOMATCH;
4254 break;
4255 case '>': if (p[1] != '=')
4256 {
4257 type = TYPE_GREATER;
4258 len = 1;
4259 }
4260 else
4261 type = TYPE_GEQUAL;
4262 break;
4263 case '<': if (p[1] != '=')
4264 {
4265 type = TYPE_SMALLER;
4266 len = 1;
4267 }
4268 else
4269 type = TYPE_SEQUAL;
4270 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004271 case 'i': if (p[1] == 's')
4272 {
4273 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4274 len = 5;
4275 if (!vim_isIDc(p[len]))
4276 {
4277 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4278 type_is = TRUE;
4279 }
4280 }
4281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283
4284 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004285 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 */
4287 if (type != TYPE_UNKNOWN)
4288 {
4289 /* extra question mark appended: ignore case */
4290 if (p[len] == '?')
4291 {
4292 ic = TRUE;
4293 ++len;
4294 }
4295 /* extra '#' appended: match case */
4296 else if (p[len] == '#')
4297 {
4298 ic = FALSE;
4299 ++len;
4300 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004301 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 else
4303 ic = p_ic;
4304
4305 /*
4306 * Get the second variable.
4307 */
4308 *arg = skipwhite(p + len);
4309 if (eval5(arg, &var2, evaluate) == FAIL)
4310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313 }
4314
4315 if (evaluate)
4316 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004317 if (type_is && rettv->v_type != var2.v_type)
4318 {
4319 /* For "is" a different type always means FALSE, for "notis"
4320 * it means TRUE. */
4321 n1 = (type == TYPE_NEQUAL);
4322 }
4323 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4324 {
4325 if (type_is)
4326 {
4327 n1 = (rettv->v_type == var2.v_type
4328 && rettv->vval.v_list == var2.vval.v_list);
4329 if (type == TYPE_NEQUAL)
4330 n1 = !n1;
4331 }
4332 else if (rettv->v_type != var2.v_type
4333 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4334 {
4335 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004336 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004337 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004338 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 clear_tv(rettv);
4340 clear_tv(&var2);
4341 return FAIL;
4342 }
4343 else
4344 {
4345 /* Compare two Lists for being equal or unequal. */
4346 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4347 if (type == TYPE_NEQUAL)
4348 n1 = !n1;
4349 }
4350 }
4351
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004352 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4353 {
4354 if (type_is)
4355 {
4356 n1 = (rettv->v_type == var2.v_type
4357 && rettv->vval.v_dict == var2.vval.v_dict);
4358 if (type == TYPE_NEQUAL)
4359 n1 = !n1;
4360 }
4361 else if (rettv->v_type != var2.v_type
4362 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4363 {
4364 if (rettv->v_type != var2.v_type)
4365 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4366 else
4367 EMSG(_("E736: Invalid operation for Dictionary"));
4368 clear_tv(rettv);
4369 clear_tv(&var2);
4370 return FAIL;
4371 }
4372 else
4373 {
4374 /* Compare two Dictionaries for being equal or unequal. */
4375 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4376 if (type == TYPE_NEQUAL)
4377 n1 = !n1;
4378 }
4379 }
4380
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4382 {
4383 if (rettv->v_type != var2.v_type
4384 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4385 {
4386 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004387 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004388 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004389 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 clear_tv(rettv);
4391 clear_tv(&var2);
4392 return FAIL;
4393 }
4394 else
4395 {
4396 /* Compare two Funcrefs for being equal or unequal. */
4397 if (rettv->vval.v_string == NULL
4398 || var2.vval.v_string == NULL)
4399 n1 = FALSE;
4400 else
4401 n1 = STRCMP(rettv->vval.v_string,
4402 var2.vval.v_string) == 0;
4403 if (type == TYPE_NEQUAL)
4404 n1 = !n1;
4405 }
4406 }
4407
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408#ifdef FEAT_FLOAT
4409 /*
4410 * If one of the two variables is a float, compare as a float.
4411 * When using "=~" or "!~", always compare as string.
4412 */
4413 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4414 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4415 {
4416 float_T f1, f2;
4417
4418 if (rettv->v_type == VAR_FLOAT)
4419 f1 = rettv->vval.v_float;
4420 else
4421 f1 = get_tv_number(rettv);
4422 if (var2.v_type == VAR_FLOAT)
4423 f2 = var2.vval.v_float;
4424 else
4425 f2 = get_tv_number(&var2);
4426 n1 = FALSE;
4427 switch (type)
4428 {
4429 case TYPE_EQUAL: n1 = (f1 == f2); break;
4430 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4431 case TYPE_GREATER: n1 = (f1 > f2); break;
4432 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4433 case TYPE_SMALLER: n1 = (f1 < f2); break;
4434 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4435 case TYPE_UNKNOWN:
4436 case TYPE_MATCH:
4437 case TYPE_NOMATCH: break; /* avoid gcc warning */
4438 }
4439 }
4440#endif
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 /*
4443 * If one of the two variables is a number, compare as a number.
4444 * When using "=~" or "!~", always compare as string.
4445 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 n1 = get_tv_number(rettv);
4450 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 switch (type)
4452 {
4453 case TYPE_EQUAL: n1 = (n1 == n2); break;
4454 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4455 case TYPE_GREATER: n1 = (n1 > n2); break;
4456 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4457 case TYPE_SMALLER: n1 = (n1 < n2); break;
4458 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4459 case TYPE_UNKNOWN:
4460 case TYPE_MATCH:
4461 case TYPE_NOMATCH: break; /* avoid gcc warning */
4462 }
4463 }
4464 else
4465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004466 s1 = get_tv_string_buf(rettv, buf1);
4467 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4469 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4470 else
4471 i = 0;
4472 n1 = FALSE;
4473 switch (type)
4474 {
4475 case TYPE_EQUAL: n1 = (i == 0); break;
4476 case TYPE_NEQUAL: n1 = (i != 0); break;
4477 case TYPE_GREATER: n1 = (i > 0); break;
4478 case TYPE_GEQUAL: n1 = (i >= 0); break;
4479 case TYPE_SMALLER: n1 = (i < 0); break;
4480 case TYPE_SEQUAL: n1 = (i <= 0); break;
4481
4482 case TYPE_MATCH:
4483 case TYPE_NOMATCH:
4484 /* avoid 'l' flag in 'cpoptions' */
4485 save_cpo = p_cpo;
4486 p_cpo = (char_u *)"";
4487 regmatch.regprog = vim_regcomp(s2,
4488 RE_MAGIC + RE_STRING);
4489 regmatch.rm_ic = ic;
4490 if (regmatch.regprog != NULL)
4491 {
4492 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4493 vim_free(regmatch.regprog);
4494 if (type == TYPE_NOMATCH)
4495 n1 = !n1;
4496 }
4497 p_cpo = save_cpo;
4498 break;
4499
4500 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4501 }
4502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503 clear_tv(rettv);
4504 clear_tv(&var2);
4505 rettv->v_type = VAR_NUMBER;
4506 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 }
4509
4510 return OK;
4511}
4512
4513/*
4514 * Handle fourth level expression:
4515 * + number addition
4516 * - number subtraction
4517 * . string concatenation
4518 *
4519 * "arg" must point to the first non-white of the expression.
4520 * "arg" is advanced to the next non-white after the recognized expression.
4521 *
4522 * Return OK or FAIL.
4523 */
4524 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 int evaluate;
4529{
Bram Moolenaar33570922005-01-25 22:26:29 +00004530 typval_T var2;
4531 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 int op;
4533 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004534#ifdef FEAT_FLOAT
4535 float_T f1 = 0, f2 = 0;
4536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 char_u *s1, *s2;
4538 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4539 char_u *p;
4540
4541 /*
4542 * Get the first variable.
4543 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004544 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return FAIL;
4546
4547 /*
4548 * Repeat computing, until no '+', '-' or '.' is following.
4549 */
4550 for (;;)
4551 {
4552 op = **arg;
4553 if (op != '+' && op != '-' && op != '.')
4554 break;
4555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556 if ((op != '+' || rettv->v_type != VAR_LIST)
4557#ifdef FEAT_FLOAT
4558 && (op == '.' || rettv->v_type != VAR_FLOAT)
4559#endif
4560 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 {
4562 /* For "list + ...", an illegal use of the first operand as
4563 * a number cannot be determined before evaluating the 2nd
4564 * operand: if this is also a list, all is ok.
4565 * For "something . ...", "something - ..." or "non-list + ...",
4566 * we know that the first operand needs to be a string or number
4567 * without evaluating the 2nd operand. So check before to avoid
4568 * side effects after an error. */
4569 if (evaluate && get_tv_string_chk(rettv) == NULL)
4570 {
4571 clear_tv(rettv);
4572 return FAIL;
4573 }
4574 }
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 /*
4577 * Get the second variable.
4578 */
4579 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004580 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 return FAIL;
4584 }
4585
4586 if (evaluate)
4587 {
4588 /*
4589 * Compute the result.
4590 */
4591 if (op == '.')
4592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004593 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4594 s2 = get_tv_string_buf_chk(&var2, buf2);
4595 if (s2 == NULL) /* type error ? */
4596 {
4597 clear_tv(rettv);
4598 clear_tv(&var2);
4599 return FAIL;
4600 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004601 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 clear_tv(rettv);
4603 rettv->v_type = VAR_STRING;
4604 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004606 else if (op == '+' && rettv->v_type == VAR_LIST
4607 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 {
4609 /* concatenate Lists */
4610 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4611 &var3) == FAIL)
4612 {
4613 clear_tv(rettv);
4614 clear_tv(&var2);
4615 return FAIL;
4616 }
4617 clear_tv(rettv);
4618 *rettv = var3;
4619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 else
4621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004622 int error = FALSE;
4623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004624#ifdef FEAT_FLOAT
4625 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004627 f1 = rettv->vval.v_float;
4628 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 else
4631#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 n1 = get_tv_number_chk(rettv, &error);
4634 if (error)
4635 {
4636 /* This can only happen for "list + non-list". For
4637 * "non-list + ..." or "something - ...", we returned
4638 * before evaluating the 2nd operand. */
4639 clear_tv(rettv);
4640 return FAIL;
4641 }
4642#ifdef FEAT_FLOAT
4643 if (var2.v_type == VAR_FLOAT)
4644 f1 = n1;
4645#endif
4646 }
4647#ifdef FEAT_FLOAT
4648 if (var2.v_type == VAR_FLOAT)
4649 {
4650 f2 = var2.vval.v_float;
4651 n2 = 0;
4652 }
4653 else
4654#endif
4655 {
4656 n2 = get_tv_number_chk(&var2, &error);
4657 if (error)
4658 {
4659 clear_tv(rettv);
4660 clear_tv(&var2);
4661 return FAIL;
4662 }
4663#ifdef FEAT_FLOAT
4664 if (rettv->v_type == VAR_FLOAT)
4665 f2 = n2;
4666#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004669
4670#ifdef FEAT_FLOAT
4671 /* If there is a float on either side the result is a float. */
4672 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4673 {
4674 if (op == '+')
4675 f1 = f1 + f2;
4676 else
4677 f1 = f1 - f2;
4678 rettv->v_type = VAR_FLOAT;
4679 rettv->vval.v_float = f1;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682#endif
4683 {
4684 if (op == '+')
4685 n1 = n1 + n2;
4686 else
4687 n1 = n1 - n2;
4688 rettv->v_type = VAR_NUMBER;
4689 rettv->vval.v_number = n1;
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 }
4695 return OK;
4696}
4697
4698/*
4699 * Handle fifth level expression:
4700 * * number multiplication
4701 * / number division
4702 * % number modulo
4703 *
4704 * "arg" must point to the first non-white of the expression.
4705 * "arg" is advanced to the next non-white after the recognized expression.
4706 *
4707 * Return OK or FAIL.
4708 */
4709 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004710eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004714 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
Bram Moolenaar33570922005-01-25 22:26:29 +00004716 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 int op;
4718 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719#ifdef FEAT_FLOAT
4720 int use_float = FALSE;
4721 float_T f1 = 0, f2;
4722#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 /*
4726 * Get the first variable.
4727 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004728 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FAIL;
4730
4731 /*
4732 * Repeat computing, until no '*', '/' or '%' is following.
4733 */
4734 for (;;)
4735 {
4736 op = **arg;
4737 if (op != '*' && op != '/' && op != '%')
4738 break;
4739
4740 if (evaluate)
4741 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004742#ifdef FEAT_FLOAT
4743 if (rettv->v_type == VAR_FLOAT)
4744 {
4745 f1 = rettv->vval.v_float;
4746 use_float = TRUE;
4747 n1 = 0;
4748 }
4749 else
4750#endif
4751 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 if (error)
4754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 else
4757 n1 = 0;
4758
4759 /*
4760 * Get the second variable.
4761 */
4762 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004763 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 return FAIL;
4765
4766 if (evaluate)
4767 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768#ifdef FEAT_FLOAT
4769 if (var2.v_type == VAR_FLOAT)
4770 {
4771 if (!use_float)
4772 {
4773 f1 = n1;
4774 use_float = TRUE;
4775 }
4776 f2 = var2.vval.v_float;
4777 n2 = 0;
4778 }
4779 else
4780#endif
4781 {
4782 n2 = get_tv_number_chk(&var2, &error);
4783 clear_tv(&var2);
4784 if (error)
4785 return FAIL;
4786#ifdef FEAT_FLOAT
4787 if (use_float)
4788 f2 = n2;
4789#endif
4790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 /*
4793 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004799 if (op == '*')
4800 f1 = f1 * f2;
4801 else if (op == '/')
4802 {
4803 /* We rely on the floating point library to handle divide
4804 * by zero to result in "inf" and not a crash. */
4805 f1 = f1 / f2;
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004809 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004810 return FAIL;
4811 }
4812 rettv->v_type = VAR_FLOAT;
4813 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818 if (op == '*')
4819 n1 = n1 * n2;
4820 else if (op == '/')
4821 {
4822 if (n2 == 0) /* give an error message? */
4823 {
4824 if (n1 == 0)
4825 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4826 else if (n1 < 0)
4827 n1 = -0x7fffffffL;
4828 else
4829 n1 = 0x7fffffffL;
4830 }
4831 else
4832 n1 = n1 / n2;
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835 {
4836 if (n2 == 0) /* give an error message? */
4837 n1 = 0;
4838 else
4839 n1 = n1 % n2;
4840 }
4841 rettv->v_type = VAR_NUMBER;
4842 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 }
4846
4847 return OK;
4848}
4849
4850/*
4851 * Handle sixth level expression:
4852 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004853 * "string" string constant
4854 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 * &option-name option value
4856 * @r register contents
4857 * identifier variable value
4858 * function() function call
4859 * $VAR environment variable
4860 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004861 * [expr, expr] List
4862 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 *
4864 * Also handle:
4865 * ! in front logical NOT
4866 * - in front unary minus
4867 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004868 * trailing [] subscript in String or List
4869 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 *
4871 * "arg" must point to the first non-white of the expression.
4872 * "arg" is advanced to the next non-white after the recognized expression.
4873 *
4874 * Return OK or FAIL.
4875 */
4876 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004877eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004881 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 long n;
4884 int len;
4885 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 char_u *start_leader, *end_leader;
4887 int ret = OK;
4888 char_u *alias;
4889
4890 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 /*
4897 * Skip '!' and '-' characters. They are handled later.
4898 */
4899 start_leader = *arg;
4900 while (**arg == '!' || **arg == '-' || **arg == '+')
4901 *arg = skipwhite(*arg + 1);
4902 end_leader = *arg;
4903
4904 switch (**arg)
4905 {
4906 /*
4907 * Number constant.
4908 */
4909 case '0':
4910 case '1':
4911 case '2':
4912 case '3':
4913 case '4':
4914 case '5':
4915 case '6':
4916 case '7':
4917 case '8':
4918 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 {
4920#ifdef FEAT_FLOAT
4921 char_u *p = skipdigits(*arg + 1);
4922 int get_float = FALSE;
4923
4924 /* We accept a float when the format matches
4925 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004926 * strict to avoid backwards compatibility problems.
4927 * Don't look for a float after the "." operator, so that
4928 * ":let vers = 1.2.3" doesn't fail. */
4929 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 get_float = TRUE;
4932 p = skipdigits(p + 2);
4933 if (*p == 'e' || *p == 'E')
4934 {
4935 ++p;
4936 if (*p == '-' || *p == '+')
4937 ++p;
4938 if (!vim_isdigit(*p))
4939 get_float = FALSE;
4940 else
4941 p = skipdigits(p + 1);
4942 }
4943 if (ASCII_ISALPHA(*p) || *p == '.')
4944 get_float = FALSE;
4945 }
4946 if (get_float)
4947 {
4948 float_T f;
4949
4950 *arg += string2float(*arg, &f);
4951 if (evaluate)
4952 {
4953 rettv->v_type = VAR_FLOAT;
4954 rettv->vval.v_float = f;
4955 }
4956 }
4957 else
4958#endif
4959 {
4960 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4961 *arg += len;
4962 if (evaluate)
4963 {
4964 rettv->v_type = VAR_NUMBER;
4965 rettv->vval.v_number = n;
4966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /*
4972 * String constant: "string".
4973 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004974 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 break;
4976
4977 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 break;
4982
4983 /*
4984 * List: [expr, expr]
4985 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988
4989 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 * Dictionary: {key: val, key: val}
4991 */
4992 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4993 break;
4994
4995 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004996 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004998 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000
5001 /*
5002 * Environment variable: $VAR.
5003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 break;
5006
5007 /*
5008 * Register contents: @r.
5009 */
5010 case '@': ++*arg;
5011 if (evaluate)
5012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005014 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 if (**arg != NUL)
5017 ++*arg;
5018 break;
5019
5020 /*
5021 * nested expression: (expression).
5022 */
5023 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005024 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (**arg == ')')
5026 ++*arg;
5027 else if (ret == OK)
5028 {
5029 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005030 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 ret = FAIL;
5032 }
5033 break;
5034
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 break;
5037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038
5039 if (ret == NOTDONE)
5040 {
5041 /*
5042 * Must be a variable or function name.
5043 * Can also be a curly-braces kind of name: {expr}.
5044 */
5045 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005046 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 if (alias != NULL)
5048 s = alias;
5049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005050 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 ret = FAIL;
5052 else
5053 {
5054 if (**arg == '(') /* recursive! */
5055 {
5056 /* If "s" is the name of a variable of type VAR_FUNC
5057 * use its contents. */
5058 s = deref_func_name(s, &len);
5059
5060 /* Invoke the function. */
5061 ret = get_func_tv(s, len, rettv, arg,
5062 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005063 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 /* Stop the expression evaluation when immediately
5065 * aborting on error, or when an interrupt occurred or
5066 * an exception was thrown but not caught. */
5067 if (aborting())
5068 {
5069 if (ret == OK)
5070 clear_tv(rettv);
5071 ret = FAIL;
5072 }
5073 }
5074 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005075 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005076 else
5077 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 }
5079
5080 if (alias != NULL)
5081 vim_free(alias);
5082 }
5083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 *arg = skipwhite(*arg);
5085
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005086 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5087 * expr(expr). */
5088 if (ret == OK)
5089 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 /*
5092 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5093 */
5094 if (ret == OK && evaluate && end_leader > start_leader)
5095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005096 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 int val = 0;
5098#ifdef FEAT_FLOAT
5099 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 if (rettv->v_type == VAR_FLOAT)
5102 f = rettv->vval.v_float;
5103 else
5104#endif
5105 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005106 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 clear_tv(rettv);
5109 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005111 else
5112 {
5113 while (end_leader > start_leader)
5114 {
5115 --end_leader;
5116 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 {
5118#ifdef FEAT_FLOAT
5119 if (rettv->v_type == VAR_FLOAT)
5120 f = !f;
5121 else
5122#endif
5123 val = !val;
5124 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005125 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005126 {
5127#ifdef FEAT_FLOAT
5128 if (rettv->v_type == VAR_FLOAT)
5129 f = -f;
5130 else
5131#endif
5132 val = -val;
5133 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005134 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135#ifdef FEAT_FLOAT
5136 if (rettv->v_type == VAR_FLOAT)
5137 {
5138 clear_tv(rettv);
5139 rettv->vval.v_float = f;
5140 }
5141 else
5142#endif
5143 {
5144 clear_tv(rettv);
5145 rettv->v_type = VAR_NUMBER;
5146 rettv->vval.v_number = val;
5147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150
5151 return ret;
5152}
5153
5154/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005155 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5156 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5158 */
5159 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005162 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165{
5166 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005167 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 long len = -1;
5170 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005174 if (rettv->v_type == VAR_FUNC
5175#ifdef FEAT_FLOAT
5176 || rettv->v_type == VAR_FLOAT
5177#endif
5178 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005180 if (verbose)
5181 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 return FAIL;
5183 }
5184
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 /*
5188 * dict.name
5189 */
5190 key = *arg + 1;
5191 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5192 ;
5193 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 }
5197 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 /*
5200 * something[idx]
5201 *
5202 * Get the (first) variable from inside the [].
5203 */
5204 *arg = skipwhite(*arg + 1);
5205 if (**arg == ':')
5206 empty1 = TRUE;
5207 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5208 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5210 {
5211 /* not a number or string */
5212 clear_tv(&var1);
5213 return FAIL;
5214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215
5216 /*
5217 * Get the second variable from inside the [:].
5218 */
5219 if (**arg == ':')
5220 {
5221 range = TRUE;
5222 *arg = skipwhite(*arg + 1);
5223 if (**arg == ']')
5224 empty2 = TRUE;
5225 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 if (!empty1)
5228 clear_tv(&var1);
5229 return FAIL;
5230 }
5231 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5232 {
5233 /* not a number or string */
5234 if (!empty1)
5235 clear_tv(&var1);
5236 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 return FAIL;
5238 }
5239 }
5240
5241 /* Check for the ']'. */
5242 if (**arg != ']')
5243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (verbose)
5245 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 clear_tv(&var1);
5247 if (range)
5248 clear_tv(&var2);
5249 return FAIL;
5250 }
5251 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 }
5253
5254 if (evaluate)
5255 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 n1 = 0;
5257 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259 n1 = get_tv_number(&var1);
5260 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 }
5262 if (range)
5263 {
5264 if (empty2)
5265 n2 = -1;
5266 else
5267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005268 n2 = get_tv_number(&var2);
5269 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 }
5271 }
5272
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005273 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 {
5275 case VAR_NUMBER:
5276 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 len = (long)STRLEN(s);
5279 if (range)
5280 {
5281 /* The resulting variable is a substring. If the indexes
5282 * are out of range the result is empty. */
5283 if (n1 < 0)
5284 {
5285 n1 = len + n1;
5286 if (n1 < 0)
5287 n1 = 0;
5288 }
5289 if (n2 < 0)
5290 n2 = len + n2;
5291 else if (n2 >= len)
5292 n2 = len;
5293 if (n1 >= len || n2 < 0 || n1 > n2)
5294 s = NULL;
5295 else
5296 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5297 }
5298 else
5299 {
5300 /* The resulting variable is a string of a single
5301 * character. If the index is too big or negative the
5302 * result is empty. */
5303 if (n1 >= len || n1 < 0)
5304 s = NULL;
5305 else
5306 s = vim_strnsave(s + n1, 1);
5307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 clear_tv(rettv);
5309 rettv->v_type = VAR_STRING;
5310 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 break;
5312
5313 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005314 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 if (n1 < 0)
5316 n1 = len + n1;
5317 if (!empty1 && (n1 < 0 || n1 >= len))
5318 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005319 /* For a range we allow invalid values and return an empty
5320 * list. A list index out of range is an error. */
5321 if (!range)
5322 {
5323 if (verbose)
5324 EMSGN(_(e_listidx), n1);
5325 return FAIL;
5326 }
5327 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 }
5329 if (range)
5330 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005331 list_T *l;
5332 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333
5334 if (n2 < 0)
5335 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005336 else if (n2 >= len)
5337 n2 = len - 1;
5338 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005339 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 l = list_alloc();
5341 if (l == NULL)
5342 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005343 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 n1 <= n2; ++n1)
5345 {
5346 if (list_append_tv(l, &item->li_tv) == FAIL)
5347 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005348 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 return FAIL;
5350 }
5351 item = item->li_next;
5352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 clear_tv(rettv);
5354 rettv->v_type = VAR_LIST;
5355 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005356 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 else
5359 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005360 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 clear_tv(rettv);
5362 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 }
5364 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365
5366 case VAR_DICT:
5367 if (range)
5368 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005369 if (verbose)
5370 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 if (len == -1)
5372 clear_tv(&var1);
5373 return FAIL;
5374 }
5375 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005376 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377
5378 if (len == -1)
5379 {
5380 key = get_tv_string(&var1);
5381 if (*key == NUL)
5382 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005383 if (verbose)
5384 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 clear_tv(&var1);
5386 return FAIL;
5387 }
5388 }
5389
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005390 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005392 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005393 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 if (len == -1)
5395 clear_tv(&var1);
5396 if (item == NULL)
5397 return FAIL;
5398
5399 copy_tv(&item->di_tv, &var1);
5400 clear_tv(rettv);
5401 *rettv = var1;
5402 }
5403 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 }
5406
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 return OK;
5408}
5409
5410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 * Get an option value.
5412 * "arg" points to the '&' or '+' before the option name.
5413 * "arg" is advanced to character after the option name.
5414 * Return OK or FAIL.
5415 */
5416 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 int evaluate;
5421{
5422 char_u *option_end;
5423 long numval;
5424 char_u *stringval;
5425 int opt_type;
5426 int c;
5427 int working = (**arg == '+'); /* has("+option") */
5428 int ret = OK;
5429 int opt_flags;
5430
5431 /*
5432 * Isolate the option name and find its value.
5433 */
5434 option_end = find_option_end(arg, &opt_flags);
5435 if (option_end == NULL)
5436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 EMSG2(_("E112: Option name missing: %s"), *arg);
5439 return FAIL;
5440 }
5441
5442 if (!evaluate)
5443 {
5444 *arg = option_end;
5445 return OK;
5446 }
5447
5448 c = *option_end;
5449 *option_end = NUL;
5450 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453 if (opt_type == -3) /* invalid name */
5454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 EMSG2(_("E113: Unknown option: %s"), *arg);
5457 ret = FAIL;
5458 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 if (opt_type == -2) /* hidden string option */
5462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 rettv->v_type = VAR_STRING;
5464 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466 else if (opt_type == -1) /* hidden number option */
5467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 rettv->v_type = VAR_NUMBER;
5469 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471 else if (opt_type == 1) /* number option */
5472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 rettv->v_type = VAR_NUMBER;
5474 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476 else /* string option */
5477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 rettv->v_type = VAR_STRING;
5479 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481 }
5482 else if (working && (opt_type == -2 || opt_type == -1))
5483 ret = FAIL;
5484
5485 *option_end = c; /* put back for error messages */
5486 *arg = option_end;
5487
5488 return ret;
5489}
5490
5491/*
5492 * Allocate a variable for a string constant.
5493 * Return OK or FAIL.
5494 */
5495 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 int evaluate;
5500{
5501 char_u *p;
5502 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 int extra = 0;
5504
5505 /*
5506 * Find the end of the string, skipping backslashed characters.
5507 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005508 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 if (*p == '\\' && p[1] != NUL)
5511 {
5512 ++p;
5513 /* A "\<x>" form occupies at least 4 characters, and produces up
5514 * to 6 characters: reserve space for 2 extra */
5515 if (*p == '<')
5516 extra += 2;
5517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
5519
5520 if (*p != '"')
5521 {
5522 EMSG2(_("E114: Missing quote: %s"), *arg);
5523 return FAIL;
5524 }
5525
5526 /* If only parsing, set *arg and return here */
5527 if (!evaluate)
5528 {
5529 *arg = p + 1;
5530 return OK;
5531 }
5532
5533 /*
5534 * Copy the string into allocated memory, handling backslashed
5535 * characters.
5536 */
5537 name = alloc((unsigned)(p - *arg + extra));
5538 if (name == NULL)
5539 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 rettv->v_type = VAR_STRING;
5541 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
5545 if (*p == '\\')
5546 {
5547 switch (*++p)
5548 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005549 case 'b': *name++ = BS; ++p; break;
5550 case 'e': *name++ = ESC; ++p; break;
5551 case 'f': *name++ = FF; ++p; break;
5552 case 'n': *name++ = NL; ++p; break;
5553 case 'r': *name++ = CAR; ++p; break;
5554 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
5556 case 'X': /* hex: "\x1", "\x12" */
5557 case 'x':
5558 case 'u': /* Unicode: "\u0023" */
5559 case 'U':
5560 if (vim_isxdigit(p[1]))
5561 {
5562 int n, nr;
5563 int c = toupper(*p);
5564
5565 if (c == 'X')
5566 n = 2;
5567 else
5568 n = 4;
5569 nr = 0;
5570 while (--n >= 0 && vim_isxdigit(p[1]))
5571 {
5572 ++p;
5573 nr = (nr << 4) + hex2nr(*p);
5574 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576#ifdef FEAT_MBYTE
5577 /* For "\u" store the number according to
5578 * 'encoding'. */
5579 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 else
5582#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 break;
5586
5587 /* octal: "\1", "\12", "\123" */
5588 case '0':
5589 case '1':
5590 case '2':
5591 case '3':
5592 case '4':
5593 case '5':
5594 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 case '7': *name = *p++ - '0';
5596 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 *name = (*name << 3) + *p++ - '0';
5599 if (*p >= '0' && *p <= '7')
5600 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 break;
5604
5605 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (extra != 0)
5608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 break;
5611 }
5612 /* FALLTHROUGH */
5613
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 break;
5616 }
5617 }
5618 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 *arg = p + 1;
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 return OK;
5626}
5627
5628/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 * Return OK or FAIL.
5631 */
5632 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 int evaluate;
5637{
5638 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005639 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 int reduce = 0;
5641
5642 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005644 */
5645 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5646 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005650 break;
5651 ++reduce;
5652 ++p;
5653 }
5654 }
5655
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005659 return FAIL;
5660 }
5661
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005663 if (!evaluate)
5664 {
5665 *arg = p + 1;
5666 return OK;
5667 }
5668
5669 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671 */
5672 str = alloc((unsigned)((p - *arg) - reduce));
5673 if (str == NULL)
5674 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005677
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683 break;
5684 ++p;
5685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 *arg = p + 1;
5690
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005691 return OK;
5692}
5693
5694/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 * Allocate a variable for a List and fill it from "*arg".
5696 * Return OK or FAIL.
5697 */
5698 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005699get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 int evaluate;
5703{
Bram Moolenaar33570922005-01-25 22:26:29 +00005704 list_T *l = NULL;
5705 typval_T tv;
5706 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707
5708 if (evaluate)
5709 {
5710 l = list_alloc();
5711 if (l == NULL)
5712 return FAIL;
5713 }
5714
5715 *arg = skipwhite(*arg + 1);
5716 while (**arg != ']' && **arg != NUL)
5717 {
5718 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5719 goto failret;
5720 if (evaluate)
5721 {
5722 item = listitem_alloc();
5723 if (item != NULL)
5724 {
5725 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005726 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 list_append(l, item);
5728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005729 else
5730 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 }
5732
5733 if (**arg == ']')
5734 break;
5735 if (**arg != ',')
5736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 goto failret;
5739 }
5740 *arg = skipwhite(*arg + 1);
5741 }
5742
5743 if (**arg != ']')
5744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746failret:
5747 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005748 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 return FAIL;
5750 }
5751
5752 *arg = skipwhite(*arg + 1);
5753 if (evaluate)
5754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005755 rettv->v_type = VAR_LIST;
5756 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 ++l->lv_refcount;
5758 }
5759
5760 return OK;
5761}
5762
5763/*
5764 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005765 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005767 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768list_alloc()
5769{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005770 list_T *l;
5771
5772 l = (list_T *)alloc_clear(sizeof(list_T));
5773 if (l != NULL)
5774 {
5775 /* Prepend the list to the list of lists for garbage collection. */
5776 if (first_list != NULL)
5777 first_list->lv_used_prev = l;
5778 l->lv_used_prev = NULL;
5779 l->lv_used_next = first_list;
5780 first_list = l;
5781 }
5782 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783}
5784
5785/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005786 * Allocate an empty list for a return value.
5787 * Returns OK or FAIL.
5788 */
5789 static int
5790rettv_list_alloc(rettv)
5791 typval_T *rettv;
5792{
5793 list_T *l = list_alloc();
5794
5795 if (l == NULL)
5796 return FAIL;
5797
5798 rettv->vval.v_list = l;
5799 rettv->v_type = VAR_LIST;
5800 ++l->lv_refcount;
5801 return OK;
5802}
5803
5804/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 * Unreference a list: decrement the reference count and free it when it
5806 * becomes zero.
5807 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005808 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005812 if (l != NULL && --l->lv_refcount <= 0)
5813 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814}
5815
5816/*
5817 * Free a list, including all items it points to.
5818 * Ignores the reference count.
5819 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005820 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005821list_free(l, recurse)
5822 list_T *l;
5823 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824{
Bram Moolenaar33570922005-01-25 22:26:29 +00005825 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005827 /* Remove the list from the list of lists for garbage collection. */
5828 if (l->lv_used_prev == NULL)
5829 first_list = l->lv_used_next;
5830 else
5831 l->lv_used_prev->lv_used_next = l->lv_used_next;
5832 if (l->lv_used_next != NULL)
5833 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5834
Bram Moolenaard9fba312005-06-26 22:34:35 +00005835 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005837 /* Remove the item before deleting it. */
5838 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005839 if (recurse || (item->li_tv.v_type != VAR_LIST
5840 && item->li_tv.v_type != VAR_DICT))
5841 clear_tv(&item->li_tv);
5842 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 }
5844 vim_free(l);
5845}
5846
5847/*
5848 * Allocate a list item.
5849 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851listitem_alloc()
5852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854}
5855
5856/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005857 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 */
5859 static void
5860listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005861 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005863 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 vim_free(item);
5865}
5866
5867/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005868 * Remove a list item from a List and free it. Also clears the value.
5869 */
5870 static void
5871listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005872 list_T *l;
5873 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005874{
5875 list_remove(l, item, item);
5876 listitem_free(item);
5877}
5878
5879/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 * Get the number of items in a list.
5881 */
5882 static long
5883list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005884 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 if (l == NULL)
5887 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889}
5890
5891/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005892 * Return TRUE when two lists have exactly the same values.
5893 */
5894 static int
5895list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005896 list_T *l1;
5897 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898 int ic; /* ignore case for strings */
5899{
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005901
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005902 if (l1 == NULL || l2 == NULL)
5903 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005904 if (l1 == l2)
5905 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005906 if (list_len(l1) != list_len(l2))
5907 return FALSE;
5908
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005909 for (item1 = l1->lv_first, item2 = l2->lv_first;
5910 item1 != NULL && item2 != NULL;
5911 item1 = item1->li_next, item2 = item2->li_next)
5912 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5913 return FALSE;
5914 return item1 == NULL && item2 == NULL;
5915}
5916
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005917#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5918 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005919/*
5920 * Return the dictitem that an entry in a hashtable points to.
5921 */
5922 dictitem_T *
5923dict_lookup(hi)
5924 hashitem_T *hi;
5925{
5926 return HI2DI(hi);
5927}
5928#endif
5929
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005931 * Return TRUE when two dictionaries have exactly the same key/values.
5932 */
5933 static int
5934dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005935 dict_T *d1;
5936 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005937 int ic; /* ignore case for strings */
5938{
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 hashitem_T *hi;
5940 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005941 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005942
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005943 if (d1 == NULL || d2 == NULL)
5944 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005945 if (d1 == d2)
5946 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005947 if (dict_len(d1) != dict_len(d2))
5948 return FALSE;
5949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005950 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005953 if (!HASHITEM_EMPTY(hi))
5954 {
5955 item2 = dict_find(d2, hi->hi_key, -1);
5956 if (item2 == NULL)
5957 return FALSE;
5958 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5959 return FALSE;
5960 --todo;
5961 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005962 }
5963 return TRUE;
5964}
5965
5966/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005968 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005969 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005970 */
5971 static int
5972tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 typval_T *tv1;
5974 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005975 int ic; /* ignore case */
5976{
5977 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005978 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005979 static int recursive = 0; /* cach recursive loops */
5980 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005981
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005982 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005983 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005984 /* Catch lists and dicts that have an endless loop by limiting
5985 * recursiveness to 1000. We guess they are equal then. */
5986 if (recursive >= 1000)
5987 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005988
5989 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005991 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005992 ++recursive;
5993 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5994 --recursive;
5995 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005996
5997 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005998 ++recursive;
5999 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6000 --recursive;
6001 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006002
6003 case VAR_FUNC:
6004 return (tv1->vval.v_string != NULL
6005 && tv2->vval.v_string != NULL
6006 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6007
6008 case VAR_NUMBER:
6009 return tv1->vval.v_number == tv2->vval.v_number;
6010
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006011#ifdef FEAT_FLOAT
6012 case VAR_FLOAT:
6013 return tv1->vval.v_float == tv2->vval.v_float;
6014#endif
6015
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006016 case VAR_STRING:
6017 s1 = get_tv_string_buf(tv1, buf1);
6018 s2 = get_tv_string_buf(tv2, buf2);
6019 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006020 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006021
6022 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006023 return TRUE;
6024}
6025
6026/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 * Locate item with index "n" in list "l" and return it.
6028 * A negative index is counted from the end; -1 is the last item.
6029 * Returns NULL when "n" is out of range.
6030 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 long n;
6035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 long idx;
6038
6039 if (l == NULL)
6040 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006041
6042 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006044 n = l->lv_len + n;
6045
6046 /* Check for index out of range. */
6047 if (n < 0 || n >= l->lv_len)
6048 return NULL;
6049
6050 /* When there is a cached index may start search from there. */
6051 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006053 if (n < l->lv_idx / 2)
6054 {
6055 /* closest to the start of the list */
6056 item = l->lv_first;
6057 idx = 0;
6058 }
6059 else if (n > (l->lv_idx + l->lv_len) / 2)
6060 {
6061 /* closest to the end of the list */
6062 item = l->lv_last;
6063 idx = l->lv_len - 1;
6064 }
6065 else
6066 {
6067 /* closest to the cached index */
6068 item = l->lv_idx_item;
6069 idx = l->lv_idx;
6070 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 }
6072 else
6073 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006074 if (n < l->lv_len / 2)
6075 {
6076 /* closest to the start of the list */
6077 item = l->lv_first;
6078 idx = 0;
6079 }
6080 else
6081 {
6082 /* closest to the end of the list */
6083 item = l->lv_last;
6084 idx = l->lv_len - 1;
6085 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006087
6088 while (n > idx)
6089 {
6090 /* search forward */
6091 item = item->li_next;
6092 ++idx;
6093 }
6094 while (n < idx)
6095 {
6096 /* search backward */
6097 item = item->li_prev;
6098 --idx;
6099 }
6100
6101 /* cache the used index */
6102 l->lv_idx = idx;
6103 l->lv_idx_item = item;
6104
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006105 return item;
6106}
6107
6108/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006109 * Get list item "l[idx]" as a number.
6110 */
6111 static long
6112list_find_nr(l, idx, errorp)
6113 list_T *l;
6114 long idx;
6115 int *errorp; /* set to TRUE when something wrong */
6116{
6117 listitem_T *li;
6118
6119 li = list_find(l, idx);
6120 if (li == NULL)
6121 {
6122 if (errorp != NULL)
6123 *errorp = TRUE;
6124 return -1L;
6125 }
6126 return get_tv_number_chk(&li->li_tv, errorp);
6127}
6128
6129/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006130 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6131 */
6132 char_u *
6133list_find_str(l, idx)
6134 list_T *l;
6135 long idx;
6136{
6137 listitem_T *li;
6138
6139 li = list_find(l, idx - 1);
6140 if (li == NULL)
6141 {
6142 EMSGN(_(e_listidx), idx);
6143 return NULL;
6144 }
6145 return get_tv_string(&li->li_tv);
6146}
6147
6148/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006149 * Locate "item" list "l" and return its index.
6150 * Returns -1 when "item" is not in the list.
6151 */
6152 static long
6153list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 list_T *l;
6155 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006156{
6157 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006159
6160 if (l == NULL)
6161 return -1;
6162 idx = 0;
6163 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6164 ++idx;
6165 if (li == NULL)
6166 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006167 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006168}
6169
6170/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 * Append item "item" to the end of list "l".
6172 */
6173 static void
6174list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 list_T *l;
6176 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
6178 if (l->lv_last == NULL)
6179 {
6180 /* empty list */
6181 l->lv_first = item;
6182 l->lv_last = item;
6183 item->li_prev = NULL;
6184 }
6185 else
6186 {
6187 l->lv_last->li_next = item;
6188 item->li_prev = l->lv_last;
6189 l->lv_last = item;
6190 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006191 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 item->li_next = NULL;
6193}
6194
6195/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006199 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 list_T *l;
6202 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006204 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205
Bram Moolenaar05159a02005-02-26 23:04:13 +00006206 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006207 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208 copy_tv(tv, &li->li_tv);
6209 list_append(l, li);
6210 return OK;
6211}
6212
6213/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006214 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006215 * Return FAIL when out of memory.
6216 */
6217 int
6218list_append_dict(list, dict)
6219 list_T *list;
6220 dict_T *dict;
6221{
6222 listitem_T *li = listitem_alloc();
6223
6224 if (li == NULL)
6225 return FAIL;
6226 li->li_tv.v_type = VAR_DICT;
6227 li->li_tv.v_lock = 0;
6228 li->li_tv.vval.v_dict = dict;
6229 list_append(list, li);
6230 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 return OK;
6232}
6233
6234/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006235 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006236 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006237 * Returns FAIL when out of memory.
6238 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006239 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006240list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006241 list_T *l;
6242 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006243 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006244{
6245 listitem_T *li = listitem_alloc();
6246
6247 if (li == NULL)
6248 return FAIL;
6249 list_append(l, li);
6250 li->li_tv.v_type = VAR_STRING;
6251 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006252 if (str == NULL)
6253 li->li_tv.vval.v_string = NULL;
6254 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006255 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006256 return FAIL;
6257 return OK;
6258}
6259
6260/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006261 * Append "n" to list "l".
6262 * Returns FAIL when out of memory.
6263 */
6264 static int
6265list_append_number(l, n)
6266 list_T *l;
6267 varnumber_T n;
6268{
6269 listitem_T *li;
6270
6271 li = listitem_alloc();
6272 if (li == NULL)
6273 return FAIL;
6274 li->li_tv.v_type = VAR_NUMBER;
6275 li->li_tv.v_lock = 0;
6276 li->li_tv.vval.v_number = n;
6277 list_append(l, li);
6278 return OK;
6279}
6280
6281/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006283 * If "item" is NULL append at the end.
6284 * Return FAIL when out of memory.
6285 */
6286 static int
6287list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 list_T *l;
6289 typval_T *tv;
6290 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006291{
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006293
6294 if (ni == NULL)
6295 return FAIL;
6296 copy_tv(tv, &ni->li_tv);
6297 if (item == NULL)
6298 /* Append new item at end of list. */
6299 list_append(l, ni);
6300 else
6301 {
6302 /* Insert new item before existing item. */
6303 ni->li_prev = item->li_prev;
6304 ni->li_next = item;
6305 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 ++l->lv_idx;
6309 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006310 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006311 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 l->lv_idx_item = NULL;
6314 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006315 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006316 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006317 }
6318 return OK;
6319}
6320
6321/*
6322 * Extend "l1" with "l2".
6323 * If "bef" is NULL append at the end, otherwise insert before this item.
6324 * Returns FAIL when out of memory.
6325 */
6326 static int
6327list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 list_T *l1;
6329 list_T *l2;
6330 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331{
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006333 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006335 /* We also quit the loop when we have inserted the original item count of
6336 * the list, avoid a hang when we extend a list with itself. */
6337 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6339 return FAIL;
6340 return OK;
6341}
6342
6343/*
6344 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6345 * Return FAIL when out of memory.
6346 */
6347 static int
6348list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 list_T *l1;
6350 list_T *l2;
6351 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352{
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006354
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006355 if (l1 == NULL || l2 == NULL)
6356 return FAIL;
6357
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006358 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006359 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 if (l == NULL)
6361 return FAIL;
6362 tv->v_type = VAR_LIST;
6363 tv->vval.v_list = l;
6364
6365 /* append all items from the second list */
6366 return list_extend(l, l2, NULL);
6367}
6368
6369/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006370 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006372 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 * Returns NULL when out of memory.
6374 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006379 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380{
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 list_T *copy;
6382 listitem_T *item;
6383 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384
6385 if (orig == NULL)
6386 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387
6388 copy = list_alloc();
6389 if (copy != NULL)
6390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006391 if (copyID != 0)
6392 {
6393 /* Do this before adding the items, because one of the items may
6394 * refer back to this list. */
6395 orig->lv_copyID = copyID;
6396 orig->lv_copylist = copy;
6397 }
6398 for (item = orig->lv_first; item != NULL && !got_int;
6399 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 {
6401 ni = listitem_alloc();
6402 if (ni == NULL)
6403 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006405 {
6406 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6407 {
6408 vim_free(ni);
6409 break;
6410 }
6411 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006413 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 list_append(copy, ni);
6415 }
6416 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006417 if (item != NULL)
6418 {
6419 list_unref(copy);
6420 copy = NULL;
6421 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 }
6423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 return copy;
6425}
6426
6427/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006429 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006432list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 list_T *l;
6434 listitem_T *item;
6435 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436{
Bram Moolenaar33570922005-01-25 22:26:29 +00006437 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439 /* notify watchers */
6440 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 list_fix_watch(l, ip);
6444 if (ip == item2)
6445 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447
6448 if (item2->li_next == NULL)
6449 l->lv_last = item->li_prev;
6450 else
6451 item2->li_next->li_prev = item->li_prev;
6452 if (item->li_prev == NULL)
6453 l->lv_first = item2->li_next;
6454 else
6455 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457}
6458
6459/*
6460 * Return an allocated string with the string representation of a list.
6461 * May return NULL.
6462 */
6463 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006464list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006466 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467{
6468 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469
6470 if (tv->vval.v_list == NULL)
6471 return NULL;
6472 ga_init2(&ga, (int)sizeof(char), 80);
6473 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006474 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006475 {
6476 vim_free(ga.ga_data);
6477 return NULL;
6478 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 ga_append(&ga, ']');
6480 ga_append(&ga, NUL);
6481 return (char_u *)ga.ga_data;
6482}
6483
6484/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006485 * Join list "l" into a string in "*gap", using separator "sep".
6486 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006487 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006490list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006491 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 char_u *sep;
6494 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006495 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006496{
6497 int first = TRUE;
6498 char_u *tofree;
6499 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006501 char_u *s;
6502
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006504 {
6505 if (first)
6506 first = FALSE;
6507 else
6508 ga_concat(gap, sep);
6509
6510 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006511 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006513 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006514 if (s != NULL)
6515 ga_concat(gap, s);
6516 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 if (s == NULL)
6518 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006519 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006520 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006521 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006522}
6523
6524/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006525 * Garbage collection for lists and dictionaries.
6526 *
6527 * We use reference counts to be able to free most items right away when they
6528 * are no longer used. But for composite items it's possible that it becomes
6529 * unused while the reference count is > 0: When there is a recursive
6530 * reference. Example:
6531 * :let l = [1, 2, 3]
6532 * :let d = {9: l}
6533 * :let l[1] = d
6534 *
6535 * Since this is quite unusual we handle this with garbage collection: every
6536 * once in a while find out which lists and dicts are not referenced from any
6537 * variable.
6538 *
6539 * Here is a good reference text about garbage collection (refers to Python
6540 * but it applies to all reference-counting mechanisms):
6541 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006542 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006543
6544/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006545 * Do garbage collection for lists and dicts.
6546 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006547 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006548 int
6549garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006550{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006551 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006552 buf_T *buf;
6553 win_T *wp;
6554 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006555 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006556 int did_free;
6557 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006558#ifdef FEAT_WINDOWS
6559 tabpage_T *tp;
6560#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006561
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006562 /* Only do this once. */
6563 want_garbage_collect = FALSE;
6564 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006565 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006566
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006567 /* We advance by two because we add one for items referenced through
6568 * previous_funccal. */
6569 current_copyID += COPYID_INC;
6570 copyID = current_copyID;
6571
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006572 /*
6573 * 1. Go through all accessible variables and mark all lists and dicts
6574 * with copyID.
6575 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006576
6577 /* Don't free variables in the previous_funccal list unless they are only
6578 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006579 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006580 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6581 {
6582 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6583 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6584 }
6585
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006586 /* script-local variables */
6587 for (i = 1; i <= ga_scripts.ga_len; ++i)
6588 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6589
6590 /* buffer-local variables */
6591 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6592 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6593
6594 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006595 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006596 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6597
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006598#ifdef FEAT_WINDOWS
6599 /* tabpage-local variables */
6600 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6601 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6602#endif
6603
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006604 /* global variables */
6605 set_ref_in_ht(&globvarht, copyID);
6606
6607 /* function-local variables */
6608 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6609 {
6610 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6611 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6612 }
6613
Bram Moolenaard812df62008-11-09 12:46:09 +00006614 /* v: vars */
6615 set_ref_in_ht(&vimvarht, copyID);
6616
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006617 /*
6618 * 2. Free lists and dictionaries that are not referenced.
6619 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006620 did_free = free_unref_items(copyID);
6621
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006622 /*
6623 * 3. Check if any funccal can be freed now.
6624 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006625 for (pfc = &previous_funccal; *pfc != NULL; )
6626 {
6627 if (can_free_funccal(*pfc, copyID))
6628 {
6629 fc = *pfc;
6630 *pfc = fc->caller;
6631 free_funccal(fc, TRUE);
6632 did_free = TRUE;
6633 did_free_funccal = TRUE;
6634 }
6635 else
6636 pfc = &(*pfc)->caller;
6637 }
6638 if (did_free_funccal)
6639 /* When a funccal was freed some more items might be garbage
6640 * collected, so run again. */
6641 (void)garbage_collect();
6642
6643 return did_free;
6644}
6645
6646/*
6647 * Free lists and dictionaries that are no longer referenced.
6648 */
6649 static int
6650free_unref_items(copyID)
6651 int copyID;
6652{
6653 dict_T *dd;
6654 list_T *ll;
6655 int did_free = FALSE;
6656
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006657 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006658 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 */
6660 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006661 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006662 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006663 /* Free the Dictionary and ordinary items it contains, but don't
6664 * recurse into Lists and Dictionaries, they will be in the list
6665 * of dicts or list of lists. */
6666 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006667 did_free = TRUE;
6668
6669 /* restart, next dict may also have been freed */
6670 dd = first_dict;
6671 }
6672 else
6673 dd = dd->dv_used_next;
6674
6675 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006676 * Go through the list of lists and free items without the copyID.
6677 * But don't free a list that has a watcher (used in a for loop), these
6678 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006679 */
6680 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006681 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6682 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006684 /* Free the List and ordinary items it contains, but don't recurse
6685 * into Lists and Dictionaries, they will be in the list of dicts
6686 * or list of lists. */
6687 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 did_free = TRUE;
6689
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006690 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006691 ll = first_list;
6692 }
6693 else
6694 ll = ll->lv_used_next;
6695
6696 return did_free;
6697}
6698
6699/*
6700 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6701 */
6702 static void
6703set_ref_in_ht(ht, copyID)
6704 hashtab_T *ht;
6705 int copyID;
6706{
6707 int todo;
6708 hashitem_T *hi;
6709
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006710 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006711 for (hi = ht->ht_array; todo > 0; ++hi)
6712 if (!HASHITEM_EMPTY(hi))
6713 {
6714 --todo;
6715 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6716 }
6717}
6718
6719/*
6720 * Mark all lists and dicts referenced through list "l" with "copyID".
6721 */
6722 static void
6723set_ref_in_list(l, copyID)
6724 list_T *l;
6725 int copyID;
6726{
6727 listitem_T *li;
6728
6729 for (li = l->lv_first; li != NULL; li = li->li_next)
6730 set_ref_in_item(&li->li_tv, copyID);
6731}
6732
6733/*
6734 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6735 */
6736 static void
6737set_ref_in_item(tv, copyID)
6738 typval_T *tv;
6739 int copyID;
6740{
6741 dict_T *dd;
6742 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743
6744 switch (tv->v_type)
6745 {
6746 case VAR_DICT:
6747 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006748 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006749 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 /* Didn't see this dict yet. */
6751 dd->dv_copyID = copyID;
6752 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006753 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756 case VAR_LIST:
6757 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006758 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 /* Didn't see this list yet. */
6761 ll->lv_copyID = copyID;
6762 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006763 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767}
6768
6769/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 * Allocate an empty header for a dictionary.
6771 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006772 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773dict_alloc()
6774{
Bram Moolenaar33570922005-01-25 22:26:29 +00006775 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006776
Bram Moolenaar33570922005-01-25 22:26:29 +00006777 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006778 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006779 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006780 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 if (first_dict != NULL)
6782 first_dict->dv_used_prev = d;
6783 d->dv_used_next = first_dict;
6784 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006785 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786
Bram Moolenaar33570922005-01-25 22:26:29 +00006787 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006788 d->dv_lock = 0;
6789 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006790 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006791 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006792 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006793}
6794
6795/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006796 * Allocate an empty dict for a return value.
6797 * Returns OK or FAIL.
6798 */
6799 static int
6800rettv_dict_alloc(rettv)
6801 typval_T *rettv;
6802{
6803 dict_T *d = dict_alloc();
6804
6805 if (d == NULL)
6806 return FAIL;
6807
6808 rettv->vval.v_dict = d;
6809 rettv->v_type = VAR_DICT;
6810 ++d->dv_refcount;
6811 return OK;
6812}
6813
6814
6815/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006816 * Unreference a Dictionary: decrement the reference count and free it when it
6817 * becomes zero.
6818 */
6819 static void
6820dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006823 if (d != NULL && --d->dv_refcount <= 0)
6824 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825}
6826
6827/*
6828 * Free a Dictionary, including all items it contains.
6829 * Ignores the reference count.
6830 */
6831 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006832dict_free(d, recurse)
6833 dict_T *d;
6834 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006835{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006836 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 /* Remove the dict from the list of dicts for garbage collection. */
6841 if (d->dv_used_prev == NULL)
6842 first_dict = d->dv_used_next;
6843 else
6844 d->dv_used_prev->dv_used_next = d->dv_used_next;
6845 if (d->dv_used_next != NULL)
6846 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6847
6848 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006850 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006851 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006852 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006853 if (!HASHITEM_EMPTY(hi))
6854 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 /* Remove the item before deleting it, just in case there is
6856 * something recursive causing trouble. */
6857 di = HI2DI(hi);
6858 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006859 if (recurse || (di->di_tv.v_type != VAR_LIST
6860 && di->di_tv.v_type != VAR_DICT))
6861 clear_tv(&di->di_tv);
6862 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006863 --todo;
6864 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006865 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006867 vim_free(d);
6868}
6869
6870/*
6871 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006872 * The "key" is copied to the new item.
6873 * Note that the value of the item "di_tv" still needs to be initialized!
6874 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006875 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006876 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006877dictitem_alloc(key)
6878 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006879{
Bram Moolenaar33570922005-01-25 22:26:29 +00006880 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006881
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006882 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006883 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006886 di->di_flags = 0;
6887 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006888 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889}
6890
6891/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892 * Make a copy of a Dictionary item.
6893 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006895dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897{
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006900 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6901 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 if (di != NULL)
6903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006904 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006905 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006906 copy_tv(&org->di_tv, &di->di_tv);
6907 }
6908 return di;
6909}
6910
6911/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006912 * Remove item "item" from Dictionary "dict" and free it.
6913 */
6914 static void
6915dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 dict_T *dict;
6917 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006918{
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006920
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922 if (HASHITEM_EMPTY(hi))
6923 EMSG2(_(e_intern2), "dictitem_remove()");
6924 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926 dictitem_free(item);
6927}
6928
6929/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930 * Free a dict item. Also clears the value.
6931 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006932 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006934 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936 clear_tv(&item->di_tv);
6937 vim_free(item);
6938}
6939
6940/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006941 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6942 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006943 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006944 * Returns NULL when out of memory.
6945 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006947dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006949 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006950 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951{
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 dict_T *copy;
6953 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006954 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006956
6957 if (orig == NULL)
6958 return NULL;
6959
6960 copy = dict_alloc();
6961 if (copy != NULL)
6962 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006963 if (copyID != 0)
6964 {
6965 orig->dv_copyID = copyID;
6966 orig->dv_copydict = copy;
6967 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006968 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006969 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006973 --todo;
6974
6975 di = dictitem_alloc(hi->hi_key);
6976 if (di == NULL)
6977 break;
6978 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006979 {
6980 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6981 copyID) == FAIL)
6982 {
6983 vim_free(di);
6984 break;
6985 }
6986 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006987 else
6988 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6989 if (dict_add(copy, di) == FAIL)
6990 {
6991 dictitem_free(di);
6992 break;
6993 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006995 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006996
Bram Moolenaare9a41262005-01-15 22:18:47 +00006997 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 if (todo > 0)
6999 {
7000 dict_unref(copy);
7001 copy = NULL;
7002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007003 }
7004
7005 return copy;
7006}
7007
7008/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007010 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007012 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 dict_T *d;
7015 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016{
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007018}
7019
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007021 * Add a number or string entry to dictionary "d".
7022 * When "str" is NULL use number "nr", otherwise use "str".
7023 * Returns FAIL when out of memory and when key already exists.
7024 */
7025 int
7026dict_add_nr_str(d, key, nr, str)
7027 dict_T *d;
7028 char *key;
7029 long nr;
7030 char_u *str;
7031{
7032 dictitem_T *item;
7033
7034 item = dictitem_alloc((char_u *)key);
7035 if (item == NULL)
7036 return FAIL;
7037 item->di_tv.v_lock = 0;
7038 if (str == NULL)
7039 {
7040 item->di_tv.v_type = VAR_NUMBER;
7041 item->di_tv.vval.v_number = nr;
7042 }
7043 else
7044 {
7045 item->di_tv.v_type = VAR_STRING;
7046 item->di_tv.vval.v_string = vim_strsave(str);
7047 }
7048 if (dict_add(d, item) == FAIL)
7049 {
7050 dictitem_free(item);
7051 return FAIL;
7052 }
7053 return OK;
7054}
7055
7056/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007057 * Add a list entry to dictionary "d".
7058 * Returns FAIL when out of memory and when key already exists.
7059 */
7060 int
7061dict_add_list(d, key, list)
7062 dict_T *d;
7063 char *key;
7064 list_T *list;
7065{
7066 dictitem_T *item;
7067
7068 item = dictitem_alloc((char_u *)key);
7069 if (item == NULL)
7070 return FAIL;
7071 item->di_tv.v_lock = 0;
7072 item->di_tv.v_type = VAR_LIST;
7073 item->di_tv.vval.v_list = list;
7074 if (dict_add(d, item) == FAIL)
7075 {
7076 dictitem_free(item);
7077 return FAIL;
7078 }
7079 return OK;
7080}
7081
7082/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007083 * Get the number of items in a Dictionary.
7084 */
7085 static long
7086dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007087 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007088{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 if (d == NULL)
7090 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007091 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092}
7093
7094/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095 * Find item "key[len]" in Dictionary "d".
7096 * If "len" is negative use strlen(key).
7097 * Returns NULL when not found.
7098 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007099 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007100dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 char_u *key;
7103 int len;
7104{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105#define AKEYLEN 200
7106 char_u buf[AKEYLEN];
7107 char_u *akey;
7108 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 if (len < 0)
7112 akey = key;
7113 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 tofree = akey = vim_strnsave(key, len);
7116 if (akey == NULL)
7117 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007118 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 else
7120 {
7121 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007122 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 akey = buf;
7124 }
7125
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 vim_free(tofree);
7128 if (HASHITEM_EMPTY(hi))
7129 return NULL;
7130 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131}
7132
7133/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007134 * Get a string item from a dictionary.
7135 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007136 * Returns NULL if the entry doesn't exist or out of memory.
7137 */
7138 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007139get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007140 dict_T *d;
7141 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007142 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007143{
7144 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007145 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007146
7147 di = dict_find(d, key, -1);
7148 if (di == NULL)
7149 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007150 s = get_tv_string(&di->di_tv);
7151 if (save && s != NULL)
7152 s = vim_strsave(s);
7153 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007154}
7155
7156/*
7157 * Get a number item from a dictionary.
7158 * Returns 0 if the entry doesn't exist or out of memory.
7159 */
7160 long
7161get_dict_number(d, key)
7162 dict_T *d;
7163 char_u *key;
7164{
7165 dictitem_T *di;
7166
7167 di = dict_find(d, key, -1);
7168 if (di == NULL)
7169 return 0;
7170 return get_tv_number(&di->di_tv);
7171}
7172
7173/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 * Return an allocated string with the string representation of a Dictionary.
7175 * May return NULL.
7176 */
7177 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007178dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007180 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181{
7182 garray_T ga;
7183 int first = TRUE;
7184 char_u *tofree;
7185 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 return NULL;
7193 ga_init2(&ga, (int)sizeof(char), 80);
7194 ga_append(&ga, '{');
7195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007196 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201 --todo;
7202
7203 if (first)
7204 first = FALSE;
7205 else
7206 ga_concat(&ga, (char_u *)", ");
7207
7208 tofree = string_quote(hi->hi_key, FALSE);
7209 if (tofree != NULL)
7210 {
7211 ga_concat(&ga, tofree);
7212 vim_free(tofree);
7213 }
7214 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007215 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 if (s != NULL)
7217 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007219 if (s == NULL)
7220 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007223 if (todo > 0)
7224 {
7225 vim_free(ga.ga_data);
7226 return NULL;
7227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228
7229 ga_append(&ga, '}');
7230 ga_append(&ga, NUL);
7231 return (char_u *)ga.ga_data;
7232}
7233
7234/*
7235 * Allocate a variable for a Dictionary and fill it from "*arg".
7236 * Return OK or FAIL. Returns NOTDONE for {expr}.
7237 */
7238 static int
7239get_dict_tv(arg, rettv, evaluate)
7240 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 int evaluate;
7243{
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 dict_T *d = NULL;
7245 typval_T tvkey;
7246 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007247 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251
7252 /*
7253 * First check if it's not a curly-braces thing: {expr}.
7254 * Must do this without evaluating, otherwise a function may be called
7255 * twice. Unfortunately this means we need to call eval1() twice for the
7256 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007257 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 if (*start != '}')
7260 {
7261 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7262 return FAIL;
7263 if (*start == '}')
7264 return NOTDONE;
7265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266
7267 if (evaluate)
7268 {
7269 d = dict_alloc();
7270 if (d == NULL)
7271 return FAIL;
7272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 tvkey.v_type = VAR_UNKNOWN;
7274 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275
7276 *arg = skipwhite(*arg + 1);
7277 while (**arg != '}' && **arg != NUL)
7278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 goto failret;
7281 if (**arg != ':')
7282 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007283 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 goto failret;
7286 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007287 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007289 key = get_tv_string_buf_chk(&tvkey, buf);
7290 if (key == NULL || *key == NUL)
7291 {
7292 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7293 if (key != NULL)
7294 EMSG(_(e_emptykey));
7295 clear_tv(&tvkey);
7296 goto failret;
7297 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299
7300 *arg = skipwhite(*arg + 1);
7301 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7302 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007303 if (evaluate)
7304 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 goto failret;
7306 }
7307 if (evaluate)
7308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 if (item != NULL)
7311 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007312 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 clear_tv(&tv);
7315 goto failret;
7316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 item = dictitem_alloc(key);
7318 clear_tv(&tvkey);
7319 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007322 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (dict_add(d, item) == FAIL)
7324 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 }
7326 }
7327
7328 if (**arg == '}')
7329 break;
7330 if (**arg != ',')
7331 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007332 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 goto failret;
7334 }
7335 *arg = skipwhite(*arg + 1);
7336 }
7337
7338 if (**arg != '}')
7339 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007340 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341failret:
7342 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007343 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 return FAIL;
7345 }
7346
7347 *arg = skipwhite(*arg + 1);
7348 if (evaluate)
7349 {
7350 rettv->v_type = VAR_DICT;
7351 rettv->vval.v_dict = d;
7352 ++d->dv_refcount;
7353 }
7354
7355 return OK;
7356}
7357
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007359 * Return a string with the string representation of a variable.
7360 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007361 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007362 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007363 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007364 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 */
7366 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007367echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007370 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007371 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 static int recurse = 0;
7374 char_u *r = NULL;
7375
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007378 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 *tofree = NULL;
7380 return NULL;
7381 }
7382 ++recurse;
7383
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 switch (tv->v_type)
7385 {
7386 case VAR_FUNC:
7387 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 r = tv->vval.v_string;
7389 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392 if (tv->vval.v_list == NULL)
7393 {
7394 *tofree = NULL;
7395 r = NULL;
7396 }
7397 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7398 {
7399 *tofree = NULL;
7400 r = (char_u *)"[...]";
7401 }
7402 else
7403 {
7404 tv->vval.v_list->lv_copyID = copyID;
7405 *tofree = list2string(tv, copyID);
7406 r = *tofree;
7407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007409
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411 if (tv->vval.v_dict == NULL)
7412 {
7413 *tofree = NULL;
7414 r = NULL;
7415 }
7416 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7417 {
7418 *tofree = NULL;
7419 r = (char_u *)"{...}";
7420 }
7421 else
7422 {
7423 tv->vval.v_dict->dv_copyID = copyID;
7424 *tofree = dict2string(tv, copyID);
7425 r = *tofree;
7426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 case VAR_STRING:
7430 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 *tofree = NULL;
7432 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007433 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007434
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007435#ifdef FEAT_FLOAT
7436 case VAR_FLOAT:
7437 *tofree = NULL;
7438 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7439 r = numbuf;
7440 break;
7441#endif
7442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007444 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007445 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447
7448 --recurse;
7449 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007450}
7451
7452/*
7453 * Return a string with the string representation of a variable.
7454 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7455 * "numbuf" is used for a number.
7456 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007457 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007458 */
7459 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007460tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007462 char_u **tofree;
7463 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007464 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007465{
7466 switch (tv->v_type)
7467 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007468 case VAR_FUNC:
7469 *tofree = string_quote(tv->vval.v_string, TRUE);
7470 return *tofree;
7471 case VAR_STRING:
7472 *tofree = string_quote(tv->vval.v_string, FALSE);
7473 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007474#ifdef FEAT_FLOAT
7475 case VAR_FLOAT:
7476 *tofree = NULL;
7477 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7478 return numbuf;
7479#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007481 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007484 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007485 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007486 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007487 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007488}
7489
7490/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 * Return string "str" in ' quotes, doubling ' characters.
7492 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007494 */
7495 static char_u *
7496string_quote(str, function)
7497 char_u *str;
7498 int function;
7499{
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007501 char_u *p, *r, *s;
7502
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 len = (function ? 13 : 3);
7504 if (str != NULL)
7505 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007506 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 for (p = str; *p != NUL; mb_ptr_adv(p))
7508 if (*p == '\'')
7509 ++len;
7510 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007511 s = r = alloc(len);
7512 if (r != NULL)
7513 {
7514 if (function)
7515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007517 r += 10;
7518 }
7519 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 if (str != NULL)
7522 for (p = str; *p != NUL; )
7523 {
7524 if (*p == '\'')
7525 *r++ = '\'';
7526 MB_COPY_CHAR(p, r);
7527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007529 if (function)
7530 *r++ = ')';
7531 *r++ = NUL;
7532 }
7533 return s;
7534}
7535
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007536#ifdef FEAT_FLOAT
7537/*
7538 * Convert the string "text" to a floating point number.
7539 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7540 * this always uses a decimal point.
7541 * Returns the length of the text that was consumed.
7542 */
7543 static int
7544string2float(text, value)
7545 char_u *text;
7546 float_T *value; /* result stored here */
7547{
7548 char *s = (char *)text;
7549 float_T f;
7550
7551 f = strtod(s, &s);
7552 *value = f;
7553 return (int)((char_u *)s - text);
7554}
7555#endif
7556
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 * Get the value of an environment variable.
7559 * "arg" is pointing to the '$'. It is advanced to after the name.
7560 * If the environment variable was not set, silently assume it is empty.
7561 * Always return OK.
7562 */
7563 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 int evaluate;
7568{
7569 char_u *string = NULL;
7570 int len;
7571 int cc;
7572 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007573 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574
7575 ++*arg;
7576 name = *arg;
7577 len = get_env_len(arg);
7578 if (evaluate)
7579 {
7580 if (len != 0)
7581 {
7582 cc = name[len];
7583 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007584 /* first try vim_getenv(), fast for normal environment vars */
7585 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007587 {
7588 if (!mustfree)
7589 string = vim_strsave(string);
7590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 else
7592 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007593 if (mustfree)
7594 vim_free(string);
7595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 /* next try expanding things like $VIM and ${HOME} */
7597 string = expand_env_save(name - 1);
7598 if (string != NULL && *string == '$')
7599 {
7600 vim_free(string);
7601 string = NULL;
7602 }
7603 }
7604 name[len] = cc;
7605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606 rettv->v_type = VAR_STRING;
7607 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609
7610 return OK;
7611}
7612
7613/*
7614 * Array with names and number of arguments of all internal functions
7615 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7616 */
7617static struct fst
7618{
7619 char *f_name; /* function name */
7620 char f_min_argc; /* minimal number of arguments */
7621 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007623 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624} functions[] =
7625{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007626#ifdef FEAT_FLOAT
7627 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007628 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007629#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007630 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 {"append", 2, 2, f_append},
7632 {"argc", 0, 0, f_argc},
7633 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007634 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007635#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007636 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007637 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007638 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007641 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {"bufexists", 1, 1, f_bufexists},
7643 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7644 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7645 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7646 {"buflisted", 1, 1, f_buflisted},
7647 {"bufloaded", 1, 1, f_bufloaded},
7648 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007649 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {"bufwinnr", 1, 1, f_bufwinnr},
7651 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007652 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007653 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007654#ifdef FEAT_FLOAT
7655 {"ceil", 1, 1, f_ceil},
7656#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007657 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"char2nr", 1, 1, f_char2nr},
7659 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007660 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007662#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007663 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007664 {"complete_add", 1, 1, f_complete_add},
7665 {"complete_check", 0, 0, f_complete_check},
7666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007668 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007669#ifdef FEAT_FLOAT
7670 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007671 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007675 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007676 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {"delete", 1, 1, f_delete},
7678 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007679 {"diff_filler", 1, 1, f_diff_filler},
7680 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007681 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"eventhandler", 0, 0, f_eventhandler},
7685 {"executable", 1, 1, f_executable},
7686 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007687#ifdef FEAT_FLOAT
7688 {"exp", 1, 1, f_exp},
7689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007691 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007692 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7694 {"filereadable", 1, 1, f_filereadable},
7695 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007697 {"finddir", 1, 3, f_finddir},
7698 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007699#ifdef FEAT_FLOAT
7700 {"float2nr", 1, 1, f_float2nr},
7701 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007702 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007703#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007704 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"fnamemodify", 2, 2, f_fnamemodify},
7706 {"foldclosed", 1, 1, f_foldclosed},
7707 {"foldclosedend", 1, 1, f_foldclosedend},
7708 {"foldlevel", 1, 1, f_foldlevel},
7709 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007710 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007712 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007713 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007714 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007715 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {"getbufvar", 2, 2, f_getbufvar},
7717 {"getchar", 0, 1, f_getchar},
7718 {"getcharmod", 0, 0, f_getcharmod},
7719 {"getcmdline", 0, 0, f_getcmdline},
7720 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007721 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007723 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007724 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {"getfsize", 1, 1, f_getfsize},
7726 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007727 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007728 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007729 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007730 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007731 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007732 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007733 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007734 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007736 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007737 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {"getwinposx", 0, 0, f_getwinposx},
7739 {"getwinposy", 0, 0, f_getwinposy},
7740 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007741 {"glob", 1, 2, f_glob},
7742 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007744 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007745 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007746 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7748 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7749 {"histadd", 2, 2, f_histadd},
7750 {"histdel", 1, 2, f_histdel},
7751 {"histget", 1, 2, f_histget},
7752 {"histnr", 1, 1, f_histnr},
7753 {"hlID", 1, 1, f_hlID},
7754 {"hlexists", 1, 1, f_hlexists},
7755 {"hostname", 0, 0, f_hostname},
7756 {"iconv", 3, 3, f_iconv},
7757 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007758 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007759 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007761 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"inputrestore", 0, 0, f_inputrestore},
7763 {"inputsave", 0, 0, f_inputsave},
7764 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007765 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007767 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {"libcall", 3, 3, f_libcall},
7774 {"libcallnr", 3, 3, f_libcallnr},
7775 {"line", 1, 1, f_line},
7776 {"line2byte", 1, 1, f_line2byte},
7777 {"lispindent", 1, 1, f_lispindent},
7778 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007780 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007781 {"log10", 1, 1, f_log10},
7782#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007783 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007784 {"maparg", 1, 3, f_maparg},
7785 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007786 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007787 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007788 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007789 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007790 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007791 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007792 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007793 {"max", 1, 1, f_max},
7794 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007795#ifdef vim_mkdir
7796 {"mkdir", 1, 3, f_mkdir},
7797#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007798 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007799#ifdef FEAT_MZSCHEME
7800 {"mzeval", 1, 1, f_mzeval},
7801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"nextnonblank", 1, 1, f_nextnonblank},
7803 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007804 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007805#ifdef FEAT_FLOAT
7806 {"pow", 2, 2, f_pow},
7807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007809 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007810 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007811 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007812 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007813 {"reltime", 0, 2, f_reltime},
7814 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"remote_expr", 2, 3, f_remote_expr},
7816 {"remote_foreground", 1, 1, f_remote_foreground},
7817 {"remote_peek", 1, 2, f_remote_peek},
7818 {"remote_read", 1, 1, f_remote_read},
7819 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007820 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007822 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007824 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825#ifdef FEAT_FLOAT
7826 {"round", 1, 1, f_round},
7827#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007828 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007829 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007830 {"searchpair", 3, 7, f_searchpair},
7831 {"searchpairpos", 3, 7, f_searchpairpos},
7832 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {"server2client", 2, 2, f_server2client},
7834 {"serverlist", 0, 0, f_serverlist},
7835 {"setbufvar", 3, 3, f_setbufvar},
7836 {"setcmdpos", 1, 1, f_setcmdpos},
7837 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007838 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007839 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007840 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007841 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007843 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007844 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007846 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#ifdef FEAT_FLOAT
7849 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007850 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007852 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007853 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007854 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007855 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007856 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007857#ifdef FEAT_FLOAT
7858 {"sqrt", 1, 1, f_sqrt},
7859 {"str2float", 1, 1, f_str2float},
7860#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007861 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007862 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007863 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864#ifdef HAVE_STRFTIME
7865 {"strftime", 1, 2, f_strftime},
7866#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007868 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"strlen", 1, 1, f_strlen},
7870 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007871 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007873 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {"submatch", 1, 1, f_submatch},
7875 {"substitute", 4, 4, f_substitute},
7876 {"synID", 3, 3, f_synID},
7877 {"synIDattr", 2, 3, f_synIDattr},
7878 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007879 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007880 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007881 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007882 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007883 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007884 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007885 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007886#ifdef FEAT_FLOAT
7887 {"tan", 1, 1, f_tan},
7888 {"tanh", 1, 1, f_tanh},
7889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007891 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"tolower", 1, 1, f_tolower},
7893 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007894 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007895#ifdef FEAT_FLOAT
7896 {"trunc", 1, 1, f_trunc},
7897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007899 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007900 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007901 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"virtcol", 1, 1, f_virtcol},
7903 {"visualmode", 0, 1, f_visualmode},
7904 {"winbufnr", 1, 1, f_winbufnr},
7905 {"wincol", 0, 0, f_wincol},
7906 {"winheight", 1, 1, f_winheight},
7907 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007908 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007910 {"winrestview", 1, 1, f_winrestview},
7911 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007913 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914};
7915
7916#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7917
7918/*
7919 * Function given to ExpandGeneric() to obtain the list of internal
7920 * or user defined function names.
7921 */
7922 char_u *
7923get_function_name(xp, idx)
7924 expand_T *xp;
7925 int idx;
7926{
7927 static int intidx = -1;
7928 char_u *name;
7929
7930 if (idx == 0)
7931 intidx = -1;
7932 if (intidx < 0)
7933 {
7934 name = get_user_func_name(xp, idx);
7935 if (name != NULL)
7936 return name;
7937 }
7938 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7939 {
7940 STRCPY(IObuff, functions[intidx].f_name);
7941 STRCAT(IObuff, "(");
7942 if (functions[intidx].f_max_argc == 0)
7943 STRCAT(IObuff, ")");
7944 return IObuff;
7945 }
7946
7947 return NULL;
7948}
7949
7950/*
7951 * Function given to ExpandGeneric() to obtain the list of internal or
7952 * user defined variable or function names.
7953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 char_u *
7955get_expr_name(xp, idx)
7956 expand_T *xp;
7957 int idx;
7958{
7959 static int intidx = -1;
7960 char_u *name;
7961
7962 if (idx == 0)
7963 intidx = -1;
7964 if (intidx < 0)
7965 {
7966 name = get_function_name(xp, idx);
7967 if (name != NULL)
7968 return name;
7969 }
7970 return get_user_var_name(xp, ++intidx);
7971}
7972
7973#endif /* FEAT_CMDL_COMPL */
7974
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007975#if defined(EBCDIC) || defined(PROTO)
7976/*
7977 * Compare struct fst by function name.
7978 */
7979 static int
7980compare_func_name(s1, s2)
7981 const void *s1;
7982 const void *s2;
7983{
7984 struct fst *p1 = (struct fst *)s1;
7985 struct fst *p2 = (struct fst *)s2;
7986
7987 return STRCMP(p1->f_name, p2->f_name);
7988}
7989
7990/*
7991 * Sort the function table by function name.
7992 * The sorting of the table above is ASCII dependant.
7993 * On machines using EBCDIC we have to sort it.
7994 */
7995 static void
7996sortFunctions()
7997{
7998 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7999
8000 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8001}
8002#endif
8003
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005/*
8006 * Find internal function in table above.
8007 * Return index, or -1 if not found
8008 */
8009 static int
8010find_internal_func(name)
8011 char_u *name; /* name of the function */
8012{
8013 int first = 0;
8014 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8015 int cmp;
8016 int x;
8017
8018 /*
8019 * Find the function name in the table. Binary search.
8020 */
8021 while (first <= last)
8022 {
8023 x = first + ((unsigned)(last - first) >> 1);
8024 cmp = STRCMP(name, functions[x].f_name);
8025 if (cmp < 0)
8026 last = x - 1;
8027 else if (cmp > 0)
8028 first = x + 1;
8029 else
8030 return x;
8031 }
8032 return -1;
8033}
8034
8035/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008036 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8037 * name it contains, otherwise return "name".
8038 */
8039 static char_u *
8040deref_func_name(name, lenp)
8041 char_u *name;
8042 int *lenp;
8043{
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008045 int cc;
8046
8047 cc = name[*lenp];
8048 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008049 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 {
8055 *lenp = 0;
8056 return (char_u *)""; /* just in case */
8057 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008058 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008060 }
8061
8062 return name;
8063}
8064
8065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 * Allocate a variable for the result of a function.
8067 * Return OK or FAIL.
8068 */
8069 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8071 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 char_u *name; /* name of the function */
8073 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 char_u **arg; /* argument, pointing to the '(' */
8076 linenr_T firstline; /* first line of range */
8077 linenr_T lastline; /* last line of range */
8078 int *doesrange; /* return: function handled range */
8079 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081{
8082 char_u *argp;
8083 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008084 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 int argcount = 0; /* number of arguments found */
8086
8087 /*
8088 * Get the arguments.
8089 */
8090 argp = *arg;
8091 while (argcount < MAX_FUNC_ARGS)
8092 {
8093 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8094 if (*argp == ')' || *argp == ',' || *argp == NUL)
8095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8097 {
8098 ret = FAIL;
8099 break;
8100 }
8101 ++argcount;
8102 if (*argp != ',')
8103 break;
8104 }
8105 if (*argp == ')')
8106 ++argp;
8107 else
8108 ret = FAIL;
8109
8110 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008112 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 {
8115 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008116 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008118 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
8121 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123
8124 *arg = skipwhite(argp);
8125 return ret;
8126}
8127
8128
8129/*
8130 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008131 * Return OK when the function can't be called, FAIL otherwise.
8132 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 */
8134 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008135call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008136 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008137 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008141 typval_T *argvars; /* vars for arguments, must have "argcount"
8142 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 linenr_T firstline; /* first line of range */
8144 linenr_T lastline; /* last line of range */
8145 int *doesrange; /* return: function handled range */
8146 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008147 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150#define ERROR_UNKNOWN 0
8151#define ERROR_TOOMANY 1
8152#define ERROR_TOOFEW 2
8153#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154#define ERROR_DICT 4
8155#define ERROR_NONE 5
8156#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 int error = ERROR_NONE;
8158 int i;
8159 int llen;
8160 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161#define FLEN_FIXED 40
8162 char_u fname_buf[FLEN_FIXED + 1];
8163 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008164 char_u *name;
8165
8166 /* Make a copy of the name, if it comes from a funcref variable it could
8167 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008168 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008169 if (name == NULL)
8170 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171
8172 /*
8173 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8174 * Change <SNR>123_name() to K_SNR 123_name().
8175 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 llen = eval_fname_script(name);
8178 if (llen > 0)
8179 {
8180 fname_buf[0] = K_SPECIAL;
8181 fname_buf[1] = KS_EXTRA;
8182 fname_buf[2] = (int)KE_SNR;
8183 i = 3;
8184 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8185 {
8186 if (current_SID <= 0)
8187 error = ERROR_SCRIPT;
8188 else
8189 {
8190 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8191 i = (int)STRLEN(fname_buf);
8192 }
8193 }
8194 if (i + STRLEN(name + llen) < FLEN_FIXED)
8195 {
8196 STRCPY(fname_buf + i, name + llen);
8197 fname = fname_buf;
8198 }
8199 else
8200 {
8201 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8202 if (fname == NULL)
8203 error = ERROR_OTHER;
8204 else
8205 {
8206 mch_memmove(fname, fname_buf, (size_t)i);
8207 STRCPY(fname + i, name + llen);
8208 }
8209 }
8210 }
8211 else
8212 fname = name;
8213
8214 *doesrange = FALSE;
8215
8216
8217 /* execute the function if no errors detected and executing */
8218 if (evaluate && error == ERROR_NONE)
8219 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008220 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8221 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 error = ERROR_UNKNOWN;
8223
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008224 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
8226 /*
8227 * User defined function.
8228 */
8229 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008232 /* Trigger FuncUndefined event, may load the function. */
8233 if (fp == NULL
8234 && apply_autocmds(EVENT_FUNCUNDEFINED,
8235 fname, fname, TRUE, NULL)
8236 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008238 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 fp = find_func(fname);
8240 }
8241#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008242 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008243 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008244 {
8245 /* loaded a package, search for the function again */
8246 fp = find_func(fname);
8247 }
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (fp != NULL)
8250 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008251 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008253 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008255 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008257 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008258 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 else
8260 {
8261 /*
8262 * Call the user function.
8263 * Save and restore search patterns, script variables and
8264 * redo buffer.
8265 */
8266 save_search_patterns();
8267 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008268 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008270 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008271 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8272 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8273 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008274 /* Function was unreferenced while being used, free it
8275 * now. */
8276 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 restoreRedobuff();
8278 restore_search_patterns();
8279 error = ERROR_NONE;
8280 }
8281 }
8282 }
8283 else
8284 {
8285 /*
8286 * Find the function name in the table, call its implementation.
8287 */
8288 i = find_internal_func(fname);
8289 if (i >= 0)
8290 {
8291 if (argcount < functions[i].f_min_argc)
8292 error = ERROR_TOOFEW;
8293 else if (argcount > functions[i].f_max_argc)
8294 error = ERROR_TOOMANY;
8295 else
8296 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008297 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008298 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 error = ERROR_NONE;
8300 }
8301 }
8302 }
8303 /*
8304 * The function call (or "FuncUndefined" autocommand sequence) might
8305 * have been aborted by an error, an interrupt, or an explicitly thrown
8306 * exception that has not been caught so far. This situation can be
8307 * tested for by calling aborting(). For an error in an internal
8308 * function or for the "E132" error in call_user_func(), however, the
8309 * throw point at which the "force_abort" flag (temporarily reset by
8310 * emsg()) is normally updated has not been reached yet. We need to
8311 * update that flag first to make aborting() reliable.
8312 */
8313 update_force_abort();
8314 }
8315 if (error == ERROR_NONE)
8316 ret = OK;
8317
8318 /*
8319 * Report an error unless the argument evaluation or function call has been
8320 * cancelled due to an aborting error, an interrupt, or an exception.
8321 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008322 if (!aborting())
8323 {
8324 switch (error)
8325 {
8326 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008327 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008328 break;
8329 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008330 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008331 break;
8332 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008333 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008334 name);
8335 break;
8336 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008337 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 name);
8339 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008340 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008341 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008342 name);
8343 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008344 }
8345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 if (fname != name && fname != fname_buf)
8348 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008349 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
8351 return ret;
8352}
8353
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008354/*
8355 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008356 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008357 */
8358 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008359emsg_funcname(ermsg, name)
8360 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008361 char_u *name;
8362{
8363 char_u *p;
8364
8365 if (*name == K_SPECIAL)
8366 p = concat_str((char_u *)"<SNR>", name + 3);
8367 else
8368 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008369 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008370 if (p != name)
8371 vim_free(p);
8372}
8373
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008374/*
8375 * Return TRUE for a non-zero Number and a non-empty String.
8376 */
8377 static int
8378non_zero_arg(argvars)
8379 typval_T *argvars;
8380{
8381 return ((argvars[0].v_type == VAR_NUMBER
8382 && argvars[0].vval.v_number != 0)
8383 || (argvars[0].v_type == VAR_STRING
8384 && argvars[0].vval.v_string != NULL
8385 && *argvars[0].vval.v_string != NUL));
8386}
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388/*********************************************
8389 * Implementation of the built-in functions
8390 */
8391
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008392#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008393static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8394
8395/*
8396 * Get the float value of "argvars[0]" into "f".
8397 * Returns FAIL when the argument is not a Number or Float.
8398 */
8399 static int
8400get_float_arg(argvars, f)
8401 typval_T *argvars;
8402 float_T *f;
8403{
8404 if (argvars[0].v_type == VAR_FLOAT)
8405 {
8406 *f = argvars[0].vval.v_float;
8407 return OK;
8408 }
8409 if (argvars[0].v_type == VAR_NUMBER)
8410 {
8411 *f = (float_T)argvars[0].vval.v_number;
8412 return OK;
8413 }
8414 EMSG(_("E808: Number or Float required"));
8415 return FAIL;
8416}
8417
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008418/*
8419 * "abs(expr)" function
8420 */
8421 static void
8422f_abs(argvars, rettv)
8423 typval_T *argvars;
8424 typval_T *rettv;
8425{
8426 if (argvars[0].v_type == VAR_FLOAT)
8427 {
8428 rettv->v_type = VAR_FLOAT;
8429 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8430 }
8431 else
8432 {
8433 varnumber_T n;
8434 int error = FALSE;
8435
8436 n = get_tv_number_chk(&argvars[0], &error);
8437 if (error)
8438 rettv->vval.v_number = -1;
8439 else if (n > 0)
8440 rettv->vval.v_number = n;
8441 else
8442 rettv->vval.v_number = -n;
8443 }
8444}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008445
8446/*
8447 * "acos()" function
8448 */
8449 static void
8450f_acos(argvars, rettv)
8451 typval_T *argvars;
8452 typval_T *rettv;
8453{
8454 float_T f;
8455
8456 rettv->v_type = VAR_FLOAT;
8457 if (get_float_arg(argvars, &f) == OK)
8458 rettv->vval.v_float = acos(f);
8459 else
8460 rettv->vval.v_float = 0.0;
8461}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008462#endif
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008465 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 */
8467 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008468f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008469 typval_T *argvars;
8470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
Bram Moolenaar33570922005-01-25 22:26:29 +00008472 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008477 if ((l = argvars[0].vval.v_list) != NULL
8478 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8479 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 }
8482 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008483 EMSG(_(e_listreq));
8484}
8485
8486/*
8487 * "append(lnum, string/list)" function
8488 */
8489 static void
8490f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008493{
8494 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008495 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 list_T *l = NULL;
8497 listitem_T *li = NULL;
8498 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008499 long added = 0;
8500
Bram Moolenaar0d660222005-01-07 21:51:51 +00008501 lnum = get_tv_lnum(argvars);
8502 if (lnum >= 0
8503 && lnum <= curbuf->b_ml.ml_line_count
8504 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008506 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008508 l = argvars[1].vval.v_list;
8509 if (l == NULL)
8510 return;
8511 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008513 for (;;)
8514 {
8515 if (l == NULL)
8516 tv = &argvars[1]; /* append a string */
8517 else if (li == NULL)
8518 break; /* end of list */
8519 else
8520 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008521 line = get_tv_string_chk(tv);
8522 if (line == NULL) /* type error */
8523 {
8524 rettv->vval.v_number = 1; /* Failed */
8525 break;
8526 }
8527 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008528 ++added;
8529 if (l == NULL)
8530 break;
8531 li = li->li_next;
8532 }
8533
8534 appended_lines_mark(lnum, added);
8535 if (curwin->w_cursor.lnum > lnum)
8536 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 else
8539 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540}
8541
8542/*
8543 * "argc()" function
8544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008547 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008550 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * "argidx()" function
8555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008558 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562}
8563
8564/*
8565 * "argv(nr)" function
8566 */
8567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 typval_T *argvars;
8570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 int idx;
8573
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008574 if (argvars[0].v_type != VAR_UNKNOWN)
8575 {
8576 idx = get_tv_number_chk(&argvars[0], NULL);
8577 if (idx >= 0 && idx < ARGCOUNT)
8578 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8579 else
8580 rettv->vval.v_string = NULL;
8581 rettv->v_type = VAR_STRING;
8582 }
8583 else if (rettv_list_alloc(rettv) == OK)
8584 for (idx = 0; idx < ARGCOUNT; ++idx)
8585 list_append_string(rettv->vval.v_list,
8586 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008589#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008590/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008591 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008593 static void
8594f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008595 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008596 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008597{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008598 float_T f;
8599
8600 rettv->v_type = VAR_FLOAT;
8601 if (get_float_arg(argvars, &f) == OK)
8602 rettv->vval.v_float = asin(f);
8603 else
8604 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008605}
8606
8607/*
8608 * "atan()" function
8609 */
8610 static void
8611f_atan(argvars, rettv)
8612 typval_T *argvars;
8613 typval_T *rettv;
8614{
8615 float_T f;
8616
8617 rettv->v_type = VAR_FLOAT;
8618 if (get_float_arg(argvars, &f) == OK)
8619 rettv->vval.v_float = atan(f);
8620 else
8621 rettv->vval.v_float = 0.0;
8622}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008623
8624/*
8625 * "atan2()" function
8626 */
8627 static void
8628f_atan2(argvars, rettv)
8629 typval_T *argvars;
8630 typval_T *rettv;
8631{
8632 float_T fx, fy;
8633
8634 rettv->v_type = VAR_FLOAT;
8635 if (get_float_arg(argvars, &fx) == OK
8636 && get_float_arg(&argvars[1], &fy) == OK)
8637 rettv->vval.v_float = atan2(fx, fy);
8638 else
8639 rettv->vval.v_float = 0.0;
8640}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008641#endif
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643/*
8644 * "browse(save, title, initdir, default)" function
8645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008648 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
8651#ifdef FEAT_BROWSE
8652 int save;
8653 char_u *title;
8654 char_u *initdir;
8655 char_u *defname;
8656 char_u buf[NUMBUFLEN];
8657 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008660 save = get_tv_number_chk(&argvars[0], &error);
8661 title = get_tv_string_chk(&argvars[1]);
8662 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8663 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008665 if (error || title == NULL || initdir == NULL || defname == NULL)
8666 rettv->vval.v_string = NULL;
8667 else
8668 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008669 do_browse(save ? BROWSE_SAVE : 0,
8670 title, defname, NULL, initdir, NULL, curbuf);
8671#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008673#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008675}
8676
8677/*
8678 * "browsedir(title, initdir)" function
8679 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008682 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008684{
8685#ifdef FEAT_BROWSE
8686 char_u *title;
8687 char_u *initdir;
8688 char_u buf[NUMBUFLEN];
8689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008690 title = get_tv_string_chk(&argvars[0]);
8691 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008693 if (title == NULL || initdir == NULL)
8694 rettv->vval.v_string = NULL;
8695 else
8696 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008697 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702}
8703
Bram Moolenaar33570922005-01-25 22:26:29 +00008704static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706/*
8707 * Find a buffer by number or exact name.
8708 */
8709 static buf_T *
8710find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008711 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008715 if (avar->v_type == VAR_NUMBER)
8716 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008717 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008719 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008720 if (buf == NULL)
8721 {
8722 /* No full path name match, try a match with a URL or a "nofile"
8723 * buffer, these don't use the full path. */
8724 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8725 if (buf->b_fname != NULL
8726 && (path_with_url(buf->b_fname)
8727#ifdef FEAT_QUICKFIX
8728 || bt_nofile(buf)
8729#endif
8730 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008731 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008732 break;
8733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 }
8735 return buf;
8736}
8737
8738/*
8739 * "bufexists(expr)" function
8740 */
8741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008743 typval_T *argvars;
8744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747}
8748
8749/*
8750 * "buflisted(expr)" function
8751 */
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *argvars;
8755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 buf_T *buf;
8758
8759 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761}
8762
8763/*
8764 * "bufloaded(expr)" function
8765 */
8766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T *argvars;
8769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
8771 buf_T *buf;
8772
8773 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
Bram Moolenaar33570922005-01-25 22:26:29 +00008777static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008778
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779/*
8780 * Get buffer by number or pattern.
8781 */
8782 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008784 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 int save_magic;
8788 char_u *save_cpo;
8789 buf_T *buf;
8790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 if (tv->v_type == VAR_NUMBER)
8792 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008793 if (tv->v_type != VAR_STRING)
8794 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 if (name == NULL || *name == NUL)
8796 return curbuf;
8797 if (name[0] == '$' && name[1] == NUL)
8798 return lastbuf;
8799
8800 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8801 save_magic = p_magic;
8802 p_magic = TRUE;
8803 save_cpo = p_cpo;
8804 p_cpo = (char_u *)"";
8805
8806 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8807 TRUE, FALSE));
8808
8809 p_magic = save_magic;
8810 p_cpo = save_cpo;
8811
8812 /* If not found, try expanding the name, like done for bufexists(). */
8813 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
8816 return buf;
8817}
8818
8819/*
8820 * "bufname(expr)" function
8821 */
8822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
8827 buf_T *buf;
8828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008829 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 buf = get_buf_tv(&argvars[0]);
8832 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 --emsg_off;
8838}
8839
8840/*
8841 * "bufnr(expr)" function
8842 */
8843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *argvars;
8846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008849 int error = FALSE;
8850 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008855 --emsg_off;
8856
8857 /* If the buffer isn't found and the second argument is not zero create a
8858 * new buffer. */
8859 if (buf == NULL
8860 && argvars[1].v_type != VAR_UNKNOWN
8861 && get_tv_number_chk(&argvars[1], &error) != 0
8862 && !error
8863 && (name = get_tv_string_chk(&argvars[0])) != NULL
8864 && !error)
8865 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871}
8872
8873/*
8874 * "bufwinnr(nr)" function
8875 */
8876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 typval_T *argvars;
8879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881#ifdef FEAT_WINDOWS
8882 win_T *wp;
8883 int winnr = 0;
8884#endif
8885 buf_T *buf;
8886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#ifdef FEAT_WINDOWS
8891 for (wp = firstwin; wp; wp = wp->w_next)
8892 {
8893 ++winnr;
8894 if (wp->w_buffer == buf)
8895 break;
8896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900#endif
8901 --emsg_off;
8902}
8903
8904/*
8905 * "byte2line(byte)" function
8906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008909 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911{
8912#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914#else
8915 long boff = 0;
8916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 (linenr_T)0, &boff);
8923#endif
8924}
8925
8926/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008927 * "byteidx()" function
8928 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008931 typval_T *argvars;
8932 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008933{
8934#ifdef FEAT_MBYTE
8935 char_u *t;
8936#endif
8937 char_u *str;
8938 long idx;
8939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 str = get_tv_string_chk(&argvars[0]);
8941 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008944 return;
8945
8946#ifdef FEAT_MBYTE
8947 t = str;
8948 for ( ; idx > 0; idx--)
8949 {
8950 if (*t == NUL) /* EOL reached */
8951 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008952 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008953 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008954 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008955#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008956 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008958#endif
8959}
8960
8961/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008962 * "call(func, arglist)" function
8963 */
8964 static void
8965f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *argvars;
8967 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008968{
8969 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008970 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008971 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008972 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008973 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008975
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008976 if (argvars[1].v_type != VAR_LIST)
8977 {
8978 EMSG(_(e_listreq));
8979 return;
8980 }
8981 if (argvars[1].vval.v_list == NULL)
8982 return;
8983
8984 if (argvars[0].v_type == VAR_FUNC)
8985 func = argvars[0].vval.v_string;
8986 else
8987 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 if (*func == NUL)
8989 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008990
Bram Moolenaare9a41262005-01-15 22:18:47 +00008991 if (argvars[2].v_type != VAR_UNKNOWN)
8992 {
8993 if (argvars[2].v_type != VAR_DICT)
8994 {
8995 EMSG(_(e_dictreq));
8996 return;
8997 }
8998 selfdict = argvars[2].vval.v_dict;
8999 }
9000
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009001 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9002 item = item->li_next)
9003 {
9004 if (argc == MAX_FUNC_ARGS)
9005 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009006 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 break;
9008 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009009 /* Make a copy of each argument. This is needed to be able to set
9010 * v_lock to VAR_FIXED in the copy without changing the original list.
9011 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012 copy_tv(&item->li_tv, &argv[argc++]);
9013 }
9014
9015 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009016 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9018 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009019
9020 /* Free the arguments. */
9021 while (argc > 0)
9022 clear_tv(&argv[--argc]);
9023}
9024
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009025#ifdef FEAT_FLOAT
9026/*
9027 * "ceil({float})" function
9028 */
9029 static void
9030f_ceil(argvars, rettv)
9031 typval_T *argvars;
9032 typval_T *rettv;
9033{
9034 float_T f;
9035
9036 rettv->v_type = VAR_FLOAT;
9037 if (get_float_arg(argvars, &f) == OK)
9038 rettv->vval.v_float = ceil(f);
9039 else
9040 rettv->vval.v_float = 0.0;
9041}
9042#endif
9043
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009044/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009045 * "changenr()" function
9046 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009047 static void
9048f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009049 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009050 typval_T *rettv;
9051{
9052 rettv->vval.v_number = curbuf->b_u_seq_cur;
9053}
9054
9055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 * "char2nr(string)" function
9057 */
9058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009060 typval_T *argvars;
9061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063#ifdef FEAT_MBYTE
9064 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 else
9067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
9071/*
9072 * "cindent(lnum)" function
9073 */
9074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 typval_T *argvars;
9077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079#ifdef FEAT_CINDENT
9080 pos_T pos;
9081 linenr_T lnum;
9082
9083 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9086 {
9087 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 curwin->w_cursor = pos;
9090 }
9091 else
9092#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094}
9095
9096/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009097 * "clearmatches()" function
9098 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009099 static void
9100f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009101 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009102 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009103{
9104#ifdef FEAT_SEARCH_EXTRA
9105 clear_matches(curwin);
9106#endif
9107}
9108
9109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 * "col(string)" function
9111 */
9112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009114 typval_T *argvars;
9115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
9117 colnr_T col = 0;
9118 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009119 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009121 fp = var2fpos(&argvars[0], FALSE, &fnum);
9122 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 {
9124 if (fp->col == MAXCOL)
9125 {
9126 /* '> can be MAXCOL, get the length of the line then */
9127 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009128 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 else
9130 col = MAXCOL;
9131 }
9132 else
9133 {
9134 col = fp->col + 1;
9135#ifdef FEAT_VIRTUALEDIT
9136 /* col(".") when the cursor is on the NUL at the end of the line
9137 * because of "coladd" can be seen as an extra column. */
9138 if (virtual_active() && fp == &curwin->w_cursor)
9139 {
9140 char_u *p = ml_get_cursor();
9141
9142 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9143 curwin->w_virtcol - curwin->w_cursor.coladd))
9144 {
9145# ifdef FEAT_MBYTE
9146 int l;
9147
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009148 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 col += l;
9150# else
9151 if (*p != NUL && p[1] == NUL)
9152 ++col;
9153# endif
9154 }
9155 }
9156#endif
9157 }
9158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160}
9161
Bram Moolenaar572cb562005-08-05 21:35:02 +00009162#if defined(FEAT_INS_EXPAND)
9163/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009164 * "complete()" function
9165 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009166 static void
9167f_complete(argvars, rettv)
9168 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009169 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009170{
9171 int startcol;
9172
9173 if ((State & INSERT) == 0)
9174 {
9175 EMSG(_("E785: complete() can only be used in Insert mode"));
9176 return;
9177 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009178
9179 /* Check for undo allowed here, because if something was already inserted
9180 * the line was already saved for undo and this check isn't done. */
9181 if (!undo_allowed())
9182 return;
9183
Bram Moolenaarade00832006-03-10 21:46:58 +00009184 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9185 {
9186 EMSG(_(e_invarg));
9187 return;
9188 }
9189
9190 startcol = get_tv_number_chk(&argvars[0], NULL);
9191 if (startcol <= 0)
9192 return;
9193
9194 set_completion(startcol - 1, argvars[1].vval.v_list);
9195}
9196
9197/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009198 * "complete_add()" function
9199 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009200 static void
9201f_complete_add(argvars, rettv)
9202 typval_T *argvars;
9203 typval_T *rettv;
9204{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009205 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009206}
9207
9208/*
9209 * "complete_check()" function
9210 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009211 static void
9212f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009213 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009214 typval_T *rettv;
9215{
9216 int saved = RedrawingDisabled;
9217
9218 RedrawingDisabled = 0;
9219 ins_compl_check_keys(0);
9220 rettv->vval.v_number = compl_interrupted;
9221 RedrawingDisabled = saved;
9222}
9223#endif
9224
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225/*
9226 * "confirm(message, buttons[, default [, type]])" function
9227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009230 typval_T *argvars UNUSED;
9231 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
9233#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9234 char_u *message;
9235 char_u *buttons = NULL;
9236 char_u buf[NUMBUFLEN];
9237 char_u buf2[NUMBUFLEN];
9238 int def = 1;
9239 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 char_u *typestr;
9241 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 message = get_tv_string_chk(&argvars[0]);
9244 if (message == NULL)
9245 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009246 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9249 if (buttons == NULL)
9250 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9257 if (typestr == NULL)
9258 error = TRUE;
9259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 switch (TOUPPER_ASC(*typestr))
9262 {
9263 case 'E': type = VIM_ERROR; break;
9264 case 'Q': type = VIM_QUESTION; break;
9265 case 'I': type = VIM_INFO; break;
9266 case 'W': type = VIM_WARNING; break;
9267 case 'G': type = VIM_GENERIC; break;
9268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 }
9271 }
9272 }
9273
9274 if (buttons == NULL || *buttons == NUL)
9275 buttons = (char_u *)_("&Ok");
9276
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009277 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009278 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280#endif
9281}
9282
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009283/*
9284 * "copy()" function
9285 */
9286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009288 typval_T *argvars;
9289 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009290{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009291 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009294#ifdef FEAT_FLOAT
9295/*
9296 * "cos()" function
9297 */
9298 static void
9299f_cos(argvars, rettv)
9300 typval_T *argvars;
9301 typval_T *rettv;
9302{
9303 float_T f;
9304
9305 rettv->v_type = VAR_FLOAT;
9306 if (get_float_arg(argvars, &f) == OK)
9307 rettv->vval.v_float = cos(f);
9308 else
9309 rettv->vval.v_float = 0.0;
9310}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009311
9312/*
9313 * "cosh()" function
9314 */
9315 static void
9316f_cosh(argvars, rettv)
9317 typval_T *argvars;
9318 typval_T *rettv;
9319{
9320 float_T f;
9321
9322 rettv->v_type = VAR_FLOAT;
9323 if (get_float_arg(argvars, &f) == OK)
9324 rettv->vval.v_float = cosh(f);
9325 else
9326 rettv->vval.v_float = 0.0;
9327}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009328#endif
9329
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009331 * "count()" function
9332 */
9333 static void
9334f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *argvars;
9336 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009337{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009338 long n = 0;
9339 int ic = FALSE;
9340
Bram Moolenaare9a41262005-01-15 22:18:47 +00009341 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009342 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 listitem_T *li;
9344 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009345 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009346
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 if ((l = argvars[0].vval.v_list) != NULL)
9348 {
9349 li = l->lv_first;
9350 if (argvars[2].v_type != VAR_UNKNOWN)
9351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009352 int error = FALSE;
9353
9354 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009355 if (argvars[3].v_type != VAR_UNKNOWN)
9356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009357 idx = get_tv_number_chk(&argvars[3], &error);
9358 if (!error)
9359 {
9360 li = list_find(l, idx);
9361 if (li == NULL)
9362 EMSGN(_(e_listidx), idx);
9363 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 if (error)
9366 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009367 }
9368
9369 for ( ; li != NULL; li = li->li_next)
9370 if (tv_equal(&li->li_tv, &argvars[1], ic))
9371 ++n;
9372 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009374 else if (argvars[0].v_type == VAR_DICT)
9375 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 int todo;
9377 dict_T *d;
9378 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009379
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009380 if ((d = argvars[0].vval.v_dict) != NULL)
9381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382 int error = FALSE;
9383
Bram Moolenaare9a41262005-01-15 22:18:47 +00009384 if (argvars[2].v_type != VAR_UNKNOWN)
9385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 if (argvars[3].v_type != VAR_UNKNOWN)
9388 EMSG(_(e_invarg));
9389 }
9390
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009391 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009393 {
9394 if (!HASHITEM_EMPTY(hi))
9395 {
9396 --todo;
9397 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9398 ++n;
9399 }
9400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401 }
9402 }
9403 else
9404 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009405 rettv->vval.v_number = n;
9406}
9407
9408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9410 *
9411 * Checks the existence of a cscope connection.
9412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009415 typval_T *argvars UNUSED;
9416 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417{
9418#ifdef FEAT_CSCOPE
9419 int num = 0;
9420 char_u *dbpath = NULL;
9421 char_u *prepend = NULL;
9422 char_u buf[NUMBUFLEN];
9423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009424 if (argvars[0].v_type != VAR_UNKNOWN
9425 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 num = (int)get_tv_number(&argvars[0]);
9428 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 }
9432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#endif
9435}
9436
9437/*
9438 * "cursor(lnum, col)" function
9439 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009440 * Moves the cursor to the specified line and column.
9441 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009449#ifdef FEAT_VIRTUALEDIT
9450 long coladd = 0;
9451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009453 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009454 if (argvars[1].v_type == VAR_UNKNOWN)
9455 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009456 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009457
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009458 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009459 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009460 line = pos.lnum;
9461 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009462#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009463 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009464#endif
9465 }
9466 else
9467 {
9468 line = get_tv_lnum(argvars);
9469 col = get_tv_number_chk(&argvars[1], NULL);
9470#ifdef FEAT_VIRTUALEDIT
9471 if (argvars[2].v_type != VAR_UNKNOWN)
9472 coladd = get_tv_number_chk(&argvars[2], NULL);
9473#endif
9474 }
9475 if (line < 0 || col < 0
9476#ifdef FEAT_VIRTUALEDIT
9477 || coladd < 0
9478#endif
9479 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (line > 0)
9482 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (col > 0)
9484 curwin->w_cursor.col = col - 1;
9485#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009486 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487#endif
9488
9489 /* Make sure the cursor is in a valid position. */
9490 check_cursor();
9491#ifdef FEAT_MBYTE
9492 /* Correct cursor for multi-byte character. */
9493 if (has_mbyte)
9494 mb_adjust_cursor();
9495#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009496
9497 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009498 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499}
9500
9501/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009502 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 */
9504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009506 typval_T *argvars;
9507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009509 int noref = 0;
9510
9511 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009513 if (noref < 0 || noref > 1)
9514 EMSG(_(e_invarg));
9515 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009516 {
9517 current_copyID += COPYID_INC;
9518 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520}
9521
9522/*
9523 * "delete()" function
9524 */
9525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 typval_T *argvars;
9528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
9536/*
9537 * "did_filetype()" function
9538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009541 typval_T *argvars UNUSED;
9542 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546#endif
9547}
9548
9549/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009550 * "diff_filler()" function
9551 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009554 typval_T *argvars UNUSED;
9555 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009556{
9557#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009559#endif
9560}
9561
9562/*
9563 * "diff_hlID()" function
9564 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009567 typval_T *argvars UNUSED;
9568 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009569{
9570#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009572 static linenr_T prev_lnum = 0;
9573 static int changedtick = 0;
9574 static int fnum = 0;
9575 static int change_start = 0;
9576 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009577 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009578 int filler_lines;
9579 int col;
9580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 if (lnum < 0) /* ignore type error in {lnum} arg */
9582 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009583 if (lnum != prev_lnum
9584 || changedtick != curbuf->b_changedtick
9585 || fnum != curbuf->b_fnum)
9586 {
9587 /* New line, buffer, change: need to get the values. */
9588 filler_lines = diff_check(curwin, lnum);
9589 if (filler_lines < 0)
9590 {
9591 if (filler_lines == -1)
9592 {
9593 change_start = MAXCOL;
9594 change_end = -1;
9595 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9596 hlID = HLF_ADD; /* added line */
9597 else
9598 hlID = HLF_CHD; /* changed line */
9599 }
9600 else
9601 hlID = HLF_ADD; /* added line */
9602 }
9603 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009604 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009605 prev_lnum = lnum;
9606 changedtick = curbuf->b_changedtick;
9607 fnum = curbuf->b_fnum;
9608 }
9609
9610 if (hlID == HLF_CHD || hlID == HLF_TXD)
9611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009613 if (col >= change_start && col <= change_end)
9614 hlID = HLF_TXD; /* changed text */
9615 else
9616 hlID = HLF_CHD; /* changed line */
9617 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009618 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009619#endif
9620}
9621
9622/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009623 * "empty({expr})" function
9624 */
9625 static void
9626f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 typval_T *argvars;
9628 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009629{
9630 int n;
9631
9632 switch (argvars[0].v_type)
9633 {
9634 case VAR_STRING:
9635 case VAR_FUNC:
9636 n = argvars[0].vval.v_string == NULL
9637 || *argvars[0].vval.v_string == NUL;
9638 break;
9639 case VAR_NUMBER:
9640 n = argvars[0].vval.v_number == 0;
9641 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009642#ifdef FEAT_FLOAT
9643 case VAR_FLOAT:
9644 n = argvars[0].vval.v_float == 0.0;
9645 break;
9646#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009647 case VAR_LIST:
9648 n = argvars[0].vval.v_list == NULL
9649 || argvars[0].vval.v_list->lv_first == NULL;
9650 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 case VAR_DICT:
9652 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009653 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009655 default:
9656 EMSG2(_(e_intern2), "f_empty()");
9657 n = 0;
9658 }
9659
9660 rettv->vval.v_number = n;
9661}
9662
9663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 * "escape({string}, {chars})" function
9665 */
9666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009668 typval_T *argvars;
9669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671 char_u buf[NUMBUFLEN];
9672
Bram Moolenaar758711c2005-02-02 23:11:38 +00009673 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9674 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676}
9677
9678/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009679 * "eval()" function
9680 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009681 static void
9682f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009683 typval_T *argvars;
9684 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009685{
9686 char_u *s;
9687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 s = get_tv_string_chk(&argvars[0]);
9689 if (s != NULL)
9690 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009692 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9693 {
9694 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009695 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009696 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009697 else if (*s != NUL)
9698 EMSG(_(e_trailing));
9699}
9700
9701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 * "eventhandler()" function
9703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009706 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710}
9711
9712/*
9713 * "executable()" function
9714 */
9715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 typval_T *argvars;
9718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721}
9722
9723/*
9724 * "exists()" function
9725 */
9726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 char_u *p;
9732 char_u *name;
9733 int n = FALSE;
9734 int len = 0;
9735
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009736 no_autoload = TRUE;
9737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (*p == '$') /* environment variable */
9740 {
9741 /* first try "normal" environment variables (fast) */
9742 if (mch_getenv(p + 1) != NULL)
9743 n = TRUE;
9744 else
9745 {
9746 /* try expanding things like $VIM and ${HOME} */
9747 p = expand_env_save(p);
9748 if (p != NULL && *p != '$')
9749 n = TRUE;
9750 vim_free(p);
9751 }
9752 }
9753 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009756 if (*skipwhite(p) != NUL)
9757 n = FALSE; /* trailing garbage */
9758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 else if (*p == '*') /* internal or user defined function */
9760 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009761 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 }
9763 else if (*p == ':')
9764 {
9765 n = cmd_exists(p + 1);
9766 }
9767 else if (*p == '#')
9768 {
9769#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009770 if (p[1] == '#')
9771 n = autocmd_supported(p + 2);
9772 else
9773 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#endif
9775 }
9776 else /* internal variable */
9777 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009778 char_u *tofree;
9779 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009781 /* get_name_len() takes care of expanding curly braces */
9782 name = p;
9783 len = get_name_len(&p, &tofree, TRUE, FALSE);
9784 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009786 if (tofree != NULL)
9787 name = tofree;
9788 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9789 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009791 /* handle d.key, l[idx], f(expr) */
9792 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9793 if (n)
9794 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 }
9796 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009797 if (*p != NUL)
9798 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009800 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 }
9802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009804
9805 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009808#ifdef FEAT_FLOAT
9809/*
9810 * "exp()" function
9811 */
9812 static void
9813f_exp(argvars, rettv)
9814 typval_T *argvars;
9815 typval_T *rettv;
9816{
9817 float_T f;
9818
9819 rettv->v_type = VAR_FLOAT;
9820 if (get_float_arg(argvars, &f) == OK)
9821 rettv->vval.v_float = exp(f);
9822 else
9823 rettv->vval.v_float = 0.0;
9824}
9825#endif
9826
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827/*
9828 * "expand()" function
9829 */
9830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009832 typval_T *argvars;
9833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834{
9835 char_u *s;
9836 int len;
9837 char_u *errormsg;
9838 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9839 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->v_type = VAR_STRING;
9843 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 if (*s == '%' || *s == '#' || *s == '<')
9845 {
9846 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009847 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 --emsg_off;
9849 }
9850 else
9851 {
9852 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009853 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 if (argvars[1].v_type != VAR_UNKNOWN
9855 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 if (!error)
9858 {
9859 ExpandInit(&xpc);
9860 xpc.xp_context = EXPAND_FILES;
9861 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009862 }
9863 else
9864 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 }
9866}
9867
9868/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009869 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009870 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009871 */
9872 static void
9873f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009874 typval_T *argvars;
9875 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009877 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 list_T *l1, *l2;
9880 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009881 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009883
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884 l1 = argvars[0].vval.v_list;
9885 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009886 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9887 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009888 {
9889 if (argvars[2].v_type != VAR_UNKNOWN)
9890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009891 before = get_tv_number_chk(&argvars[2], &error);
9892 if (error)
9893 return; /* type error; errmsg already given */
9894
Bram Moolenaar758711c2005-02-02 23:11:38 +00009895 if (before == l1->lv_len)
9896 item = NULL;
9897 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009898 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009899 item = list_find(l1, before);
9900 if (item == NULL)
9901 {
9902 EMSGN(_(e_listidx), before);
9903 return;
9904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 }
9906 }
9907 else
9908 item = NULL;
9909 list_extend(l1, l2, item);
9910
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 copy_tv(&argvars[0], rettv);
9912 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009916 dict_T *d1, *d2;
9917 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 char_u *action;
9919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009921 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922
9923 d1 = argvars[0].vval.v_dict;
9924 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009925 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9926 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 {
9928 /* Check the third argument. */
9929 if (argvars[2].v_type != VAR_UNKNOWN)
9930 {
9931 static char *(av[]) = {"keep", "force", "error"};
9932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 action = get_tv_string_chk(&argvars[2]);
9934 if (action == NULL)
9935 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936 for (i = 0; i < 3; ++i)
9937 if (STRCMP(action, av[i]) == 0)
9938 break;
9939 if (i == 3)
9940 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009941 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009942 return;
9943 }
9944 }
9945 else
9946 action = (char_u *)"force";
9947
9948 /* Go over all entries in the second dict and add them to the
9949 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009950 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009951 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009953 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009955 --todo;
9956 di1 = dict_find(d1, hi2->hi_key, -1);
9957 if (di1 == NULL)
9958 {
9959 di1 = dictitem_copy(HI2DI(hi2));
9960 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9961 dictitem_free(di1);
9962 }
9963 else if (*action == 'e')
9964 {
9965 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9966 break;
9967 }
9968 else if (*action == 'f')
9969 {
9970 clear_tv(&di1->di_tv);
9971 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9972 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 }
9974 }
9975
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 copy_tv(&argvars[0], rettv);
9977 }
9978 }
9979 else
9980 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009981}
9982
9983/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009984 * "feedkeys()" function
9985 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009986 static void
9987f_feedkeys(argvars, rettv)
9988 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009989 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009990{
9991 int remap = TRUE;
9992 char_u *keys, *flags;
9993 char_u nbuf[NUMBUFLEN];
9994 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009995 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009996
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009997 /* This is not allowed in the sandbox. If the commands would still be
9998 * executed in the sandbox it would be OK, but it probably happens later,
9999 * when "sandbox" is no longer set. */
10000 if (check_secure())
10001 return;
10002
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010003 keys = get_tv_string(&argvars[0]);
10004 if (*keys != NUL)
10005 {
10006 if (argvars[1].v_type != VAR_UNKNOWN)
10007 {
10008 flags = get_tv_string_buf(&argvars[1], nbuf);
10009 for ( ; *flags != NUL; ++flags)
10010 {
10011 switch (*flags)
10012 {
10013 case 'n': remap = FALSE; break;
10014 case 'm': remap = TRUE; break;
10015 case 't': typed = TRUE; break;
10016 }
10017 }
10018 }
10019
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010020 /* Need to escape K_SPECIAL and CSI before putting the string in the
10021 * typeahead buffer. */
10022 keys_esc = vim_strsave_escape_csi(keys);
10023 if (keys_esc != NULL)
10024 {
10025 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010026 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010027 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010028 if (vgetc_busy)
10029 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010030 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010031 }
10032}
10033
10034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 * "filereadable()" function
10036 */
10037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *argvars;
10040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010042 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 char_u *p;
10044 int n;
10045
Bram Moolenaarc236c162008-07-13 17:41:49 +000010046#ifndef O_NONBLOCK
10047# define O_NONBLOCK 0
10048#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010050 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10051 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 {
10053 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010054 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056 else
10057 n = FALSE;
10058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060}
10061
10062/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010063 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 * rights to write into.
10065 */
10066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 typval_T *argvars;
10069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010071 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072}
10073
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010074static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010075
10076 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010077findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010078 typval_T *argvars;
10079 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010080 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010081{
10082#ifdef FEAT_SEARCHPATH
10083 char_u *fname;
10084 char_u *fresult = NULL;
10085 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10086 char_u *p;
10087 char_u pathbuf[NUMBUFLEN];
10088 int count = 1;
10089 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010090 int error = FALSE;
10091#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010092
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010093 rettv->vval.v_string = NULL;
10094 rettv->v_type = VAR_STRING;
10095
10096#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010098
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010099 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10102 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010103 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010104 else
10105 {
10106 if (*p != NUL)
10107 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010110 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010112 }
10113
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010114 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10115 error = TRUE;
10116
10117 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 do
10120 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010121 if (rettv->v_type == VAR_STRING)
10122 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 fresult = find_file_in_path_option(first ? fname : NULL,
10124 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010125 0, first, path,
10126 find_what,
10127 curbuf->b_ffname,
10128 find_what == FINDFILE_DIR
10129 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010131
10132 if (fresult != NULL && rettv->v_type == VAR_LIST)
10133 list_append_string(rettv->vval.v_list, fresult, -1);
10134
10135 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010137
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010138 if (rettv->v_type == VAR_STRING)
10139 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010140#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010141}
10142
Bram Moolenaar33570922005-01-25 22:26:29 +000010143static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10144static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010145
10146/*
10147 * Implementation of map() and filter().
10148 */
10149 static void
10150filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010153 int map;
10154{
10155 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010156 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 listitem_T *li, *nli;
10158 list_T *l = NULL;
10159 dictitem_T *di;
10160 hashtab_T *ht;
10161 hashitem_T *hi;
10162 dict_T *d = NULL;
10163 typval_T save_val;
10164 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010165 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010166 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010167 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010168 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010169 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010170
Bram Moolenaare9a41262005-01-15 22:18:47 +000010171 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010172 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010173 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010174 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 return;
10176 }
10177 else if (argvars[0].v_type == VAR_DICT)
10178 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010179 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010180 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010181 return;
10182 }
10183 else
10184 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010185 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010186 return;
10187 }
10188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 expr = get_tv_string_buf_chk(&argvars[1], buf);
10190 /* On type errors, the preceding call has already displayed an error
10191 * message. Avoid a misleading error message for an empty string that
10192 * was not passed as argument. */
10193 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 prepare_vimvar(VV_VAL, &save_val);
10196 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010197
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010198 /* We reset "did_emsg" to be able to detect whether an error
10199 * occurred during evaluation of the expression. */
10200 save_did_emsg = did_emsg;
10201 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010202
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010203 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 vimvars[VV_KEY].vv_type = VAR_STRING;
10207
10208 ht = &d->dv_hashtab;
10209 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010210 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010211 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 if (!HASHITEM_EMPTY(hi))
10214 {
10215 --todo;
10216 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010217 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 break;
10219 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010220 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010221 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 break;
10223 if (!map && rem)
10224 dictitem_remove(d, di);
10225 clear_tv(&vimvars[VV_KEY].vv_tv);
10226 }
10227 }
10228 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 }
10230 else
10231 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010232 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 for (li = l->lv_first; li != NULL; li = nli)
10235 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010236 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010237 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010239 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010240 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010241 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010242 break;
10243 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010245 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010246 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010247 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010248
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010249 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010251
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010252 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010254
10255 copy_tv(&argvars[0], rettv);
10256}
10257
10258 static int
10259filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010260 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010261 char_u *expr;
10262 int map;
10263 int *remp;
10264{
Bram Moolenaar33570922005-01-25 22:26:29 +000010265 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010267 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 s = expr;
10271 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010272 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 if (*s != NUL) /* check for trailing chars after expr */
10274 {
10275 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010276 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 }
10278 if (map)
10279 {
10280 /* map(): replace the list item value */
10281 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010282 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 *tv = rettv;
10284 }
10285 else
10286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 int error = FALSE;
10288
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 /* On type error, nothing has been removed; return FAIL to stop the
10293 * loop. The error message was given by get_tv_number_chk(). */
10294 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010295 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010297 retval = OK;
10298theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010300 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010301}
10302
10303/*
10304 * "filter()" function
10305 */
10306 static void
10307f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010308 typval_T *argvars;
10309 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010310{
10311 filter_map(argvars, rettv, FALSE);
10312}
10313
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010314/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010315 * "finddir({fname}[, {path}[, {count}]])" function
10316 */
10317 static void
10318f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010319 typval_T *argvars;
10320 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010321{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010322 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323}
10324
10325/*
10326 * "findfile({fname}[, {path}[, {count}]])" function
10327 */
10328 static void
10329f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010330 typval_T *argvars;
10331 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010332{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010333 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010334}
10335
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010336#ifdef FEAT_FLOAT
10337/*
10338 * "float2nr({float})" function
10339 */
10340 static void
10341f_float2nr(argvars, rettv)
10342 typval_T *argvars;
10343 typval_T *rettv;
10344{
10345 float_T f;
10346
10347 if (get_float_arg(argvars, &f) == OK)
10348 {
10349 if (f < -0x7fffffff)
10350 rettv->vval.v_number = -0x7fffffff;
10351 else if (f > 0x7fffffff)
10352 rettv->vval.v_number = 0x7fffffff;
10353 else
10354 rettv->vval.v_number = (varnumber_T)f;
10355 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010356}
10357
10358/*
10359 * "floor({float})" function
10360 */
10361 static void
10362f_floor(argvars, rettv)
10363 typval_T *argvars;
10364 typval_T *rettv;
10365{
10366 float_T f;
10367
10368 rettv->v_type = VAR_FLOAT;
10369 if (get_float_arg(argvars, &f) == OK)
10370 rettv->vval.v_float = floor(f);
10371 else
10372 rettv->vval.v_float = 0.0;
10373}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010374
10375/*
10376 * "fmod()" function
10377 */
10378 static void
10379f_fmod(argvars, rettv)
10380 typval_T *argvars;
10381 typval_T *rettv;
10382{
10383 float_T fx, fy;
10384
10385 rettv->v_type = VAR_FLOAT;
10386 if (get_float_arg(argvars, &fx) == OK
10387 && get_float_arg(&argvars[1], &fy) == OK)
10388 rettv->vval.v_float = fmod(fx, fy);
10389 else
10390 rettv->vval.v_float = 0.0;
10391}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010392#endif
10393
Bram Moolenaar0d660222005-01-07 21:51:51 +000010394/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010395 * "fnameescape({string})" function
10396 */
10397 static void
10398f_fnameescape(argvars, rettv)
10399 typval_T *argvars;
10400 typval_T *rettv;
10401{
10402 rettv->vval.v_string = vim_strsave_fnameescape(
10403 get_tv_string(&argvars[0]), FALSE);
10404 rettv->v_type = VAR_STRING;
10405}
10406
10407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 * "fnamemodify({fname}, {mods})" function
10409 */
10410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010411f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010412 typval_T *argvars;
10413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414{
10415 char_u *fname;
10416 char_u *mods;
10417 int usedlen = 0;
10418 int len;
10419 char_u *fbuf = NULL;
10420 char_u buf[NUMBUFLEN];
10421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 fname = get_tv_string_chk(&argvars[0]);
10423 mods = get_tv_string_buf_chk(&argvars[1], buf);
10424 if (fname == NULL || mods == NULL)
10425 fname = NULL;
10426 else
10427 {
10428 len = (int)STRLEN(fname);
10429 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 vim_free(fbuf);
10438}
10439
Bram Moolenaar33570922005-01-25 22:26:29 +000010440static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
10442/*
10443 * "foldclosed()" function
10444 */
10445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010447 typval_T *argvars;
10448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 int end;
10450{
10451#ifdef FEAT_FOLDING
10452 linenr_T lnum;
10453 linenr_T first, last;
10454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10457 {
10458 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10459 {
10460 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 return;
10465 }
10466 }
10467#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469}
10470
10471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010472 * "foldclosed()" function
10473 */
10474 static void
10475f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 typval_T *argvars;
10477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010478{
10479 foldclosed_both(argvars, rettv, FALSE);
10480}
10481
10482/*
10483 * "foldclosedend()" function
10484 */
10485 static void
10486f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 typval_T *argvars;
10488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010489{
10490 foldclosed_both(argvars, rettv, TRUE);
10491}
10492
10493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 * "foldlevel()" function
10495 */
10496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501#ifdef FEAT_FOLDING
10502 linenr_T lnum;
10503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508}
10509
10510/*
10511 * "foldtext()" function
10512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010515 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517{
10518#ifdef FEAT_FOLDING
10519 linenr_T lnum;
10520 char_u *s;
10521 char_u *r;
10522 int len;
10523 char *txt;
10524#endif
10525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 rettv->v_type = VAR_STRING;
10527 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010529 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10530 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10531 <= curbuf->b_ml.ml_line_count
10532 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 {
10534 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10536 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 {
10538 if (!linewhite(lnum))
10539 break;
10540 ++lnum;
10541 }
10542
10543 /* Find interesting text in this line. */
10544 s = skipwhite(ml_get(lnum));
10545 /* skip C comment-start */
10546 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010549 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010550 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010551 {
10552 s = skipwhite(ml_get(lnum + 1));
10553 if (*s == '*')
10554 s = skipwhite(s + 1);
10555 }
10556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 txt = _("+-%s%3ld lines: ");
10558 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 + 20 /* for %3ld */
10561 + STRLEN(s))); /* concatenated */
10562 if (r != NULL)
10563 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10565 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10566 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 len = (int)STRLEN(r);
10568 STRCAT(r, s);
10569 /* remove 'foldmarker' and 'commentstring' */
10570 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 }
10573 }
10574#endif
10575}
10576
10577/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010578 * "foldtextresult(lnum)" function
10579 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010582 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010584{
10585#ifdef FEAT_FOLDING
10586 linenr_T lnum;
10587 char_u *text;
10588 char_u buf[51];
10589 foldinfo_T foldinfo;
10590 int fold_count;
10591#endif
10592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->v_type = VAR_STRING;
10594 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010595#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 /* treat illegal types and illegal string values for {lnum} the same */
10598 if (lnum < 0)
10599 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010600 fold_count = foldedCount(curwin, lnum, &foldinfo);
10601 if (fold_count > 0)
10602 {
10603 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10604 &foldinfo, buf);
10605 if (text == buf)
10606 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010608 }
10609#endif
10610}
10611
10612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 * "foreground()" function
10614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010617 typval_T *argvars UNUSED;
10618 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620#ifdef FEAT_GUI
10621 if (gui.in_use)
10622 gui_mch_set_foreground();
10623#else
10624# ifdef WIN32
10625 win32_set_foreground();
10626# endif
10627#endif
10628}
10629
10630/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010631 * "function()" function
10632 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T *argvars;
10636 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010637{
10638 char_u *s;
10639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010641 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010642 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010643 /* Don't check an autoload name for existence here. */
10644 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010645 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010646 else
10647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 rettv->vval.v_string = vim_strsave(s);
10649 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010650 }
10651}
10652
10653/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010654 * "garbagecollect()" function
10655 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010656 static void
10657f_garbagecollect(argvars, rettv)
10658 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010659 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010660{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010661 /* This is postponed until we are back at the toplevel, because we may be
10662 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10663 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010664
10665 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10666 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010667}
10668
10669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010670 * "get()" function
10671 */
10672 static void
10673f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010674 typval_T *argvars;
10675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676{
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 listitem_T *li;
10678 list_T *l;
10679 dictitem_T *di;
10680 dict_T *d;
10681 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010682
Bram Moolenaare9a41262005-01-15 22:18:47 +000010683 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010684 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 int error = FALSE;
10688
10689 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10690 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010691 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010694 else if (argvars[0].v_type == VAR_DICT)
10695 {
10696 if ((d = argvars[0].vval.v_dict) != NULL)
10697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010698 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010699 if (di != NULL)
10700 tv = &di->di_tv;
10701 }
10702 }
10703 else
10704 EMSG2(_(e_listdictarg), "get()");
10705
10706 if (tv == NULL)
10707 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 copy_tv(&argvars[2], rettv);
10710 }
10711 else
10712 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010713}
10714
Bram Moolenaar342337a2005-07-21 21:11:17 +000010715static 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 +000010716
10717/*
10718 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010719 * Return a range (from start to end) of lines in rettv from the specified
10720 * buffer.
10721 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010722 */
10723 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010724get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010725 buf_T *buf;
10726 linenr_T start;
10727 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010728 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010729 typval_T *rettv;
10730{
10731 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010732
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010733 if (retlist && rettv_list_alloc(rettv) == FAIL)
10734 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010735
10736 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10737 return;
10738
10739 if (!retlist)
10740 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010741 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10742 p = ml_get_buf(buf, start, FALSE);
10743 else
10744 p = (char_u *)"";
10745
10746 rettv->v_type = VAR_STRING;
10747 rettv->vval.v_string = vim_strsave(p);
10748 }
10749 else
10750 {
10751 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010752 return;
10753
10754 if (start < 1)
10755 start = 1;
10756 if (end > buf->b_ml.ml_line_count)
10757 end = buf->b_ml.ml_line_count;
10758 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010759 if (list_append_string(rettv->vval.v_list,
10760 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010761 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010762 }
10763}
10764
10765/*
10766 * "getbufline()" function
10767 */
10768 static void
10769f_getbufline(argvars, rettv)
10770 typval_T *argvars;
10771 typval_T *rettv;
10772{
10773 linenr_T lnum;
10774 linenr_T end;
10775 buf_T *buf;
10776
10777 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10778 ++emsg_off;
10779 buf = get_buf_tv(&argvars[0]);
10780 --emsg_off;
10781
Bram Moolenaar661b1822005-07-28 22:36:45 +000010782 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010783 if (argvars[2].v_type == VAR_UNKNOWN)
10784 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010785 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010786 end = get_tv_lnum_buf(&argvars[2], buf);
10787
Bram Moolenaar342337a2005-07-21 21:11:17 +000010788 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010789}
10790
Bram Moolenaar0d660222005-01-07 21:51:51 +000010791/*
10792 * "getbufvar()" function
10793 */
10794 static void
10795f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 typval_T *argvars;
10797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798{
10799 buf_T *buf;
10800 buf_T *save_curbuf;
10801 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010802 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10805 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010806 ++emsg_off;
10807 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808
10809 rettv->v_type = VAR_STRING;
10810 rettv->vval.v_string = NULL;
10811
10812 if (buf != NULL && varname != NULL)
10813 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010814 /* set curbuf to be our buf, temporarily */
10815 save_curbuf = curbuf;
10816 curbuf = buf;
10817
Bram Moolenaar0d660222005-01-07 21:51:51 +000010818 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010819 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820 else
10821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 if (*varname == NUL)
10823 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10824 * scope prefix before the NUL byte is required by
10825 * find_var_in_ht(). */
10826 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010827 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010828 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010832
10833 /* restore previous notion of curbuf */
10834 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010835 }
10836
10837 --emsg_off;
10838}
10839
10840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 * "getchar()" function
10842 */
10843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 typval_T *argvars;
10846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
10848 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010851 /* Position the cursor. Needed after a message that ends in a space. */
10852 windgoto(msg_row, msg_col);
10853
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 ++no_mapping;
10855 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010856 for (;;)
10857 {
10858 if (argvars[0].v_type == VAR_UNKNOWN)
10859 /* getchar(): blocking wait. */
10860 n = safe_vgetc();
10861 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10862 /* getchar(1): only check if char avail */
10863 n = vpeekc();
10864 else if (error || vpeekc() == NUL)
10865 /* illegal argument or getchar(0) and no char avail: return zero */
10866 n = 0;
10867 else
10868 /* getchar(0) and char avail: return char */
10869 n = safe_vgetc();
10870 if (n == K_IGNORE)
10871 continue;
10872 break;
10873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 --no_mapping;
10875 --allow_keys;
10876
Bram Moolenaar219b8702006-11-01 14:32:36 +000010877 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10878 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10879 vimvars[VV_MOUSE_COL].vv_nr = 0;
10880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 if (IS_SPECIAL(n) || mod_mask != 0)
10883 {
10884 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10885 int i = 0;
10886
10887 /* Turn a special key into three bytes, plus modifier. */
10888 if (mod_mask != 0)
10889 {
10890 temp[i++] = K_SPECIAL;
10891 temp[i++] = KS_MODIFIER;
10892 temp[i++] = mod_mask;
10893 }
10894 if (IS_SPECIAL(n))
10895 {
10896 temp[i++] = K_SPECIAL;
10897 temp[i++] = K_SECOND(n);
10898 temp[i++] = K_THIRD(n);
10899 }
10900#ifdef FEAT_MBYTE
10901 else if (has_mbyte)
10902 i += (*mb_char2bytes)(n, temp + i);
10903#endif
10904 else
10905 temp[i++] = n;
10906 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 rettv->v_type = VAR_STRING;
10908 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010909
10910#ifdef FEAT_MOUSE
10911 if (n == K_LEFTMOUSE
10912 || n == K_LEFTMOUSE_NM
10913 || n == K_LEFTDRAG
10914 || n == K_LEFTRELEASE
10915 || n == K_LEFTRELEASE_NM
10916 || n == K_MIDDLEMOUSE
10917 || n == K_MIDDLEDRAG
10918 || n == K_MIDDLERELEASE
10919 || n == K_RIGHTMOUSE
10920 || n == K_RIGHTDRAG
10921 || n == K_RIGHTRELEASE
10922 || n == K_X1MOUSE
10923 || n == K_X1DRAG
10924 || n == K_X1RELEASE
10925 || n == K_X2MOUSE
10926 || n == K_X2DRAG
10927 || n == K_X2RELEASE
10928 || n == K_MOUSEDOWN
10929 || n == K_MOUSEUP)
10930 {
10931 int row = mouse_row;
10932 int col = mouse_col;
10933 win_T *win;
10934 linenr_T lnum;
10935# ifdef FEAT_WINDOWS
10936 win_T *wp;
10937# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010938 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010939
10940 if (row >= 0 && col >= 0)
10941 {
10942 /* Find the window at the mouse coordinates and compute the
10943 * text position. */
10944 win = mouse_find_win(&row, &col);
10945 (void)mouse_comp_pos(win, &row, &col, &lnum);
10946# ifdef FEAT_WINDOWS
10947 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010948 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010949# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010950 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010951 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10952 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10953 }
10954 }
10955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 }
10957}
10958
10959/*
10960 * "getcharmod()" function
10961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968}
10969
10970/*
10971 * "getcmdline()" function
10972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->v_type = VAR_STRING;
10979 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
10983 * "getcmdpos()" function
10984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010987 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991}
10992
10993/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010994 * "getcmdtype()" function
10995 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010996 static void
10997f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010998 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010999 typval_T *rettv;
11000{
11001 rettv->v_type = VAR_STRING;
11002 rettv->vval.v_string = alloc(2);
11003 if (rettv->vval.v_string != NULL)
11004 {
11005 rettv->vval.v_string[0] = get_cmdline_type();
11006 rettv->vval.v_string[1] = NUL;
11007 }
11008}
11009
11010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 * "getcwd()" function
11012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011015 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
11018 char_u cwd[MAXPATHL];
11019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 else
11024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011027 if (rettv->vval.v_string != NULL)
11028 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029#endif
11030 }
11031}
11032
11033/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011034 * "getfontname()" function
11035 */
11036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011039 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->v_type = VAR_STRING;
11042 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011043#ifdef FEAT_GUI
11044 if (gui.in_use)
11045 {
11046 GuiFont font;
11047 char_u *name = NULL;
11048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011049 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011050 {
11051 /* Get the "Normal" font. Either the name saved by
11052 * hl_set_font_name() or from the font ID. */
11053 font = gui.norm_font;
11054 name = hl_get_font_name();
11055 }
11056 else
11057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011059 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11060 return;
11061 font = gui_mch_get_font(name, FALSE);
11062 if (font == NOFONT)
11063 return; /* Invalid font name, return empty string. */
11064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011066 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011067 gui_mch_free_font(font);
11068 }
11069#endif
11070}
11071
11072/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011073 * "getfperm({fname})" function
11074 */
11075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011077 typval_T *argvars;
11078 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011079{
11080 char_u *fname;
11081 struct stat st;
11082 char_u *perm = NULL;
11083 char_u flags[] = "rwx";
11084 int i;
11085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011089 if (mch_stat((char *)fname, &st) >= 0)
11090 {
11091 perm = vim_strsave((char_u *)"---------");
11092 if (perm != NULL)
11093 {
11094 for (i = 0; i < 9; i++)
11095 {
11096 if (st.st_mode & (1 << (8 - i)))
11097 perm[i] = flags[i % 3];
11098 }
11099 }
11100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011102}
11103
11104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 * "getfsize({fname})" function
11106 */
11107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011109 typval_T *argvars;
11110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
11112 char_u *fname;
11113 struct stat st;
11114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
11119 if (mch_stat((char *)fname, &st) >= 0)
11120 {
11121 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011126
11127 /* non-perfect check for overflow */
11128 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11129 rettv->vval.v_number = -2;
11130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 }
11132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134}
11135
11136/*
11137 * "getftime({fname})" function
11138 */
11139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
11144 char_u *fname;
11145 struct stat st;
11146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148
11149 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153}
11154
11155/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011156 * "getftype({fname})" function
11157 */
11158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *argvars;
11161 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011162{
11163 char_u *fname;
11164 struct stat st;
11165 char_u *type = NULL;
11166 char *t;
11167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011171 if (mch_lstat((char *)fname, &st) >= 0)
11172 {
11173#ifdef S_ISREG
11174 if (S_ISREG(st.st_mode))
11175 t = "file";
11176 else if (S_ISDIR(st.st_mode))
11177 t = "dir";
11178# ifdef S_ISLNK
11179 else if (S_ISLNK(st.st_mode))
11180 t = "link";
11181# endif
11182# ifdef S_ISBLK
11183 else if (S_ISBLK(st.st_mode))
11184 t = "bdev";
11185# endif
11186# ifdef S_ISCHR
11187 else if (S_ISCHR(st.st_mode))
11188 t = "cdev";
11189# endif
11190# ifdef S_ISFIFO
11191 else if (S_ISFIFO(st.st_mode))
11192 t = "fifo";
11193# endif
11194# ifdef S_ISSOCK
11195 else if (S_ISSOCK(st.st_mode))
11196 t = "fifo";
11197# endif
11198 else
11199 t = "other";
11200#else
11201# ifdef S_IFMT
11202 switch (st.st_mode & S_IFMT)
11203 {
11204 case S_IFREG: t = "file"; break;
11205 case S_IFDIR: t = "dir"; break;
11206# ifdef S_IFLNK
11207 case S_IFLNK: t = "link"; break;
11208# endif
11209# ifdef S_IFBLK
11210 case S_IFBLK: t = "bdev"; break;
11211# endif
11212# ifdef S_IFCHR
11213 case S_IFCHR: t = "cdev"; break;
11214# endif
11215# ifdef S_IFIFO
11216 case S_IFIFO: t = "fifo"; break;
11217# endif
11218# ifdef S_IFSOCK
11219 case S_IFSOCK: t = "socket"; break;
11220# endif
11221 default: t = "other";
11222 }
11223# else
11224 if (mch_isdir(fname))
11225 t = "dir";
11226 else
11227 t = "file";
11228# endif
11229#endif
11230 type = vim_strsave((char_u *)t);
11231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011233}
11234
11235/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011236 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237 */
11238 static void
11239f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 typval_T *argvars;
11241 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242{
11243 linenr_T lnum;
11244 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011245 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246
11247 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011248 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011249 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011250 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011251 retlist = FALSE;
11252 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011253 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011254 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011256 retlist = TRUE;
11257 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011258
Bram Moolenaar342337a2005-07-21 21:11:17 +000011259 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011260}
11261
11262/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011263 * "getmatches()" function
11264 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011265 static void
11266f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011267 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011268 typval_T *rettv;
11269{
11270#ifdef FEAT_SEARCH_EXTRA
11271 dict_T *dict;
11272 matchitem_T *cur = curwin->w_match_head;
11273
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011274 if (rettv_list_alloc(rettv) == OK)
11275 {
11276 while (cur != NULL)
11277 {
11278 dict = dict_alloc();
11279 if (dict == NULL)
11280 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011281 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11282 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11283 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11284 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11285 list_append_dict(rettv->vval.v_list, dict);
11286 cur = cur->next;
11287 }
11288 }
11289#endif
11290}
11291
11292/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011293 * "getpid()" function
11294 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011295 static void
11296f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011297 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011298 typval_T *rettv;
11299{
11300 rettv->vval.v_number = mch_get_pid();
11301}
11302
11303/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011304 * "getpos(string)" function
11305 */
11306 static void
11307f_getpos(argvars, rettv)
11308 typval_T *argvars;
11309 typval_T *rettv;
11310{
11311 pos_T *fp;
11312 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011313 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011314
11315 if (rettv_list_alloc(rettv) == OK)
11316 {
11317 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011318 fp = var2fpos(&argvars[0], TRUE, &fnum);
11319 if (fnum != -1)
11320 list_append_number(l, (varnumber_T)fnum);
11321 else
11322 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011323 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11324 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011325 list_append_number(l, (fp != NULL)
11326 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011327 : (varnumber_T)0);
11328 list_append_number(l,
11329#ifdef FEAT_VIRTUALEDIT
11330 (fp != NULL) ? (varnumber_T)fp->coladd :
11331#endif
11332 (varnumber_T)0);
11333 }
11334 else
11335 rettv->vval.v_number = FALSE;
11336}
11337
11338/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011339 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011340 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011341 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011342f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011343 typval_T *argvars UNUSED;
11344 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011345{
11346#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011347 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011348#endif
11349
Bram Moolenaar2641f772005-03-25 21:58:17 +000011350#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011351 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011352 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011353 wp = NULL;
11354 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11355 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011356 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011357 if (wp == NULL)
11358 return;
11359 }
11360
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011361 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011362 }
11363#endif
11364}
11365
11366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 * "getreg()" function
11368 */
11369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011371 typval_T *argvars;
11372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
11374 char_u *strregname;
11375 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011376 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011379 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 strregname = get_tv_string_chk(&argvars[0]);
11382 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011387 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 regname = (strregname == NULL ? '"' : *strregname);
11389 if (regname == 0)
11390 regname = '"';
11391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 rettv->vval.v_string = error ? NULL :
11394 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395}
11396
11397/*
11398 * "getregtype()" function
11399 */
11400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 typval_T *argvars;
11403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404{
11405 char_u *strregname;
11406 int regname;
11407 char_u buf[NUMBUFLEN + 2];
11408 long reglen = 0;
11409
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011410 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 {
11412 strregname = get_tv_string_chk(&argvars[0]);
11413 if (strregname == NULL) /* type error; errmsg already given */
11414 {
11415 rettv->v_type = VAR_STRING;
11416 rettv->vval.v_string = NULL;
11417 return;
11418 }
11419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 else
11421 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011422 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423
11424 regname = (strregname == NULL ? '"' : *strregname);
11425 if (regname == 0)
11426 regname = '"';
11427
11428 buf[0] = NUL;
11429 buf[1] = NUL;
11430 switch (get_reg_type(regname, &reglen))
11431 {
11432 case MLINE: buf[0] = 'V'; break;
11433 case MCHAR: buf[0] = 'v'; break;
11434#ifdef FEAT_VISUAL
11435 case MBLOCK:
11436 buf[0] = Ctrl_V;
11437 sprintf((char *)buf + 1, "%ld", reglen + 1);
11438 break;
11439#endif
11440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->v_type = VAR_STRING;
11442 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443}
11444
11445/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011446 * "gettabvar()" function
11447 */
11448 static void
11449f_gettabvar(argvars, rettv)
11450 typval_T *argvars;
11451 typval_T *rettv;
11452{
11453 tabpage_T *tp;
11454 dictitem_T *v;
11455 char_u *varname;
11456
11457 rettv->v_type = VAR_STRING;
11458 rettv->vval.v_string = NULL;
11459
11460 varname = get_tv_string_chk(&argvars[1]);
11461 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11462 if (tp != NULL && varname != NULL)
11463 {
11464 /* look up the variable */
11465 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11466 if (v != NULL)
11467 copy_tv(&v->di_tv, rettv);
11468 }
11469}
11470
11471/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011472 * "gettabwinvar()" function
11473 */
11474 static void
11475f_gettabwinvar(argvars, rettv)
11476 typval_T *argvars;
11477 typval_T *rettv;
11478{
11479 getwinvar(argvars, rettv, 1);
11480}
11481
11482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 * "getwinposx()" function
11484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011487 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491#ifdef FEAT_GUI
11492 if (gui.in_use)
11493 {
11494 int x, y;
11495
11496 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 }
11499#endif
11500}
11501
11502/*
11503 * "getwinposy()" function
11504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511#ifdef FEAT_GUI
11512 if (gui.in_use)
11513 {
11514 int x, y;
11515
11516 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519#endif
11520}
11521
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011522/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011523 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011524 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011525 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011526find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011527 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011528 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011529{
11530#ifdef FEAT_WINDOWS
11531 win_T *wp;
11532#endif
11533 int nr;
11534
11535 nr = get_tv_number_chk(vp, NULL);
11536
11537#ifdef FEAT_WINDOWS
11538 if (nr < 0)
11539 return NULL;
11540 if (nr == 0)
11541 return curwin;
11542
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011543 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11544 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011545 if (--nr <= 0)
11546 break;
11547 return wp;
11548#else
11549 if (nr == 0 || nr == 1)
11550 return curwin;
11551 return NULL;
11552#endif
11553}
11554
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555/*
11556 * "getwinvar()" function
11557 */
11558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 typval_T *argvars;
11561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011563 getwinvar(argvars, rettv, 0);
11564}
11565
11566/*
11567 * getwinvar() and gettabwinvar()
11568 */
11569 static void
11570getwinvar(argvars, rettv, off)
11571 typval_T *argvars;
11572 typval_T *rettv;
11573 int off; /* 1 for gettabwinvar() */
11574{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 win_T *win, *oldcurwin;
11576 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011578 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011580#ifdef FEAT_WINDOWS
11581 if (off == 1)
11582 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11583 else
11584 tp = curtab;
11585#endif
11586 win = find_win_by_nr(&argvars[off], tp);
11587 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011588 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 rettv->v_type = VAR_STRING;
11591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592
11593 if (win != NULL && varname != NULL)
11594 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011595 /* Set curwin to be our win, temporarily. Also set curbuf, so
11596 * that we can get buffer-local options. */
11597 oldcurwin = curwin;
11598 curwin = win;
11599 curbuf = win->w_buffer;
11600
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 else
11604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 if (*varname == NUL)
11606 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11607 * scope prefix before the NUL byte is required by
11608 * find_var_in_ht(). */
11609 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011611 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011613 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011615
11616 /* restore previous notion of curwin */
11617 curwin = oldcurwin;
11618 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 }
11620
11621 --emsg_off;
11622}
11623
11624/*
11625 * "glob()" function
11626 */
11627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011629 typval_T *argvars;
11630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011632 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011634 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011636 /* When the optional second argument is non-zero, don't remove matches
11637 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11638 if (argvars[1].v_type != VAR_UNKNOWN
11639 && get_tv_number_chk(&argvars[1], &error))
11640 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011642 if (!error)
11643 {
11644 ExpandInit(&xpc);
11645 xpc.xp_context = EXPAND_FILES;
11646 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11647 NULL, flags, WILD_ALL);
11648 }
11649 else
11650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651}
11652
11653/*
11654 * "globpath()" function
11655 */
11656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 typval_T *argvars;
11659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011661 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011663 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011664 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011666 /* When the optional second argument is non-zero, don't remove matches
11667 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11668 if (argvars[2].v_type != VAR_UNKNOWN
11669 && get_tv_number_chk(&argvars[2], &error))
11670 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011672 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 rettv->vval.v_string = NULL;
11674 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011675 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11676 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677}
11678
11679/*
11680 * "has()" function
11681 */
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
11687 int i;
11688 char_u *name;
11689 int n = FALSE;
11690 static char *(has_list[]) =
11691 {
11692#ifdef AMIGA
11693 "amiga",
11694# ifdef FEAT_ARP
11695 "arp",
11696# endif
11697#endif
11698#ifdef __BEOS__
11699 "beos",
11700#endif
11701#ifdef MSDOS
11702# ifdef DJGPP
11703 "dos32",
11704# else
11705 "dos16",
11706# endif
11707#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011708#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 "mac",
11710#endif
11711#if defined(MACOS_X_UNIX)
11712 "macunix",
11713#endif
11714#ifdef OS2
11715 "os2",
11716#endif
11717#ifdef __QNX__
11718 "qnx",
11719#endif
11720#ifdef RISCOS
11721 "riscos",
11722#endif
11723#ifdef UNIX
11724 "unix",
11725#endif
11726#ifdef VMS
11727 "vms",
11728#endif
11729#ifdef WIN16
11730 "win16",
11731#endif
11732#ifdef WIN32
11733 "win32",
11734#endif
11735#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11736 "win32unix",
11737#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011738#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 "win64",
11740#endif
11741#ifdef EBCDIC
11742 "ebcdic",
11743#endif
11744#ifndef CASE_INSENSITIVE_FILENAME
11745 "fname_case",
11746#endif
11747#ifdef FEAT_ARABIC
11748 "arabic",
11749#endif
11750#ifdef FEAT_AUTOCMD
11751 "autocmd",
11752#endif
11753#ifdef FEAT_BEVAL
11754 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011755# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11756 "balloon_multiline",
11757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758#endif
11759#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11760 "builtin_terms",
11761# ifdef ALL_BUILTIN_TCAPS
11762 "all_builtin_terms",
11763# endif
11764#endif
11765#ifdef FEAT_BYTEOFF
11766 "byte_offset",
11767#endif
11768#ifdef FEAT_CINDENT
11769 "cindent",
11770#endif
11771#ifdef FEAT_CLIENTSERVER
11772 "clientserver",
11773#endif
11774#ifdef FEAT_CLIPBOARD
11775 "clipboard",
11776#endif
11777#ifdef FEAT_CMDL_COMPL
11778 "cmdline_compl",
11779#endif
11780#ifdef FEAT_CMDHIST
11781 "cmdline_hist",
11782#endif
11783#ifdef FEAT_COMMENTS
11784 "comments",
11785#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011786#ifdef FEAT_CONCEAL
11787 "conceal",
11788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789#ifdef FEAT_CRYPT
11790 "cryptv",
11791#endif
11792#ifdef FEAT_CSCOPE
11793 "cscope",
11794#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011795#ifdef FEAT_CURSORBIND
11796 "cursorbind",
11797#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011798#ifdef CURSOR_SHAPE
11799 "cursorshape",
11800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801#ifdef DEBUG
11802 "debug",
11803#endif
11804#ifdef FEAT_CON_DIALOG
11805 "dialog_con",
11806#endif
11807#ifdef FEAT_GUI_DIALOG
11808 "dialog_gui",
11809#endif
11810#ifdef FEAT_DIFF
11811 "diff",
11812#endif
11813#ifdef FEAT_DIGRAPHS
11814 "digraphs",
11815#endif
11816#ifdef FEAT_DND
11817 "dnd",
11818#endif
11819#ifdef FEAT_EMACS_TAGS
11820 "emacs_tags",
11821#endif
11822 "eval", /* always present, of course! */
11823#ifdef FEAT_EX_EXTRA
11824 "ex_extra",
11825#endif
11826#ifdef FEAT_SEARCH_EXTRA
11827 "extra_search",
11828#endif
11829#ifdef FEAT_FKMAP
11830 "farsi",
11831#endif
11832#ifdef FEAT_SEARCHPATH
11833 "file_in_path",
11834#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011835#if defined(UNIX) && !defined(USE_SYSTEM)
11836 "filterpipe",
11837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838#ifdef FEAT_FIND_ID
11839 "find_in_path",
11840#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011841#ifdef FEAT_FLOAT
11842 "float",
11843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844#ifdef FEAT_FOLDING
11845 "folding",
11846#endif
11847#ifdef FEAT_FOOTER
11848 "footer",
11849#endif
11850#if !defined(USE_SYSTEM) && defined(UNIX)
11851 "fork",
11852#endif
11853#ifdef FEAT_GETTEXT
11854 "gettext",
11855#endif
11856#ifdef FEAT_GUI
11857 "gui",
11858#endif
11859#ifdef FEAT_GUI_ATHENA
11860# ifdef FEAT_GUI_NEXTAW
11861 "gui_neXtaw",
11862# else
11863 "gui_athena",
11864# endif
11865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866#ifdef FEAT_GUI_GTK
11867 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011870#ifdef FEAT_GUI_GNOME
11871 "gui_gnome",
11872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873#ifdef FEAT_GUI_MAC
11874 "gui_mac",
11875#endif
11876#ifdef FEAT_GUI_MOTIF
11877 "gui_motif",
11878#endif
11879#ifdef FEAT_GUI_PHOTON
11880 "gui_photon",
11881#endif
11882#ifdef FEAT_GUI_W16
11883 "gui_win16",
11884#endif
11885#ifdef FEAT_GUI_W32
11886 "gui_win32",
11887#endif
11888#ifdef FEAT_HANGULIN
11889 "hangul_input",
11890#endif
11891#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11892 "iconv",
11893#endif
11894#ifdef FEAT_INS_EXPAND
11895 "insert_expand",
11896#endif
11897#ifdef FEAT_JUMPLIST
11898 "jumplist",
11899#endif
11900#ifdef FEAT_KEYMAP
11901 "keymap",
11902#endif
11903#ifdef FEAT_LANGMAP
11904 "langmap",
11905#endif
11906#ifdef FEAT_LIBCALL
11907 "libcall",
11908#endif
11909#ifdef FEAT_LINEBREAK
11910 "linebreak",
11911#endif
11912#ifdef FEAT_LISP
11913 "lispindent",
11914#endif
11915#ifdef FEAT_LISTCMDS
11916 "listcmds",
11917#endif
11918#ifdef FEAT_LOCALMAP
11919 "localmap",
11920#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011921#ifdef FEAT_LUA
11922# ifndef DYNAMIC_LUA
11923 "lua",
11924# endif
11925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926#ifdef FEAT_MENU
11927 "menu",
11928#endif
11929#ifdef FEAT_SESSION
11930 "mksession",
11931#endif
11932#ifdef FEAT_MODIFY_FNAME
11933 "modify_fname",
11934#endif
11935#ifdef FEAT_MOUSE
11936 "mouse",
11937#endif
11938#ifdef FEAT_MOUSESHAPE
11939 "mouseshape",
11940#endif
11941#if defined(UNIX) || defined(VMS)
11942# ifdef FEAT_MOUSE_DEC
11943 "mouse_dec",
11944# endif
11945# ifdef FEAT_MOUSE_GPM
11946 "mouse_gpm",
11947# endif
11948# ifdef FEAT_MOUSE_JSB
11949 "mouse_jsbterm",
11950# endif
11951# ifdef FEAT_MOUSE_NET
11952 "mouse_netterm",
11953# endif
11954# ifdef FEAT_MOUSE_PTERM
11955 "mouse_pterm",
11956# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011957# ifdef FEAT_SYSMOUSE
11958 "mouse_sysmouse",
11959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960# ifdef FEAT_MOUSE_XTERM
11961 "mouse_xterm",
11962# endif
11963#endif
11964#ifdef FEAT_MBYTE
11965 "multi_byte",
11966#endif
11967#ifdef FEAT_MBYTE_IME
11968 "multi_byte_ime",
11969#endif
11970#ifdef FEAT_MULTI_LANG
11971 "multi_lang",
11972#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011973#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011974#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011975 "mzscheme",
11976#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978#ifdef FEAT_OLE
11979 "ole",
11980#endif
11981#ifdef FEAT_OSFILETYPE
11982 "osfiletype",
11983#endif
11984#ifdef FEAT_PATH_EXTRA
11985 "path_extra",
11986#endif
11987#ifdef FEAT_PERL
11988#ifndef DYNAMIC_PERL
11989 "perl",
11990#endif
11991#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011992#ifdef FEAT_PERSISTENT_UNDO
11993 "persistent_undo",
11994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995#ifdef FEAT_PYTHON
11996#ifndef DYNAMIC_PYTHON
11997 "python",
11998#endif
11999#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012000#ifdef FEAT_PYTHON3
12001#ifndef DYNAMIC_PYTHON3
12002 "python3",
12003#endif
12004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005#ifdef FEAT_POSTSCRIPT
12006 "postscript",
12007#endif
12008#ifdef FEAT_PRINTER
12009 "printer",
12010#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012011#ifdef FEAT_PROFILE
12012 "profile",
12013#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012014#ifdef FEAT_RELTIME
12015 "reltime",
12016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017#ifdef FEAT_QUICKFIX
12018 "quickfix",
12019#endif
12020#ifdef FEAT_RIGHTLEFT
12021 "rightleft",
12022#endif
12023#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12024 "ruby",
12025#endif
12026#ifdef FEAT_SCROLLBIND
12027 "scrollbind",
12028#endif
12029#ifdef FEAT_CMDL_INFO
12030 "showcmd",
12031 "cmdline_info",
12032#endif
12033#ifdef FEAT_SIGNS
12034 "signs",
12035#endif
12036#ifdef FEAT_SMARTINDENT
12037 "smartindent",
12038#endif
12039#ifdef FEAT_SNIFF
12040 "sniff",
12041#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012042#ifdef STARTUPTIME
12043 "startuptime",
12044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045#ifdef FEAT_STL_OPT
12046 "statusline",
12047#endif
12048#ifdef FEAT_SUN_WORKSHOP
12049 "sun_workshop",
12050#endif
12051#ifdef FEAT_NETBEANS_INTG
12052 "netbeans_intg",
12053#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012054#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012055 "spell",
12056#endif
12057#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 "syntax",
12059#endif
12060#if defined(USE_SYSTEM) || !defined(UNIX)
12061 "system",
12062#endif
12063#ifdef FEAT_TAG_BINS
12064 "tag_binary",
12065#endif
12066#ifdef FEAT_TAG_OLDSTATIC
12067 "tag_old_static",
12068#endif
12069#ifdef FEAT_TAG_ANYWHITE
12070 "tag_any_white",
12071#endif
12072#ifdef FEAT_TCL
12073# ifndef DYNAMIC_TCL
12074 "tcl",
12075# endif
12076#endif
12077#ifdef TERMINFO
12078 "terminfo",
12079#endif
12080#ifdef FEAT_TERMRESPONSE
12081 "termresponse",
12082#endif
12083#ifdef FEAT_TEXTOBJ
12084 "textobjects",
12085#endif
12086#ifdef HAVE_TGETENT
12087 "tgetent",
12088#endif
12089#ifdef FEAT_TITLE
12090 "title",
12091#endif
12092#ifdef FEAT_TOOLBAR
12093 "toolbar",
12094#endif
12095#ifdef FEAT_USR_CMDS
12096 "user-commands", /* was accidentally included in 5.4 */
12097 "user_commands",
12098#endif
12099#ifdef FEAT_VIMINFO
12100 "viminfo",
12101#endif
12102#ifdef FEAT_VERTSPLIT
12103 "vertsplit",
12104#endif
12105#ifdef FEAT_VIRTUALEDIT
12106 "virtualedit",
12107#endif
12108#ifdef FEAT_VISUAL
12109 "visual",
12110#endif
12111#ifdef FEAT_VISUALEXTRA
12112 "visualextra",
12113#endif
12114#ifdef FEAT_VREPLACE
12115 "vreplace",
12116#endif
12117#ifdef FEAT_WILDIGN
12118 "wildignore",
12119#endif
12120#ifdef FEAT_WILDMENU
12121 "wildmenu",
12122#endif
12123#ifdef FEAT_WINDOWS
12124 "windows",
12125#endif
12126#ifdef FEAT_WAK
12127 "winaltkeys",
12128#endif
12129#ifdef FEAT_WRITEBACKUP
12130 "writebackup",
12131#endif
12132#ifdef FEAT_XIM
12133 "xim",
12134#endif
12135#ifdef FEAT_XFONTSET
12136 "xfontset",
12137#endif
12138#ifdef USE_XSMP
12139 "xsmp",
12140#endif
12141#ifdef USE_XSMP_INTERACT
12142 "xsmp_interact",
12143#endif
12144#ifdef FEAT_XCLIPBOARD
12145 "xterm_clipboard",
12146#endif
12147#ifdef FEAT_XTERM_SAVE
12148 "xterm_save",
12149#endif
12150#if defined(UNIX) && defined(FEAT_X11)
12151 "X11",
12152#endif
12153 NULL
12154 };
12155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 for (i = 0; has_list[i] != NULL; ++i)
12158 if (STRICMP(name, has_list[i]) == 0)
12159 {
12160 n = TRUE;
12161 break;
12162 }
12163
12164 if (n == FALSE)
12165 {
12166 if (STRNICMP(name, "patch", 5) == 0)
12167 n = has_patch(atoi((char *)name + 5));
12168 else if (STRICMP(name, "vim_starting") == 0)
12169 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012170#ifdef FEAT_MBYTE
12171 else if (STRICMP(name, "multi_byte_encoding") == 0)
12172 n = has_mbyte;
12173#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012174#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12175 else if (STRICMP(name, "balloon_multiline") == 0)
12176 n = multiline_balloon_available();
12177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178#ifdef DYNAMIC_TCL
12179 else if (STRICMP(name, "tcl") == 0)
12180 n = tcl_enabled(FALSE);
12181#endif
12182#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12183 else if (STRICMP(name, "iconv") == 0)
12184 n = iconv_enabled(FALSE);
12185#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012186#ifdef DYNAMIC_LUA
12187 else if (STRICMP(name, "lua") == 0)
12188 n = lua_enabled(FALSE);
12189#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012190#ifdef DYNAMIC_MZSCHEME
12191 else if (STRICMP(name, "mzscheme") == 0)
12192 n = mzscheme_enabled(FALSE);
12193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#ifdef DYNAMIC_RUBY
12195 else if (STRICMP(name, "ruby") == 0)
12196 n = ruby_enabled(FALSE);
12197#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012198#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199#ifdef DYNAMIC_PYTHON
12200 else if (STRICMP(name, "python") == 0)
12201 n = python_enabled(FALSE);
12202#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012203#endif
12204#ifdef FEAT_PYTHON3
12205#ifdef DYNAMIC_PYTHON3
12206 else if (STRICMP(name, "python3") == 0)
12207 n = python3_enabled(FALSE);
12208#endif
12209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210#ifdef DYNAMIC_PERL
12211 else if (STRICMP(name, "perl") == 0)
12212 n = perl_enabled(FALSE);
12213#endif
12214#ifdef FEAT_GUI
12215 else if (STRICMP(name, "gui_running") == 0)
12216 n = (gui.in_use || gui.starting);
12217# ifdef FEAT_GUI_W32
12218 else if (STRICMP(name, "gui_win32s") == 0)
12219 n = gui_is_win32s();
12220# endif
12221# ifdef FEAT_BROWSE
12222 else if (STRICMP(name, "browse") == 0)
12223 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12224# endif
12225#endif
12226#ifdef FEAT_SYN_HL
12227 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012228 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229#endif
12230#if defined(WIN3264)
12231 else if (STRICMP(name, "win95") == 0)
12232 n = mch_windows95();
12233#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012234#ifdef FEAT_NETBEANS_INTG
12235 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012236 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 }
12239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241}
12242
12243/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012244 * "has_key()" function
12245 */
12246 static void
12247f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012248 typval_T *argvars;
12249 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012250{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012251 if (argvars[0].v_type != VAR_DICT)
12252 {
12253 EMSG(_(e_dictreq));
12254 return;
12255 }
12256 if (argvars[0].vval.v_dict == NULL)
12257 return;
12258
12259 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012260 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012261}
12262
12263/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012264 * "haslocaldir()" function
12265 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012266 static void
12267f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012268 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012269 typval_T *rettv;
12270{
12271 rettv->vval.v_number = (curwin->w_localdir != NULL);
12272}
12273
12274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 * "hasmapto()" function
12276 */
12277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012279 typval_T *argvars;
12280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281{
12282 char_u *name;
12283 char_u *mode;
12284 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012285 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012287 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012288 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 mode = (char_u *)"nvo";
12290 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012293 if (argvars[2].v_type != VAR_UNKNOWN)
12294 abbr = get_tv_number(&argvars[2]);
12295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296
Bram Moolenaar2c932302006-03-18 21:42:09 +000012297 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301}
12302
12303/*
12304 * "histadd()" function
12305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012308 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310{
12311#ifdef FEAT_CMDHIST
12312 int histype;
12313 char_u *str;
12314 char_u buf[NUMBUFLEN];
12315#endif
12316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 if (check_restricted() || check_secure())
12319 return;
12320#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012321 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12322 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 if (histype >= 0)
12324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 if (*str != NUL)
12327 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012328 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 return;
12332 }
12333 }
12334#endif
12335}
12336
12337/*
12338 * "histdel()" function
12339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012342 typval_T *argvars UNUSED;
12343 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344{
12345#ifdef FEAT_CMDHIST
12346 int n;
12347 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012348 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12351 if (str == NULL)
12352 n = 0;
12353 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012355 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012356 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012358 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 else
12361 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 get_tv_string_buf(&argvars[1], buf));
12364 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365#endif
12366}
12367
12368/*
12369 * "histget()" function
12370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375{
12376#ifdef FEAT_CMDHIST
12377 int type;
12378 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012381 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12382 if (str == NULL)
12383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 {
12386 type = get_histtype(str);
12387 if (argvars[1].v_type == VAR_UNKNOWN)
12388 idx = get_history_idx(type);
12389 else
12390 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12391 /* -1 on type error */
12392 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012395 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398}
12399
12400/*
12401 * "histnr()" function
12402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012405 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407{
12408 int i;
12409
12410#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 char_u *history = get_tv_string_chk(&argvars[0]);
12412
12413 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414 if (i >= HIST_CMD && i < HIST_COUNT)
12415 i = get_history_idx(i);
12416 else
12417#endif
12418 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420}
12421
12422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 * "highlightID(name)" function
12424 */
12425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012427 typval_T *argvars;
12428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431}
12432
12433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434 * "highlight_exists()" function
12435 */
12436 static void
12437f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012438 typval_T *argvars;
12439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012440{
12441 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12442}
12443
12444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 * "hostname()" function
12446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012449 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451{
12452 char_u hostname[256];
12453
12454 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 rettv->v_type = VAR_STRING;
12456 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457}
12458
12459/*
12460 * iconv() function
12461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466{
12467#ifdef FEAT_MBYTE
12468 char_u buf1[NUMBUFLEN];
12469 char_u buf2[NUMBUFLEN];
12470 char_u *from, *to, *str;
12471 vimconv_T vimconv;
12472#endif
12473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 rettv->v_type = VAR_STRING;
12475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476
12477#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 str = get_tv_string(&argvars[0]);
12479 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12480 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 vimconv.vc_type = CONV_NONE;
12482 convert_setup(&vimconv, from, to);
12483
12484 /* If the encodings are equal, no conversion needed. */
12485 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489
12490 convert_setup(&vimconv, NULL, NULL);
12491 vim_free(from);
12492 vim_free(to);
12493#endif
12494}
12495
12496/*
12497 * "indent()" function
12498 */
12499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012501 typval_T *argvars;
12502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
12504 linenr_T lnum;
12505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511}
12512
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012513/*
12514 * "index()" function
12515 */
12516 static void
12517f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012518 typval_T *argvars;
12519 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012520{
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 list_T *l;
12522 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012523 long idx = 0;
12524 int ic = FALSE;
12525
12526 rettv->vval.v_number = -1;
12527 if (argvars[0].v_type != VAR_LIST)
12528 {
12529 EMSG(_(e_listreq));
12530 return;
12531 }
12532 l = argvars[0].vval.v_list;
12533 if (l != NULL)
12534 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012535 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012536 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 int error = FALSE;
12539
Bram Moolenaar758711c2005-02-02 23:11:38 +000012540 /* Start at specified item. Use the cached index that list_find()
12541 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012543 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012544 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 ic = get_tv_number_chk(&argvars[3], &error);
12546 if (error)
12547 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012548 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012549
Bram Moolenaar758711c2005-02-02 23:11:38 +000012550 for ( ; item != NULL; item = item->li_next, ++idx)
12551 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012552 {
12553 rettv->vval.v_number = idx;
12554 break;
12555 }
12556 }
12557}
12558
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559static int inputsecret_flag = 0;
12560
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012561static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12562
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012564 * This function is used by f_input() and f_inputdialog() functions. The third
12565 * argument to f_input() specifies the type of completion to use at the
12566 * prompt. The third argument to f_inputdialog() specifies the value to return
12567 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568 */
12569 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012570get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *argvars;
12572 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012573 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012575 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 char_u *p = NULL;
12577 int c;
12578 char_u buf[NUMBUFLEN];
12579 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012581 int xp_type = EXPAND_NOTHING;
12582 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586
12587#ifdef NO_CONSOLE_INPUT
12588 /* While starting up, there is no place to enter text. */
12589 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591#endif
12592
12593 cmd_silent = FALSE; /* Want to see the prompt. */
12594 if (prompt != NULL)
12595 {
12596 /* Only the part of the message after the last NL is considered as
12597 * prompt for the command line */
12598 p = vim_strrchr(prompt, '\n');
12599 if (p == NULL)
12600 p = prompt;
12601 else
12602 {
12603 ++p;
12604 c = *p;
12605 *p = NUL;
12606 msg_start();
12607 msg_clr_eos();
12608 msg_puts_attr(prompt, echo_attr);
12609 msg_didout = FALSE;
12610 msg_starthere();
12611 *p = c;
12612 }
12613 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 if (argvars[1].v_type != VAR_UNKNOWN)
12616 {
12617 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12618 if (defstr != NULL)
12619 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012621 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012622 {
12623 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012624 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012625 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012626
Bram Moolenaar4463f292005-09-25 22:20:24 +000012627 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012628
Bram Moolenaar4463f292005-09-25 22:20:24 +000012629 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12630 if (xp_name == NULL)
12631 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012633 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012634
Bram Moolenaar4463f292005-09-25 22:20:24 +000012635 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12636 &xp_arg) == FAIL)
12637 return;
12638 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012639 }
12640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 if (defstr != NULL)
12642 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012643 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12644 xp_type, xp_arg);
12645
12646 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012648 /* since the user typed this, no need to wait for return */
12649 need_wait_return = FALSE;
12650 msg_didout = FALSE;
12651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 cmd_silent = cmd_silent_save;
12653}
12654
12655/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012656 * "input()" function
12657 * Also handles inputsecret() when inputsecret is set.
12658 */
12659 static void
12660f_input(argvars, rettv)
12661 typval_T *argvars;
12662 typval_T *rettv;
12663{
12664 get_user_input(argvars, rettv, FALSE);
12665}
12666
12667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 * "inputdialog()" function
12669 */
12670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 typval_T *argvars;
12673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674{
12675#if defined(FEAT_GUI_TEXTDIALOG)
12676 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12677 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12678 {
12679 char_u *message;
12680 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012681 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 message = get_tv_string_chk(&argvars[0]);
12684 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012685 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012686 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 else
12688 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 if (message != NULL && defstr != NULL
12690 && do_dialog(VIM_QUESTION, NULL, message,
12691 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 else
12694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 if (message != NULL && defstr != NULL
12696 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012697 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 rettv->vval.v_string = vim_strsave(
12699 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 }
12705 else
12706#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012707 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708}
12709
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012710/*
12711 * "inputlist()" function
12712 */
12713 static void
12714f_inputlist(argvars, rettv)
12715 typval_T *argvars;
12716 typval_T *rettv;
12717{
12718 listitem_T *li;
12719 int selected;
12720 int mouse_used;
12721
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012722#ifdef NO_CONSOLE_INPUT
12723 /* While starting up, there is no place to enter text. */
12724 if (no_console_input())
12725 return;
12726#endif
12727 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12728 {
12729 EMSG2(_(e_listarg), "inputlist()");
12730 return;
12731 }
12732
12733 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012734 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012735 lines_left = Rows; /* avoid more prompt */
12736 msg_scroll = TRUE;
12737 msg_clr_eos();
12738
12739 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12740 {
12741 msg_puts(get_tv_string(&li->li_tv));
12742 msg_putchar('\n');
12743 }
12744
12745 /* Ask for choice. */
12746 selected = prompt_for_number(&mouse_used);
12747 if (mouse_used)
12748 selected -= lines_left;
12749
12750 rettv->vval.v_number = selected;
12751}
12752
12753
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12755
12756/*
12757 * "inputrestore()" function
12758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012761 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763{
12764 if (ga_userinput.ga_len > 0)
12765 {
12766 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12768 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012769 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 }
12771 else if (p_verbose > 1)
12772 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012773 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 }
12776}
12777
12778/*
12779 * "inputsave()" function
12780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012786 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 if (ga_grow(&ga_userinput, 1) == OK)
12788 {
12789 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12790 + ga_userinput.ga_len);
12791 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012792 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 }
12794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796}
12797
12798/*
12799 * "inputsecret()" function
12800 */
12801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012803 typval_T *argvars;
12804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805{
12806 ++cmdline_star;
12807 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 --cmdline_star;
12810 --inputsecret_flag;
12811}
12812
12813/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012814 * "insert()" function
12815 */
12816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012818 typval_T *argvars;
12819 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012820{
12821 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012822 listitem_T *item;
12823 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825
12826 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012828 else if ((l = argvars[0].vval.v_list) != NULL
12829 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012830 {
12831 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 before = get_tv_number_chk(&argvars[2], &error);
12833 if (error)
12834 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012835
Bram Moolenaar758711c2005-02-02 23:11:38 +000012836 if (before == l->lv_len)
12837 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012838 else
12839 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012840 item = list_find(l, before);
12841 if (item == NULL)
12842 {
12843 EMSGN(_(e_listidx), before);
12844 l = NULL;
12845 }
12846 }
12847 if (l != NULL)
12848 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012849 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012850 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012851 }
12852 }
12853}
12854
12855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 * "isdirectory()" function
12857 */
12858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *argvars;
12861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864}
12865
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012866/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012867 * "islocked()" function
12868 */
12869 static void
12870f_islocked(argvars, rettv)
12871 typval_T *argvars;
12872 typval_T *rettv;
12873{
12874 lval_T lv;
12875 char_u *end;
12876 dictitem_T *di;
12877
12878 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012879 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12880 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012881 if (end != NULL && lv.ll_name != NULL)
12882 {
12883 if (*end != NUL)
12884 EMSG(_(e_trailing));
12885 else
12886 {
12887 if (lv.ll_tv == NULL)
12888 {
12889 if (check_changedtick(lv.ll_name))
12890 rettv->vval.v_number = 1; /* always locked */
12891 else
12892 {
12893 di = find_var(lv.ll_name, NULL);
12894 if (di != NULL)
12895 {
12896 /* Consider a variable locked when:
12897 * 1. the variable itself is locked
12898 * 2. the value of the variable is locked.
12899 * 3. the List or Dict value is locked.
12900 */
12901 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12902 || tv_islocked(&di->di_tv));
12903 }
12904 }
12905 }
12906 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012907 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012908 else if (lv.ll_newkey != NULL)
12909 EMSG2(_(e_dictkey), lv.ll_newkey);
12910 else if (lv.ll_list != NULL)
12911 /* List item. */
12912 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12913 else
12914 /* Dictionary item. */
12915 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12916 }
12917 }
12918
12919 clear_lval(&lv);
12920}
12921
Bram Moolenaar33570922005-01-25 22:26:29 +000012922static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012923
12924/*
12925 * Turn a dict into a list:
12926 * "what" == 0: list of keys
12927 * "what" == 1: list of values
12928 * "what" == 2: list of items
12929 */
12930 static void
12931dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012934 int what;
12935{
Bram Moolenaar33570922005-01-25 22:26:29 +000012936 list_T *l2;
12937 dictitem_T *di;
12938 hashitem_T *hi;
12939 listitem_T *li;
12940 listitem_T *li2;
12941 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012942 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012943
Bram Moolenaar8c711452005-01-14 21:53:12 +000012944 if (argvars[0].v_type != VAR_DICT)
12945 {
12946 EMSG(_(e_dictreq));
12947 return;
12948 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012949 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012950 return;
12951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012952 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012953 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012954
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012955 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012958 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012959 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012960 --todo;
12961 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012962
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012963 li = listitem_alloc();
12964 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012965 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012966 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012967
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012968 if (what == 0)
12969 {
12970 /* keys() */
12971 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012972 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012973 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12974 }
12975 else if (what == 1)
12976 {
12977 /* values() */
12978 copy_tv(&di->di_tv, &li->li_tv);
12979 }
12980 else
12981 {
12982 /* items() */
12983 l2 = list_alloc();
12984 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012985 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012986 li->li_tv.vval.v_list = l2;
12987 if (l2 == NULL)
12988 break;
12989 ++l2->lv_refcount;
12990
12991 li2 = listitem_alloc();
12992 if (li2 == NULL)
12993 break;
12994 list_append(l2, li2);
12995 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012996 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012997 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12998
12999 li2 = listitem_alloc();
13000 if (li2 == NULL)
13001 break;
13002 list_append(l2, li2);
13003 copy_tv(&di->di_tv, &li2->li_tv);
13004 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013005 }
13006 }
13007}
13008
13009/*
13010 * "items(dict)" function
13011 */
13012 static void
13013f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013014 typval_T *argvars;
13015 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013016{
13017 dict_list(argvars, rettv, 2);
13018}
13019
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013021 * "join()" function
13022 */
13023 static void
13024f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013025 typval_T *argvars;
13026 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013027{
13028 garray_T ga;
13029 char_u *sep;
13030
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013031 if (argvars[0].v_type != VAR_LIST)
13032 {
13033 EMSG(_(e_listreq));
13034 return;
13035 }
13036 if (argvars[0].vval.v_list == NULL)
13037 return;
13038 if (argvars[1].v_type == VAR_UNKNOWN)
13039 sep = (char_u *)" ";
13040 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013042
13043 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013044
13045 if (sep != NULL)
13046 {
13047 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013048 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049 ga_append(&ga, NUL);
13050 rettv->vval.v_string = (char_u *)ga.ga_data;
13051 }
13052 else
13053 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013054}
13055
13056/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057 * "keys()" function
13058 */
13059 static void
13060f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013061 typval_T *argvars;
13062 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013063{
13064 dict_list(argvars, rettv, 0);
13065}
13066
13067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 * "last_buffer_nr()" function.
13069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013072 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074{
13075 int n = 0;
13076 buf_T *buf;
13077
13078 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13079 if (n < buf->b_fnum)
13080 n = buf->b_fnum;
13081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013083}
13084
13085/*
13086 * "len()" function
13087 */
13088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013089f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013090 typval_T *argvars;
13091 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013092{
13093 switch (argvars[0].v_type)
13094 {
13095 case VAR_STRING:
13096 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097 rettv->vval.v_number = (varnumber_T)STRLEN(
13098 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013099 break;
13100 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013102 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013103 case VAR_DICT:
13104 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13105 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013106 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013107 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013108 break;
13109 }
13110}
13111
Bram Moolenaar33570922005-01-25 22:26:29 +000013112static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013113
13114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *argvars;
13117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013118 int type;
13119{
13120#ifdef FEAT_LIBCALL
13121 char_u *string_in;
13122 char_u **string_result;
13123 int nr_result;
13124#endif
13125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013127 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013129
13130 if (check_restricted() || check_secure())
13131 return;
13132
13133#ifdef FEAT_LIBCALL
13134 /* The first two args must be strings, otherwise its meaningless */
13135 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13136 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013137 string_in = NULL;
13138 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013139 string_in = argvars[2].vval.v_string;
13140 if (type == VAR_NUMBER)
13141 string_result = NULL;
13142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013144 if (mch_libcall(argvars[0].vval.v_string,
13145 argvars[1].vval.v_string,
13146 string_in,
13147 argvars[2].vval.v_number,
13148 string_result,
13149 &nr_result) == OK
13150 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013151 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013152 }
13153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154}
13155
13156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013157 * "libcall()" function
13158 */
13159 static void
13160f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *argvars;
13162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013163{
13164 libcall_common(argvars, rettv, VAR_STRING);
13165}
13166
13167/*
13168 * "libcallnr()" function
13169 */
13170 static void
13171f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *argvars;
13173 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013174{
13175 libcall_common(argvars, rettv, VAR_NUMBER);
13176}
13177
13178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 * "line(string)" function
13180 */
13181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 typval_T *argvars;
13184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185{
13186 linenr_T lnum = 0;
13187 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013188 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013190 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 if (fp != NULL)
13192 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194}
13195
13196/*
13197 * "line2byte(lnum)" function
13198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013201 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203{
13204#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206#else
13207 linenr_T lnum;
13208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13214 if (rettv->vval.v_number >= 0)
13215 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216#endif
13217}
13218
13219/*
13220 * "lispindent(lnum)" function
13221 */
13222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013224 typval_T *argvars;
13225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226{
13227#ifdef FEAT_LISP
13228 pos_T pos;
13229 linenr_T lnum;
13230
13231 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13234 {
13235 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 curwin->w_cursor = pos;
13238 }
13239 else
13240#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242}
13243
13244/*
13245 * "localtime()" function
13246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253}
13254
Bram Moolenaar33570922005-01-25 22:26:29 +000013255static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256
13257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013259 typval_T *argvars;
13260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 int exact;
13262{
13263 char_u *keys;
13264 char_u *which;
13265 char_u buf[NUMBUFLEN];
13266 char_u *keys_buf = NULL;
13267 char_u *rhs;
13268 int mode;
13269 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013270 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271
13272 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 rettv->v_type = VAR_STRING;
13274 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 if (*keys == NUL)
13278 return;
13279
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013283 if (argvars[2].v_type != VAR_UNKNOWN)
13284 abbr = get_tv_number(&argvars[2]);
13285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 else
13287 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288 if (which == NULL)
13289 return;
13290
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 mode = get_map_mode(&which, 0);
13292
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013293 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013294 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 vim_free(keys_buf);
13296 if (rhs != NULL)
13297 {
13298 ga_init(&ga);
13299 ga.ga_itemsize = 1;
13300 ga.ga_growsize = 40;
13301
13302 while (*rhs != NUL)
13303 ga_concat(&ga, str2special(&rhs, FALSE));
13304
13305 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 }
13308}
13309
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013310#ifdef FEAT_FLOAT
13311/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013312 * "log()" function
13313 */
13314 static void
13315f_log(argvars, rettv)
13316 typval_T *argvars;
13317 typval_T *rettv;
13318{
13319 float_T f;
13320
13321 rettv->v_type = VAR_FLOAT;
13322 if (get_float_arg(argvars, &f) == OK)
13323 rettv->vval.v_float = log(f);
13324 else
13325 rettv->vval.v_float = 0.0;
13326}
13327
13328/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013329 * "log10()" function
13330 */
13331 static void
13332f_log10(argvars, rettv)
13333 typval_T *argvars;
13334 typval_T *rettv;
13335{
13336 float_T f;
13337
13338 rettv->v_type = VAR_FLOAT;
13339 if (get_float_arg(argvars, &f) == OK)
13340 rettv->vval.v_float = log10(f);
13341 else
13342 rettv->vval.v_float = 0.0;
13343}
13344#endif
13345
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013347 * "map()" function
13348 */
13349 static void
13350f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013351 typval_T *argvars;
13352 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013353{
13354 filter_map(argvars, rettv, TRUE);
13355}
13356
13357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013358 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 */
13360 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013361f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013365 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366}
13367
13368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 */
13371 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013372f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 typval_T *argvars;
13374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377}
13378
Bram Moolenaar33570922005-01-25 22:26:29 +000013379static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380
13381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *argvars;
13384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 int type;
13386{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013387 char_u *str = NULL;
13388 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 char_u *pat;
13390 regmatch_T regmatch;
13391 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013392 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393 char_u *save_cpo;
13394 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013395 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013396 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013397 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 list_T *l = NULL;
13399 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013400 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013401 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402
13403 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13404 save_cpo = p_cpo;
13405 p_cpo = (char_u *)"";
13406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013407 rettv->vval.v_number = -1;
13408 if (type == 3)
13409 {
13410 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013411 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013412 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013413 }
13414 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416 rettv->v_type = VAR_STRING;
13417 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013420 if (argvars[0].v_type == VAR_LIST)
13421 {
13422 if ((l = argvars[0].vval.v_list) == NULL)
13423 goto theend;
13424 li = l->lv_first;
13425 }
13426 else
13427 expr = str = get_tv_string(&argvars[0]);
13428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013429 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13430 if (pat == NULL)
13431 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 int error = FALSE;
13436
13437 start = get_tv_number_chk(&argvars[2], &error);
13438 if (error)
13439 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013440 if (l != NULL)
13441 {
13442 li = list_find(l, start);
13443 if (li == NULL)
13444 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013445 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013446 }
13447 else
13448 {
13449 if (start < 0)
13450 start = 0;
13451 if (start > (long)STRLEN(str))
13452 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013453 /* When "count" argument is there ignore matches before "start",
13454 * otherwise skip part of the string. Differs when pattern is "^"
13455 * or "\<". */
13456 if (argvars[3].v_type != VAR_UNKNOWN)
13457 startcol = start;
13458 else
13459 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013460 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013461
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013462 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 nth = get_tv_number_chk(&argvars[3], &error);
13464 if (error)
13465 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 }
13467
13468 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13469 if (regmatch.regprog != NULL)
13470 {
13471 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013472
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013473 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013474 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013475 if (l != NULL)
13476 {
13477 if (li == NULL)
13478 {
13479 match = FALSE;
13480 break;
13481 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013482 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013483 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013484 if (str == NULL)
13485 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013486 }
13487
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013488 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013489
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013490 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013491 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013492 if (l == NULL && !match)
13493 break;
13494
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013495 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013496 if (l != NULL)
13497 {
13498 li = li->li_next;
13499 ++idx;
13500 }
13501 else
13502 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013503#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013504 startcol = (colnr_T)(regmatch.startp[0]
13505 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013506#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013507 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013508#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013509 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013510 }
13511
13512 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013514 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013516 int i;
13517
13518 /* return list with matched string and submatches */
13519 for (i = 0; i < NSUBEXP; ++i)
13520 {
13521 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013522 {
13523 if (list_append_string(rettv->vval.v_list,
13524 (char_u *)"", 0) == FAIL)
13525 break;
13526 }
13527 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013528 regmatch.startp[i],
13529 (int)(regmatch.endp[i] - regmatch.startp[i]))
13530 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013531 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013532 }
13533 }
13534 else if (type == 2)
13535 {
13536 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013537 if (l != NULL)
13538 copy_tv(&li->li_tv, rettv);
13539 else
13540 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013542 }
13543 else if (l != NULL)
13544 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 else
13546 {
13547 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 (varnumber_T)(regmatch.startp[0] - str);
13550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013553 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 }
13555 }
13556 vim_free(regmatch.regprog);
13557 }
13558
13559theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013560 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 p_cpo = save_cpo;
13562}
13563
13564/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013565 * "match()" function
13566 */
13567 static void
13568f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571{
13572 find_some_match(argvars, rettv, 1);
13573}
13574
13575/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013576 * "matchadd()" function
13577 */
13578 static void
13579f_matchadd(argvars, rettv)
13580 typval_T *argvars;
13581 typval_T *rettv;
13582{
13583#ifdef FEAT_SEARCH_EXTRA
13584 char_u buf[NUMBUFLEN];
13585 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13586 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13587 int prio = 10; /* default priority */
13588 int id = -1;
13589 int error = FALSE;
13590
13591 rettv->vval.v_number = -1;
13592
13593 if (grp == NULL || pat == NULL)
13594 return;
13595 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013596 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013597 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013598 if (argvars[3].v_type != VAR_UNKNOWN)
13599 id = get_tv_number_chk(&argvars[3], &error);
13600 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013601 if (error == TRUE)
13602 return;
13603 if (id >= 1 && id <= 3)
13604 {
13605 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13606 return;
13607 }
13608
13609 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13610#endif
13611}
13612
13613/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013614 * "matcharg()" function
13615 */
13616 static void
13617f_matcharg(argvars, rettv)
13618 typval_T *argvars;
13619 typval_T *rettv;
13620{
13621 if (rettv_list_alloc(rettv) == OK)
13622 {
13623#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013624 int id = get_tv_number(&argvars[0]);
13625 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013626
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013627 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013628 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013629 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13630 {
13631 list_append_string(rettv->vval.v_list,
13632 syn_id2name(m->hlg_id), -1);
13633 list_append_string(rettv->vval.v_list, m->pattern, -1);
13634 }
13635 else
13636 {
13637 list_append_string(rettv->vval.v_list, NUL, -1);
13638 list_append_string(rettv->vval.v_list, NUL, -1);
13639 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013640 }
13641#endif
13642 }
13643}
13644
13645/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013646 * "matchdelete()" function
13647 */
13648 static void
13649f_matchdelete(argvars, rettv)
13650 typval_T *argvars;
13651 typval_T *rettv;
13652{
13653#ifdef FEAT_SEARCH_EXTRA
13654 rettv->vval.v_number = match_delete(curwin,
13655 (int)get_tv_number(&argvars[0]), TRUE);
13656#endif
13657}
13658
13659/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660 * "matchend()" function
13661 */
13662 static void
13663f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013666{
13667 find_some_match(argvars, rettv, 0);
13668}
13669
13670/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013671 * "matchlist()" function
13672 */
13673 static void
13674f_matchlist(argvars, rettv)
13675 typval_T *argvars;
13676 typval_T *rettv;
13677{
13678 find_some_match(argvars, rettv, 3);
13679}
13680
13681/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682 * "matchstr()" function
13683 */
13684 static void
13685f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013686 typval_T *argvars;
13687 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688{
13689 find_some_match(argvars, rettv, 2);
13690}
13691
Bram Moolenaar33570922005-01-25 22:26:29 +000013692static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013693
13694 static void
13695max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013696 typval_T *argvars;
13697 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013698 int domax;
13699{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013700 long n = 0;
13701 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013703
13704 if (argvars[0].v_type == VAR_LIST)
13705 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 list_T *l;
13707 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013708
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013709 l = argvars[0].vval.v_list;
13710 if (l != NULL)
13711 {
13712 li = l->lv_first;
13713 if (li != NULL)
13714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013716 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013717 {
13718 li = li->li_next;
13719 if (li == NULL)
13720 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013722 if (domax ? i > n : i < n)
13723 n = i;
13724 }
13725 }
13726 }
13727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013728 else if (argvars[0].v_type == VAR_DICT)
13729 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013730 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013731 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013733 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013734
13735 d = argvars[0].vval.v_dict;
13736 if (d != NULL)
13737 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013738 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013743 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013745 if (first)
13746 {
13747 n = i;
13748 first = FALSE;
13749 }
13750 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013751 n = i;
13752 }
13753 }
13754 }
13755 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013756 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013757 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013759}
13760
13761/*
13762 * "max()" function
13763 */
13764 static void
13765f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *argvars;
13767 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013768{
13769 max_min(argvars, rettv, TRUE);
13770}
13771
13772/*
13773 * "min()" function
13774 */
13775 static void
13776f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013779{
13780 max_min(argvars, rettv, FALSE);
13781}
13782
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013783static int mkdir_recurse __ARGS((char_u *dir, int prot));
13784
13785/*
13786 * Create the directory in which "dir" is located, and higher levels when
13787 * needed.
13788 */
13789 static int
13790mkdir_recurse(dir, prot)
13791 char_u *dir;
13792 int prot;
13793{
13794 char_u *p;
13795 char_u *updir;
13796 int r = FAIL;
13797
13798 /* Get end of directory name in "dir".
13799 * We're done when it's "/" or "c:/". */
13800 p = gettail_sep(dir);
13801 if (p <= get_past_head(dir))
13802 return OK;
13803
13804 /* If the directory exists we're done. Otherwise: create it.*/
13805 updir = vim_strnsave(dir, (int)(p - dir));
13806 if (updir == NULL)
13807 return FAIL;
13808 if (mch_isdir(updir))
13809 r = OK;
13810 else if (mkdir_recurse(updir, prot) == OK)
13811 r = vim_mkdir_emsg(updir, prot);
13812 vim_free(updir);
13813 return r;
13814}
13815
13816#ifdef vim_mkdir
13817/*
13818 * "mkdir()" function
13819 */
13820 static void
13821f_mkdir(argvars, rettv)
13822 typval_T *argvars;
13823 typval_T *rettv;
13824{
13825 char_u *dir;
13826 char_u buf[NUMBUFLEN];
13827 int prot = 0755;
13828
13829 rettv->vval.v_number = FAIL;
13830 if (check_restricted() || check_secure())
13831 return;
13832
13833 dir = get_tv_string_buf(&argvars[0], buf);
13834 if (argvars[1].v_type != VAR_UNKNOWN)
13835 {
13836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 prot = get_tv_number_chk(&argvars[2], NULL);
13838 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013839 mkdir_recurse(dir, prot);
13840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013842}
13843#endif
13844
Bram Moolenaar0d660222005-01-07 21:51:51 +000013845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 * "mode()" function
13847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 typval_T *argvars;
13851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013853 char_u buf[3];
13854
13855 buf[1] = NUL;
13856 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857
13858#ifdef FEAT_VISUAL
13859 if (VIsual_active)
13860 {
13861 if (VIsual_select)
13862 buf[0] = VIsual_mode + 's' - 'v';
13863 else
13864 buf[0] = VIsual_mode;
13865 }
13866 else
13867#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013868 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13869 || State == CONFIRM)
13870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013872 if (State == ASKMORE)
13873 buf[1] = 'm';
13874 else if (State == CONFIRM)
13875 buf[1] = '?';
13876 }
13877 else if (State == EXTERNCMD)
13878 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 else if (State & INSERT)
13880 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013881#ifdef FEAT_VREPLACE
13882 if (State & VREPLACE_FLAG)
13883 {
13884 buf[0] = 'R';
13885 buf[1] = 'v';
13886 }
13887 else
13888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 if (State & REPLACE_FLAG)
13890 buf[0] = 'R';
13891 else
13892 buf[0] = 'i';
13893 }
13894 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013897 if (exmode_active)
13898 buf[1] = 'v';
13899 }
13900 else if (exmode_active)
13901 {
13902 buf[0] = 'c';
13903 buf[1] = 'e';
13904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013908 if (finish_op)
13909 buf[1] = 'o';
13910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013912 /* Clear out the minor mode when the argument is not a non-zero number or
13913 * non-empty string. */
13914 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013915 buf[1] = NUL;
13916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 rettv->vval.v_string = vim_strsave(buf);
13918 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919}
13920
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013921#ifdef FEAT_MZSCHEME
13922/*
13923 * "mzeval()" function
13924 */
13925 static void
13926f_mzeval(argvars, rettv)
13927 typval_T *argvars;
13928 typval_T *rettv;
13929{
13930 char_u *str;
13931 char_u buf[NUMBUFLEN];
13932
13933 str = get_tv_string_buf(&argvars[0], buf);
13934 do_mzeval(str, rettv);
13935}
13936#endif
13937
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939 * "nextnonblank()" function
13940 */
13941 static void
13942f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013943 typval_T *argvars;
13944 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013945{
13946 linenr_T lnum;
13947
13948 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013951 {
13952 lnum = 0;
13953 break;
13954 }
13955 if (*skipwhite(ml_get(lnum)) != NUL)
13956 break;
13957 }
13958 rettv->vval.v_number = lnum;
13959}
13960
13961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 * "nr2char()" function
13963 */
13964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013966 typval_T *argvars;
13967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968{
13969 char_u buf[NUMBUFLEN];
13970
13971#ifdef FEAT_MBYTE
13972 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 else
13975#endif
13976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013977 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 buf[1] = NUL;
13979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013980 rettv->v_type = VAR_STRING;
13981 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013982}
13983
13984/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013985 * "pathshorten()" function
13986 */
13987 static void
13988f_pathshorten(argvars, rettv)
13989 typval_T *argvars;
13990 typval_T *rettv;
13991{
13992 char_u *p;
13993
13994 rettv->v_type = VAR_STRING;
13995 p = get_tv_string_chk(&argvars[0]);
13996 if (p == NULL)
13997 rettv->vval.v_string = NULL;
13998 else
13999 {
14000 p = vim_strsave(p);
14001 rettv->vval.v_string = p;
14002 if (p != NULL)
14003 shorten_dir(p);
14004 }
14005}
14006
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014007#ifdef FEAT_FLOAT
14008/*
14009 * "pow()" function
14010 */
14011 static void
14012f_pow(argvars, rettv)
14013 typval_T *argvars;
14014 typval_T *rettv;
14015{
14016 float_T fx, fy;
14017
14018 rettv->v_type = VAR_FLOAT;
14019 if (get_float_arg(argvars, &fx) == OK
14020 && get_float_arg(&argvars[1], &fy) == OK)
14021 rettv->vval.v_float = pow(fx, fy);
14022 else
14023 rettv->vval.v_float = 0.0;
14024}
14025#endif
14026
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028 * "prevnonblank()" function
14029 */
14030 static void
14031f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014032 typval_T *argvars;
14033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034{
14035 linenr_T lnum;
14036
14037 lnum = get_tv_lnum(argvars);
14038 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14039 lnum = 0;
14040 else
14041 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14042 --lnum;
14043 rettv->vval.v_number = lnum;
14044}
14045
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014046#ifdef HAVE_STDARG_H
14047/* This dummy va_list is here because:
14048 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14049 * - locally in the function results in a "used before set" warning
14050 * - using va_start() to initialize it gives "function with fixed args" error */
14051static va_list ap;
14052#endif
14053
Bram Moolenaar8c711452005-01-14 21:53:12 +000014054/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014055 * "printf()" function
14056 */
14057 static void
14058f_printf(argvars, rettv)
14059 typval_T *argvars;
14060 typval_T *rettv;
14061{
14062 rettv->v_type = VAR_STRING;
14063 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014064#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014065 {
14066 char_u buf[NUMBUFLEN];
14067 int len;
14068 char_u *s;
14069 int saved_did_emsg = did_emsg;
14070 char *fmt;
14071
14072 /* Get the required length, allocate the buffer and do it for real. */
14073 did_emsg = FALSE;
14074 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014075 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014076 if (!did_emsg)
14077 {
14078 s = alloc(len + 1);
14079 if (s != NULL)
14080 {
14081 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014082 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014083 }
14084 }
14085 did_emsg |= saved_did_emsg;
14086 }
14087#endif
14088}
14089
14090/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014091 * "pumvisible()" function
14092 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014093 static void
14094f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014095 typval_T *argvars UNUSED;
14096 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014097{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014098#ifdef FEAT_INS_EXPAND
14099 if (pum_visible())
14100 rettv->vval.v_number = 1;
14101#endif
14102}
14103
14104/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105 * "range()" function
14106 */
14107 static void
14108f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014111{
14112 long start;
14113 long end;
14114 long stride = 1;
14115 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119 if (argvars[1].v_type == VAR_UNKNOWN)
14120 {
14121 end = start - 1;
14122 start = 0;
14123 }
14124 else
14125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014129 }
14130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 if (error)
14132 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014133 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014134 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014135 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014136 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137 else
14138 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014139 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014140 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014141 if (list_append_number(rettv->vval.v_list,
14142 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014143 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014144 }
14145}
14146
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014147/*
14148 * "readfile()" function
14149 */
14150 static void
14151f_readfile(argvars, rettv)
14152 typval_T *argvars;
14153 typval_T *rettv;
14154{
14155 int binary = FALSE;
14156 char_u *fname;
14157 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014158 listitem_T *li;
14159#define FREAD_SIZE 200 /* optimized for text lines */
14160 char_u buf[FREAD_SIZE];
14161 int readlen; /* size of last fread() */
14162 int buflen; /* nr of valid chars in buf[] */
14163 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14164 int tolist; /* first byte in buf[] still to be put in list */
14165 int chop; /* how many CR to chop off */
14166 char_u *prev = NULL; /* previously read bytes, if any */
14167 int prevlen = 0; /* length of "prev" if not NULL */
14168 char_u *s;
14169 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014170 long maxline = MAXLNUM;
14171 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014173 if (argvars[1].v_type != VAR_UNKNOWN)
14174 {
14175 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14176 binary = TRUE;
14177 if (argvars[2].v_type != VAR_UNKNOWN)
14178 maxline = get_tv_number(&argvars[2]);
14179 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014180
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014181 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014182 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014183
14184 /* Always open the file in binary mode, library functions have a mind of
14185 * their own about CR-LF conversion. */
14186 fname = get_tv_string(&argvars[0]);
14187 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14188 {
14189 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14190 return;
14191 }
14192
14193 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014194 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014195 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014196 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014197 buflen = filtd + readlen;
14198 tolist = 0;
14199 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14200 {
14201 if (buf[filtd] == '\n' || readlen <= 0)
14202 {
14203 /* Only when in binary mode add an empty list item when the
14204 * last line ends in a '\n'. */
14205 if (!binary && readlen == 0 && filtd == 0)
14206 break;
14207
14208 /* Found end-of-line or end-of-file: add a text line to the
14209 * list. */
14210 chop = 0;
14211 if (!binary)
14212 while (filtd - chop - 1 >= tolist
14213 && buf[filtd - chop - 1] == '\r')
14214 ++chop;
14215 len = filtd - tolist - chop;
14216 if (prev == NULL)
14217 s = vim_strnsave(buf + tolist, len);
14218 else
14219 {
14220 s = alloc((unsigned)(prevlen + len + 1));
14221 if (s != NULL)
14222 {
14223 mch_memmove(s, prev, prevlen);
14224 vim_free(prev);
14225 prev = NULL;
14226 mch_memmove(s + prevlen, buf + tolist, len);
14227 s[prevlen + len] = NUL;
14228 }
14229 }
14230 tolist = filtd + 1;
14231
14232 li = listitem_alloc();
14233 if (li == NULL)
14234 {
14235 vim_free(s);
14236 break;
14237 }
14238 li->li_tv.v_type = VAR_STRING;
14239 li->li_tv.v_lock = 0;
14240 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014241 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014242
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014243 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014244 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014245 if (readlen <= 0)
14246 break;
14247 }
14248 else if (buf[filtd] == NUL)
14249 buf[filtd] = '\n';
14250 }
14251 if (readlen <= 0)
14252 break;
14253
14254 if (tolist == 0)
14255 {
14256 /* "buf" is full, need to move text to an allocated buffer */
14257 if (prev == NULL)
14258 {
14259 prev = vim_strnsave(buf, buflen);
14260 prevlen = buflen;
14261 }
14262 else
14263 {
14264 s = alloc((unsigned)(prevlen + buflen));
14265 if (s != NULL)
14266 {
14267 mch_memmove(s, prev, prevlen);
14268 mch_memmove(s + prevlen, buf, buflen);
14269 vim_free(prev);
14270 prev = s;
14271 prevlen += buflen;
14272 }
14273 }
14274 filtd = 0;
14275 }
14276 else
14277 {
14278 mch_memmove(buf, buf + tolist, buflen - tolist);
14279 filtd -= tolist;
14280 }
14281 }
14282
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014283 /*
14284 * For a negative line count use only the lines at the end of the file,
14285 * free the rest.
14286 */
14287 if (maxline < 0)
14288 while (cnt > -maxline)
14289 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014290 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014291 --cnt;
14292 }
14293
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014294 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014295 fclose(fd);
14296}
14297
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014298#if defined(FEAT_RELTIME)
14299static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14300
14301/*
14302 * Convert a List to proftime_T.
14303 * Return FAIL when there is something wrong.
14304 */
14305 static int
14306list2proftime(arg, tm)
14307 typval_T *arg;
14308 proftime_T *tm;
14309{
14310 long n1, n2;
14311 int error = FALSE;
14312
14313 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14314 || arg->vval.v_list->lv_len != 2)
14315 return FAIL;
14316 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14317 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14318# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014319 tm->HighPart = n1;
14320 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014321# else
14322 tm->tv_sec = n1;
14323 tm->tv_usec = n2;
14324# endif
14325 return error ? FAIL : OK;
14326}
14327#endif /* FEAT_RELTIME */
14328
14329/*
14330 * "reltime()" function
14331 */
14332 static void
14333f_reltime(argvars, rettv)
14334 typval_T *argvars;
14335 typval_T *rettv;
14336{
14337#ifdef FEAT_RELTIME
14338 proftime_T res;
14339 proftime_T start;
14340
14341 if (argvars[0].v_type == VAR_UNKNOWN)
14342 {
14343 /* No arguments: get current time. */
14344 profile_start(&res);
14345 }
14346 else if (argvars[1].v_type == VAR_UNKNOWN)
14347 {
14348 if (list2proftime(&argvars[0], &res) == FAIL)
14349 return;
14350 profile_end(&res);
14351 }
14352 else
14353 {
14354 /* Two arguments: compute the difference. */
14355 if (list2proftime(&argvars[0], &start) == FAIL
14356 || list2proftime(&argvars[1], &res) == FAIL)
14357 return;
14358 profile_sub(&res, &start);
14359 }
14360
14361 if (rettv_list_alloc(rettv) == OK)
14362 {
14363 long n1, n2;
14364
14365# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014366 n1 = res.HighPart;
14367 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014368# else
14369 n1 = res.tv_sec;
14370 n2 = res.tv_usec;
14371# endif
14372 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14373 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14374 }
14375#endif
14376}
14377
14378/*
14379 * "reltimestr()" function
14380 */
14381 static void
14382f_reltimestr(argvars, rettv)
14383 typval_T *argvars;
14384 typval_T *rettv;
14385{
14386#ifdef FEAT_RELTIME
14387 proftime_T tm;
14388#endif
14389
14390 rettv->v_type = VAR_STRING;
14391 rettv->vval.v_string = NULL;
14392#ifdef FEAT_RELTIME
14393 if (list2proftime(&argvars[0], &tm) == OK)
14394 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14395#endif
14396}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397
Bram Moolenaar0d660222005-01-07 21:51:51 +000014398#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14399static void make_connection __ARGS((void));
14400static int check_connection __ARGS((void));
14401
14402 static void
14403make_connection()
14404{
14405 if (X_DISPLAY == NULL
14406# ifdef FEAT_GUI
14407 && !gui.in_use
14408# endif
14409 )
14410 {
14411 x_force_connect = TRUE;
14412 setup_term_clip();
14413 x_force_connect = FALSE;
14414 }
14415}
14416
14417 static int
14418check_connection()
14419{
14420 make_connection();
14421 if (X_DISPLAY == NULL)
14422 {
14423 EMSG(_("E240: No connection to Vim server"));
14424 return FAIL;
14425 }
14426 return OK;
14427}
14428#endif
14429
14430#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014431static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432
14433 static void
14434remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 typval_T *argvars;
14436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437 int expr;
14438{
14439 char_u *server_name;
14440 char_u *keys;
14441 char_u *r = NULL;
14442 char_u buf[NUMBUFLEN];
14443# ifdef WIN32
14444 HWND w;
14445# else
14446 Window w;
14447# endif
14448
14449 if (check_restricted() || check_secure())
14450 return;
14451
14452# ifdef FEAT_X11
14453 if (check_connection() == FAIL)
14454 return;
14455# endif
14456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 server_name = get_tv_string_chk(&argvars[0]);
14458 if (server_name == NULL)
14459 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014460 keys = get_tv_string_buf(&argvars[1], buf);
14461# ifdef WIN32
14462 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14463# else
14464 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14465 < 0)
14466# endif
14467 {
14468 if (r != NULL)
14469 EMSG(r); /* sending worked but evaluation failed */
14470 else
14471 EMSG2(_("E241: Unable to send to %s"), server_name);
14472 return;
14473 }
14474
14475 rettv->vval.v_string = r;
14476
14477 if (argvars[2].v_type != VAR_UNKNOWN)
14478 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014479 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014480 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014483 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 v.di_tv.v_type = VAR_STRING;
14485 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014486 idvar = get_tv_string_chk(&argvars[2]);
14487 if (idvar != NULL)
14488 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014489 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490 }
14491}
14492#endif
14493
14494/*
14495 * "remote_expr()" function
14496 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497 static void
14498f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014499 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501{
14502 rettv->v_type = VAR_STRING;
14503 rettv->vval.v_string = NULL;
14504#ifdef FEAT_CLIENTSERVER
14505 remote_common(argvars, rettv, TRUE);
14506#endif
14507}
14508
14509/*
14510 * "remote_foreground()" function
14511 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014512 static void
14513f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014514 typval_T *argvars UNUSED;
14515 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014517#ifdef FEAT_CLIENTSERVER
14518# ifdef WIN32
14519 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 {
14521 char_u *server_name = get_tv_string_chk(&argvars[0]);
14522
14523 if (server_name != NULL)
14524 serverForeground(server_name);
14525 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526# else
14527 /* Send a foreground() expression to the server. */
14528 argvars[1].v_type = VAR_STRING;
14529 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14530 argvars[2].v_type = VAR_UNKNOWN;
14531 remote_common(argvars, rettv, TRUE);
14532 vim_free(argvars[1].vval.v_string);
14533# endif
14534#endif
14535}
14536
Bram Moolenaar0d660222005-01-07 21:51:51 +000014537 static void
14538f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541{
14542#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 char_u *s = NULL;
14545# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014546 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014547# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549
14550 if (check_restricted() || check_secure())
14551 {
14552 rettv->vval.v_number = -1;
14553 return;
14554 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 serverid = get_tv_string_chk(&argvars[0]);
14556 if (serverid == NULL)
14557 {
14558 rettv->vval.v_number = -1;
14559 return; /* type error; errmsg already given */
14560 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014562 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 if (n == 0)
14564 rettv->vval.v_number = -1;
14565 else
14566 {
14567 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14568 rettv->vval.v_number = (s != NULL);
14569 }
14570# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 if (check_connection() == FAIL)
14572 return;
14573
14574 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576# endif
14577
14578 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 char_u *retvar;
14581
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 v.di_tv.v_type = VAR_STRING;
14583 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 retvar = get_tv_string_chk(&argvars[1]);
14585 if (retvar != NULL)
14586 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014587 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 }
14589#else
14590 rettv->vval.v_number = -1;
14591#endif
14592}
14593
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594 static void
14595f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014596 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014597 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598{
14599 char_u *r = NULL;
14600
14601#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014602 char_u *serverid = get_tv_string_chk(&argvars[0]);
14603
14604 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605 {
14606# ifdef WIN32
14607 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014608 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014610 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611 if (n != 0)
14612 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14613 if (r == NULL)
14614# else
14615 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617# endif
14618 EMSG(_("E277: Unable to read a server reply"));
14619 }
14620#endif
14621 rettv->v_type = VAR_STRING;
14622 rettv->vval.v_string = r;
14623}
14624
14625/*
14626 * "remote_send()" function
14627 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 static void
14629f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632{
14633 rettv->v_type = VAR_STRING;
14634 rettv->vval.v_string = NULL;
14635#ifdef FEAT_CLIENTSERVER
14636 remote_common(argvars, rettv, FALSE);
14637#endif
14638}
14639
14640/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014642 */
14643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014644f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014645 typval_T *argvars;
14646 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014647{
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 list_T *l;
14649 listitem_T *item, *item2;
14650 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014652 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014653 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014654 dict_T *d;
14655 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014656
Bram Moolenaar8c711452005-01-14 21:53:12 +000014657 if (argvars[0].v_type == VAR_DICT)
14658 {
14659 if (argvars[2].v_type != VAR_UNKNOWN)
14660 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014661 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014662 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 key = get_tv_string_chk(&argvars[1]);
14665 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 di = dict_find(d, key, -1);
14668 if (di == NULL)
14669 EMSG2(_(e_dictkey), key);
14670 else
14671 {
14672 *rettv = di->di_tv;
14673 init_tv(&di->di_tv);
14674 dictitem_remove(d, di);
14675 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014677 }
14678 }
14679 else if (argvars[0].v_type != VAR_LIST)
14680 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014681 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014682 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 int error = FALSE;
14685
14686 idx = get_tv_number_chk(&argvars[1], &error);
14687 if (error)
14688 ; /* type error: do nothing, errmsg already given */
14689 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014690 EMSGN(_(e_listidx), idx);
14691 else
14692 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014693 if (argvars[2].v_type == VAR_UNKNOWN)
14694 {
14695 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014696 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014697 *rettv = item->li_tv;
14698 vim_free(item);
14699 }
14700 else
14701 {
14702 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014703 end = get_tv_number_chk(&argvars[2], &error);
14704 if (error)
14705 ; /* type error: do nothing */
14706 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014707 EMSGN(_(e_listidx), end);
14708 else
14709 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014710 int cnt = 0;
14711
14712 for (li = item; li != NULL; li = li->li_next)
14713 {
14714 ++cnt;
14715 if (li == item2)
14716 break;
14717 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014718 if (li == NULL) /* didn't find "item2" after "item" */
14719 EMSG(_(e_invrange));
14720 else
14721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014722 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014723 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014724 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014725 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014726 l->lv_first = item;
14727 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014728 item->li_prev = NULL;
14729 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014730 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014731 }
14732 }
14733 }
14734 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014735 }
14736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737}
14738
14739/*
14740 * "rename({from}, {to})" function
14741 */
14742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014744 typval_T *argvars;
14745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746{
14747 char_u buf[NUMBUFLEN];
14748
14749 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14753 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754}
14755
14756/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014757 * "repeat()" function
14758 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014759 static void
14760f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014761 typval_T *argvars;
14762 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014763{
14764 char_u *p;
14765 int n;
14766 int slen;
14767 int len;
14768 char_u *r;
14769 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014770
14771 n = get_tv_number(&argvars[1]);
14772 if (argvars[0].v_type == VAR_LIST)
14773 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014774 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014775 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014776 if (list_extend(rettv->vval.v_list,
14777 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014778 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014779 }
14780 else
14781 {
14782 p = get_tv_string(&argvars[0]);
14783 rettv->v_type = VAR_STRING;
14784 rettv->vval.v_string = NULL;
14785
14786 slen = (int)STRLEN(p);
14787 len = slen * n;
14788 if (len <= 0)
14789 return;
14790
14791 r = alloc(len + 1);
14792 if (r != NULL)
14793 {
14794 for (i = 0; i < n; i++)
14795 mch_memmove(r + i * slen, p, (size_t)slen);
14796 r[len] = NUL;
14797 }
14798
14799 rettv->vval.v_string = r;
14800 }
14801}
14802
14803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804 * "resolve()" function
14805 */
14806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014808 typval_T *argvars;
14809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810{
14811 char_u *p;
14812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014813 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814#ifdef FEAT_SHORTCUT
14815 {
14816 char_u *v = NULL;
14817
14818 v = mch_resolve_shortcut(p);
14819 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014820 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 }
14824#else
14825# ifdef HAVE_READLINK
14826 {
14827 char_u buf[MAXPATHL + 1];
14828 char_u *cpy;
14829 int len;
14830 char_u *remain = NULL;
14831 char_u *q;
14832 int is_relative_to_current = FALSE;
14833 int has_trailing_pathsep = FALSE;
14834 int limit = 100;
14835
14836 p = vim_strsave(p);
14837
14838 if (p[0] == '.' && (vim_ispathsep(p[1])
14839 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14840 is_relative_to_current = TRUE;
14841
14842 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014843 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 has_trailing_pathsep = TRUE;
14845
14846 q = getnextcomp(p);
14847 if (*q != NUL)
14848 {
14849 /* Separate the first path component in "p", and keep the
14850 * remainder (beginning with the path separator). */
14851 remain = vim_strsave(q - 1);
14852 q[-1] = NUL;
14853 }
14854
14855 for (;;)
14856 {
14857 for (;;)
14858 {
14859 len = readlink((char *)p, (char *)buf, MAXPATHL);
14860 if (len <= 0)
14861 break;
14862 buf[len] = NUL;
14863
14864 if (limit-- == 0)
14865 {
14866 vim_free(p);
14867 vim_free(remain);
14868 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 goto fail;
14871 }
14872
14873 /* Ensure that the result will have a trailing path separator
14874 * if the argument has one. */
14875 if (remain == NULL && has_trailing_pathsep)
14876 add_pathsep(buf);
14877
14878 /* Separate the first path component in the link value and
14879 * concatenate the remainders. */
14880 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14881 if (*q != NUL)
14882 {
14883 if (remain == NULL)
14884 remain = vim_strsave(q - 1);
14885 else
14886 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014887 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 if (cpy != NULL)
14889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 vim_free(remain);
14891 remain = cpy;
14892 }
14893 }
14894 q[-1] = NUL;
14895 }
14896
14897 q = gettail(p);
14898 if (q > p && *q == NUL)
14899 {
14900 /* Ignore trailing path separator. */
14901 q[-1] = NUL;
14902 q = gettail(p);
14903 }
14904 if (q > p && !mch_isFullName(buf))
14905 {
14906 /* symlink is relative to directory of argument */
14907 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14908 if (cpy != NULL)
14909 {
14910 STRCPY(cpy, p);
14911 STRCPY(gettail(cpy), buf);
14912 vim_free(p);
14913 p = cpy;
14914 }
14915 }
14916 else
14917 {
14918 vim_free(p);
14919 p = vim_strsave(buf);
14920 }
14921 }
14922
14923 if (remain == NULL)
14924 break;
14925
14926 /* Append the first path component of "remain" to "p". */
14927 q = getnextcomp(remain + 1);
14928 len = q - remain - (*q != NUL);
14929 cpy = vim_strnsave(p, STRLEN(p) + len);
14930 if (cpy != NULL)
14931 {
14932 STRNCAT(cpy, remain, len);
14933 vim_free(p);
14934 p = cpy;
14935 }
14936 /* Shorten "remain". */
14937 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014938 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 else
14940 {
14941 vim_free(remain);
14942 remain = NULL;
14943 }
14944 }
14945
14946 /* If the result is a relative path name, make it explicitly relative to
14947 * the current directory if and only if the argument had this form. */
14948 if (!vim_ispathsep(*p))
14949 {
14950 if (is_relative_to_current
14951 && *p != NUL
14952 && !(p[0] == '.'
14953 && (p[1] == NUL
14954 || vim_ispathsep(p[1])
14955 || (p[1] == '.'
14956 && (p[2] == NUL
14957 || vim_ispathsep(p[2]))))))
14958 {
14959 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014960 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 if (cpy != NULL)
14962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 vim_free(p);
14964 p = cpy;
14965 }
14966 }
14967 else if (!is_relative_to_current)
14968 {
14969 /* Strip leading "./". */
14970 q = p;
14971 while (q[0] == '.' && vim_ispathsep(q[1]))
14972 q += 2;
14973 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014974 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 }
14976 }
14977
14978 /* Ensure that the result will have no trailing path separator
14979 * if the argument had none. But keep "/" or "//". */
14980 if (!has_trailing_pathsep)
14981 {
14982 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014983 if (after_pathsep(p, q))
14984 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 }
14986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 }
14989# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991# endif
14992#endif
14993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
14996#ifdef HAVE_READLINK
14997fail:
14998#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000}
15001
15002/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 */
15005 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015007 typval_T *argvars;
15008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009{
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 list_T *l;
15011 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013 if (argvars[0].v_type != VAR_LIST)
15014 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015015 else if ((l = argvars[0].vval.v_list) != NULL
15016 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017 {
15018 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015019 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015020 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021 while (li != NULL)
15022 {
15023 ni = li->li_prev;
15024 list_append(l, li);
15025 li = ni;
15026 }
15027 rettv->vval.v_list = l;
15028 rettv->v_type = VAR_LIST;
15029 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015030 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032}
15033
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015034#define SP_NOMOVE 0x01 /* don't move cursor */
15035#define SP_REPEAT 0x02 /* repeat to find outer pair */
15036#define SP_RETCOUNT 0x04 /* return matchcount */
15037#define SP_SETPCMARK 0x08 /* set previous context mark */
15038#define SP_START 0x10 /* accept match at start position */
15039#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15040#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015041
Bram Moolenaar33570922005-01-25 22:26:29 +000015042static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043
15044/*
15045 * Get flags for a search function.
15046 * Possibly sets "p_ws".
15047 * Returns BACKWARD, FORWARD or zero (for an error).
15048 */
15049 static int
15050get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015052 int *flagsp;
15053{
15054 int dir = FORWARD;
15055 char_u *flags;
15056 char_u nbuf[NUMBUFLEN];
15057 int mask;
15058
15059 if (varp->v_type != VAR_UNKNOWN)
15060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 flags = get_tv_string_buf_chk(varp, nbuf);
15062 if (flags == NULL)
15063 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015064 while (*flags != NUL)
15065 {
15066 switch (*flags)
15067 {
15068 case 'b': dir = BACKWARD; break;
15069 case 'w': p_ws = TRUE; break;
15070 case 'W': p_ws = FALSE; break;
15071 default: mask = 0;
15072 if (flagsp != NULL)
15073 switch (*flags)
15074 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015075 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015076 case 'e': mask = SP_END; break;
15077 case 'm': mask = SP_RETCOUNT; break;
15078 case 'n': mask = SP_NOMOVE; break;
15079 case 'p': mask = SP_SUBPAT; break;
15080 case 'r': mask = SP_REPEAT; break;
15081 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 }
15083 if (mask == 0)
15084 {
15085 EMSG2(_(e_invarg2), flags);
15086 dir = 0;
15087 }
15088 else
15089 *flagsp |= mask;
15090 }
15091 if (dir == 0)
15092 break;
15093 ++flags;
15094 }
15095 }
15096 return dir;
15097}
15098
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015100 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015102 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015103search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015105 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015106 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015108 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 char_u *pat;
15110 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015111 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 int save_p_ws = p_ws;
15113 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015114 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015115 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015116 proftime_T tm;
15117#ifdef FEAT_RELTIME
15118 long time_limit = 0;
15119#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015120 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015121 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015123 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015124 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015125 if (dir == 0)
15126 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015127 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015128 if (flags & SP_START)
15129 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015130 if (flags & SP_END)
15131 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015132
Bram Moolenaar76929292008-01-06 19:07:36 +000015133 /* Optional arguments: line number to stop searching and timeout. */
15134 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015135 {
15136 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15137 if (lnum_stop < 0)
15138 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015139#ifdef FEAT_RELTIME
15140 if (argvars[3].v_type != VAR_UNKNOWN)
15141 {
15142 time_limit = get_tv_number_chk(&argvars[3], NULL);
15143 if (time_limit < 0)
15144 goto theend;
15145 }
15146#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015147 }
15148
Bram Moolenaar76929292008-01-06 19:07:36 +000015149#ifdef FEAT_RELTIME
15150 /* Set the time limit, if there is one. */
15151 profile_setlimit(time_limit, &tm);
15152#endif
15153
Bram Moolenaar231334e2005-07-25 20:46:57 +000015154 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015155 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015156 * Check to make sure only those flags are set.
15157 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15158 * flags cannot be set. Check for that condition also.
15159 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015160 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015161 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015164 goto theend;
15165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015167 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015168 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015169 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015170 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015172 if (flags & SP_SUBPAT)
15173 retval = subpatnum;
15174 else
15175 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015176 if (flags & SP_SETPCMARK)
15177 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015179 if (match_pos != NULL)
15180 {
15181 /* Store the match cursor position */
15182 match_pos->lnum = pos.lnum;
15183 match_pos->col = pos.col + 1;
15184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 /* "/$" will put the cursor after the end of the line, may need to
15186 * correct that here */
15187 check_cursor();
15188 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015189
15190 /* If 'n' flag is used: restore cursor position. */
15191 if (flags & SP_NOMOVE)
15192 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015193 else
15194 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015195theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015197
15198 return retval;
15199}
15200
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015201#ifdef FEAT_FLOAT
15202/*
15203 * "round({float})" function
15204 */
15205 static void
15206f_round(argvars, rettv)
15207 typval_T *argvars;
15208 typval_T *rettv;
15209{
15210 float_T f;
15211
15212 rettv->v_type = VAR_FLOAT;
15213 if (get_float_arg(argvars, &f) == OK)
15214 /* round() is not in C90, use ceil() or floor() instead. */
15215 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15216 else
15217 rettv->vval.v_float = 0.0;
15218}
15219#endif
15220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015221/*
15222 * "search()" function
15223 */
15224 static void
15225f_search(argvars, rettv)
15226 typval_T *argvars;
15227 typval_T *rettv;
15228{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015229 int flags = 0;
15230
15231 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232}
15233
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015235 * "searchdecl()" function
15236 */
15237 static void
15238f_searchdecl(argvars, rettv)
15239 typval_T *argvars;
15240 typval_T *rettv;
15241{
15242 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015243 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015244 int error = FALSE;
15245 char_u *name;
15246
15247 rettv->vval.v_number = 1; /* default: FAIL */
15248
15249 name = get_tv_string_chk(&argvars[0]);
15250 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015251 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015252 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015253 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15254 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15255 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015256 if (!error && name != NULL)
15257 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015258 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015259}
15260
15261/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015262 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015264 static int
15265searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015266 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015267 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268{
15269 char_u *spat, *mpat, *epat;
15270 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 int dir;
15273 int flags = 0;
15274 char_u nbuf1[NUMBUFLEN];
15275 char_u nbuf2[NUMBUFLEN];
15276 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015277 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015278 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015279 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 spat = get_tv_string_chk(&argvars[0]);
15283 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15284 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15285 if (spat == NULL || mpat == NULL || epat == NULL)
15286 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 /* Handle the optional fourth argument: flags */
15289 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015290 if (dir == 0)
15291 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015292
15293 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015294 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15295 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015296 if ((flags & (SP_END | SP_SUBPAT)) != 0
15297 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015298 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015299 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015300 goto theend;
15301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015303 /* Using 'r' implies 'W', otherwise it doesn't work. */
15304 if (flags & SP_REPEAT)
15305 p_ws = FALSE;
15306
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015307 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015308 if (argvars[3].v_type == VAR_UNKNOWN
15309 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 skip = (char_u *)"";
15311 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015314 if (argvars[5].v_type != VAR_UNKNOWN)
15315 {
15316 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15317 if (lnum_stop < 0)
15318 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015319#ifdef FEAT_RELTIME
15320 if (argvars[6].v_type != VAR_UNKNOWN)
15321 {
15322 time_limit = get_tv_number_chk(&argvars[6], NULL);
15323 if (time_limit < 0)
15324 goto theend;
15325 }
15326#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015327 }
15328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015329 if (skip == NULL)
15330 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015333 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015334
15335theend:
15336 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015337
15338 return retval;
15339}
15340
15341/*
15342 * "searchpair()" function
15343 */
15344 static void
15345f_searchpair(argvars, rettv)
15346 typval_T *argvars;
15347 typval_T *rettv;
15348{
15349 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15350}
15351
15352/*
15353 * "searchpairpos()" function
15354 */
15355 static void
15356f_searchpairpos(argvars, rettv)
15357 typval_T *argvars;
15358 typval_T *rettv;
15359{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015360 pos_T match_pos;
15361 int lnum = 0;
15362 int col = 0;
15363
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015364 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015365 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015366
15367 if (searchpair_cmn(argvars, &match_pos) > 0)
15368 {
15369 lnum = match_pos.lnum;
15370 col = match_pos.col;
15371 }
15372
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015373 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15374 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015375}
15376
15377/*
15378 * Search for a start/middle/end thing.
15379 * Used by searchpair(), see its documentation for the details.
15380 * Returns 0 or -1 for no match,
15381 */
15382 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015383do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15384 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015385 char_u *spat; /* start pattern */
15386 char_u *mpat; /* middle pattern */
15387 char_u *epat; /* end pattern */
15388 int dir; /* BACKWARD or FORWARD */
15389 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015390 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015391 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015392 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015393 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015394{
15395 char_u *save_cpo;
15396 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15397 long retval = 0;
15398 pos_T pos;
15399 pos_T firstpos;
15400 pos_T foundpos;
15401 pos_T save_cursor;
15402 pos_T save_pos;
15403 int n;
15404 int r;
15405 int nest = 1;
15406 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015407 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015408 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015409
15410 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15411 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015412 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015413
Bram Moolenaar76929292008-01-06 19:07:36 +000015414#ifdef FEAT_RELTIME
15415 /* Set the time limit, if there is one. */
15416 profile_setlimit(time_limit, &tm);
15417#endif
15418
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015419 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15420 * start/middle/end (pat3, for the top pair). */
15421 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15422 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15423 if (pat2 == NULL || pat3 == NULL)
15424 goto theend;
15425 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15426 if (*mpat == NUL)
15427 STRCPY(pat3, pat2);
15428 else
15429 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15430 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015431 if (flags & SP_START)
15432 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015433
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 save_cursor = curwin->w_cursor;
15435 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015436 clearpos(&firstpos);
15437 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 pat = pat3;
15439 for (;;)
15440 {
15441 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015442 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15444 /* didn't find it or found the first match again: FAIL */
15445 break;
15446
15447 if (firstpos.lnum == 0)
15448 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015449 if (equalpos(pos, foundpos))
15450 {
15451 /* Found the same position again. Can happen with a pattern that
15452 * has "\zs" at the end and searching backwards. Advance one
15453 * character and try again. */
15454 if (dir == BACKWARD)
15455 decl(&pos);
15456 else
15457 incl(&pos);
15458 }
15459 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015461 /* clear the start flag to avoid getting stuck here */
15462 options &= ~SEARCH_START;
15463
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 /* If the skip pattern matches, ignore this match. */
15465 if (*skip != NUL)
15466 {
15467 save_pos = curwin->w_cursor;
15468 curwin->w_cursor = pos;
15469 r = eval_to_bool(skip, &err, NULL, FALSE);
15470 curwin->w_cursor = save_pos;
15471 if (err)
15472 {
15473 /* Evaluating {skip} caused an error, break here. */
15474 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015475 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 break;
15477 }
15478 if (r)
15479 continue;
15480 }
15481
15482 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15483 {
15484 /* Found end when searching backwards or start when searching
15485 * forward: nested pair. */
15486 ++nest;
15487 pat = pat2; /* nested, don't search for middle */
15488 }
15489 else
15490 {
15491 /* Found end when searching forward or start when searching
15492 * backward: end of (nested) pair; or found middle in outer pair. */
15493 if (--nest == 1)
15494 pat = pat3; /* outer level, search for middle */
15495 }
15496
15497 if (nest == 0)
15498 {
15499 /* Found the match: return matchcount or line number. */
15500 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015501 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015503 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015504 if (flags & SP_SETPCMARK)
15505 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 curwin->w_cursor = pos;
15507 if (!(flags & SP_REPEAT))
15508 break;
15509 nest = 1; /* search for next unmatched */
15510 }
15511 }
15512
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015513 if (match_pos != NULL)
15514 {
15515 /* Store the match cursor position */
15516 match_pos->lnum = curwin->w_cursor.lnum;
15517 match_pos->col = curwin->w_cursor.col + 1;
15518 }
15519
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015521 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 curwin->w_cursor = save_cursor;
15523
15524theend:
15525 vim_free(pat2);
15526 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015527 if (p_cpo == empty_option)
15528 p_cpo = save_cpo;
15529 else
15530 /* Darn, evaluating the {skip} expression changed the value. */
15531 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015532
15533 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534}
15535
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015536/*
15537 * "searchpos()" function
15538 */
15539 static void
15540f_searchpos(argvars, rettv)
15541 typval_T *argvars;
15542 typval_T *rettv;
15543{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015544 pos_T match_pos;
15545 int lnum = 0;
15546 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015547 int n;
15548 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015549
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015550 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015551 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015552
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015553 n = search_cmn(argvars, &match_pos, &flags);
15554 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015555 {
15556 lnum = match_pos.lnum;
15557 col = match_pos.col;
15558 }
15559
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015560 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15561 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015562 if (flags & SP_SUBPAT)
15563 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015564}
15565
15566
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 static void
15568f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572#ifdef FEAT_CLIENTSERVER
15573 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015574 char_u *server = get_tv_string_chk(&argvars[0]);
15575 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576
Bram Moolenaar0d660222005-01-07 21:51:51 +000015577 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015578 if (server == NULL || reply == NULL)
15579 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580 if (check_restricted() || check_secure())
15581 return;
15582# ifdef FEAT_X11
15583 if (check_connection() == FAIL)
15584 return;
15585# endif
15586
15587 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015589 EMSG(_("E258: Unable to send to client"));
15590 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 rettv->vval.v_number = 0;
15593#else
15594 rettv->vval.v_number = -1;
15595#endif
15596}
15597
Bram Moolenaar0d660222005-01-07 21:51:51 +000015598 static void
15599f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015600 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015601 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602{
15603 char_u *r = NULL;
15604
15605#ifdef FEAT_CLIENTSERVER
15606# ifdef WIN32
15607 r = serverGetVimNames();
15608# else
15609 make_connection();
15610 if (X_DISPLAY != NULL)
15611 r = serverGetVimNames(X_DISPLAY);
15612# endif
15613#endif
15614 rettv->v_type = VAR_STRING;
15615 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616}
15617
15618/*
15619 * "setbufvar()" function
15620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015622f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015623 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015624 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625{
15626 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015629 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 char_u nbuf[NUMBUFLEN];
15631
15632 if (check_restricted() || check_secure())
15633 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15635 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 varp = &argvars[2];
15638
15639 if (buf != NULL && varname != NULL && varp != NULL)
15640 {
15641 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643
15644 if (*varname == '&')
15645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 long numval;
15647 char_u *strval;
15648 int error = FALSE;
15649
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015651 numval = get_tv_number_chk(varp, &error);
15652 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015653 if (!error && strval != NULL)
15654 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 }
15656 else
15657 {
15658 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15659 if (bufvarname != NULL)
15660 {
15661 STRCPY(bufvarname, "b:");
15662 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015663 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 vim_free(bufvarname);
15665 }
15666 }
15667
15668 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671}
15672
15673/*
15674 * "setcmdpos()" function
15675 */
15676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015677f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015678 typval_T *argvars;
15679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 int pos = (int)get_tv_number(&argvars[0]) - 1;
15682
15683 if (pos >= 0)
15684 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685}
15686
15687/*
15688 * "setline()" function
15689 */
15690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 typval_T *argvars;
15693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694{
15695 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015696 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015697 list_T *l = NULL;
15698 listitem_T *li = NULL;
15699 long added = 0;
15700 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015702 lnum = get_tv_lnum(&argvars[0]);
15703 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015705 l = argvars[1].vval.v_list;
15706 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015708 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015709 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015710
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015711 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015712 for (;;)
15713 {
15714 if (l != NULL)
15715 {
15716 /* list argument, get next string */
15717 if (li == NULL)
15718 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015719 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015720 li = li->li_next;
15721 }
15722
15723 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015725 break;
15726 if (lnum <= curbuf->b_ml.ml_line_count)
15727 {
15728 /* existing line, replace it */
15729 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15730 {
15731 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015732 if (lnum == curwin->w_cursor.lnum)
15733 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015734 rettv->vval.v_number = 0; /* OK */
15735 }
15736 }
15737 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15738 {
15739 /* lnum is one past the last line, append the line */
15740 ++added;
15741 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15742 rettv->vval.v_number = 0; /* OK */
15743 }
15744
15745 if (l == NULL) /* only one string argument */
15746 break;
15747 ++lnum;
15748 }
15749
15750 if (added > 0)
15751 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752}
15753
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015754static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15755
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015757 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015758 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015759 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015760set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015761 win_T *wp UNUSED;
15762 typval_T *list_arg UNUSED;
15763 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015764 typval_T *rettv;
15765{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015766#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015767 char_u *act;
15768 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015769#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015770
Bram Moolenaar2641f772005-03-25 21:58:17 +000015771 rettv->vval.v_number = -1;
15772
15773#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015774 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015775 EMSG(_(e_listreq));
15776 else
15777 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015778 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015779
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015780 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015781 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015782 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015783 if (act == NULL)
15784 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015785 if (*act == 'a' || *act == 'r')
15786 action = *act;
15787 }
15788
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015789 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015790 rettv->vval.v_number = 0;
15791 }
15792#endif
15793}
15794
15795/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015796 * "setloclist()" function
15797 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015798 static void
15799f_setloclist(argvars, rettv)
15800 typval_T *argvars;
15801 typval_T *rettv;
15802{
15803 win_T *win;
15804
15805 rettv->vval.v_number = -1;
15806
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015807 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015808 if (win != NULL)
15809 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15810}
15811
15812/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015813 * "setmatches()" function
15814 */
15815 static void
15816f_setmatches(argvars, rettv)
15817 typval_T *argvars;
15818 typval_T *rettv;
15819{
15820#ifdef FEAT_SEARCH_EXTRA
15821 list_T *l;
15822 listitem_T *li;
15823 dict_T *d;
15824
15825 rettv->vval.v_number = -1;
15826 if (argvars[0].v_type != VAR_LIST)
15827 {
15828 EMSG(_(e_listreq));
15829 return;
15830 }
15831 if ((l = argvars[0].vval.v_list) != NULL)
15832 {
15833
15834 /* To some extent make sure that we are dealing with a list from
15835 * "getmatches()". */
15836 li = l->lv_first;
15837 while (li != NULL)
15838 {
15839 if (li->li_tv.v_type != VAR_DICT
15840 || (d = li->li_tv.vval.v_dict) == NULL)
15841 {
15842 EMSG(_(e_invarg));
15843 return;
15844 }
15845 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15846 && dict_find(d, (char_u *)"pattern", -1) != NULL
15847 && dict_find(d, (char_u *)"priority", -1) != NULL
15848 && dict_find(d, (char_u *)"id", -1) != NULL))
15849 {
15850 EMSG(_(e_invarg));
15851 return;
15852 }
15853 li = li->li_next;
15854 }
15855
15856 clear_matches(curwin);
15857 li = l->lv_first;
15858 while (li != NULL)
15859 {
15860 d = li->li_tv.vval.v_dict;
15861 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15862 get_dict_string(d, (char_u *)"pattern", FALSE),
15863 (int)get_dict_number(d, (char_u *)"priority"),
15864 (int)get_dict_number(d, (char_u *)"id"));
15865 li = li->li_next;
15866 }
15867 rettv->vval.v_number = 0;
15868 }
15869#endif
15870}
15871
15872/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015873 * "setpos()" function
15874 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015875 static void
15876f_setpos(argvars, rettv)
15877 typval_T *argvars;
15878 typval_T *rettv;
15879{
15880 pos_T pos;
15881 int fnum;
15882 char_u *name;
15883
Bram Moolenaar08250432008-02-13 11:42:46 +000015884 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015885 name = get_tv_string_chk(argvars);
15886 if (name != NULL)
15887 {
15888 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15889 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015890 if (--pos.col < 0)
15891 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015892 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015893 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015894 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015895 if (fnum == curbuf->b_fnum)
15896 {
15897 curwin->w_cursor = pos;
15898 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015899 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015900 }
15901 else
15902 EMSG(_(e_invarg));
15903 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015904 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15905 {
15906 /* set mark */
15907 if (setmark_pos(name[1], &pos, fnum) == OK)
15908 rettv->vval.v_number = 0;
15909 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015910 else
15911 EMSG(_(e_invarg));
15912 }
15913 }
15914}
15915
15916/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015917 * "setqflist()" function
15918 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015919 static void
15920f_setqflist(argvars, rettv)
15921 typval_T *argvars;
15922 typval_T *rettv;
15923{
15924 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15925}
15926
15927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 * "setreg()" function
15929 */
15930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015931f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 typval_T *argvars;
15933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934{
15935 int regname;
15936 char_u *strregname;
15937 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 int append;
15940 char_u yank_type;
15941 long block_len;
15942
15943 block_len = -1;
15944 yank_type = MAUTO;
15945 append = FALSE;
15946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015947 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015948 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015950 if (strregname == NULL)
15951 return; /* type error; errmsg already given */
15952 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953 if (regname == 0 || regname == '@')
15954 regname = '"';
15955 else if (regname == '=')
15956 return;
15957
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015958 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015960 stropt = get_tv_string_chk(&argvars[2]);
15961 if (stropt == NULL)
15962 return; /* type error */
15963 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 switch (*stropt)
15965 {
15966 case 'a': case 'A': /* append */
15967 append = TRUE;
15968 break;
15969 case 'v': case 'c': /* character-wise selection */
15970 yank_type = MCHAR;
15971 break;
15972 case 'V': case 'l': /* line-wise selection */
15973 yank_type = MLINE;
15974 break;
15975#ifdef FEAT_VISUAL
15976 case 'b': case Ctrl_V: /* block-wise selection */
15977 yank_type = MBLOCK;
15978 if (VIM_ISDIGIT(stropt[1]))
15979 {
15980 ++stropt;
15981 block_len = getdigits(&stropt) - 1;
15982 --stropt;
15983 }
15984 break;
15985#endif
15986 }
15987 }
15988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015989 strval = get_tv_string_chk(&argvars[1]);
15990 if (strval != NULL)
15991 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015993 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994}
15995
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015996/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015997 * "settabvar()" function
15998 */
15999 static void
16000f_settabvar(argvars, rettv)
16001 typval_T *argvars;
16002 typval_T *rettv;
16003{
16004 tabpage_T *save_curtab;
16005 char_u *varname, *tabvarname;
16006 typval_T *varp;
16007 tabpage_T *tp;
16008
16009 rettv->vval.v_number = 0;
16010
16011 if (check_restricted() || check_secure())
16012 return;
16013
16014 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16015 varname = get_tv_string_chk(&argvars[1]);
16016 varp = &argvars[2];
16017
16018 if (tp != NULL && varname != NULL && varp != NULL)
16019 {
16020 save_curtab = curtab;
16021 goto_tabpage_tp(tp);
16022
16023 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16024 if (tabvarname != NULL)
16025 {
16026 STRCPY(tabvarname, "t:");
16027 STRCPY(tabvarname + 2, varname);
16028 set_var(tabvarname, varp, TRUE);
16029 vim_free(tabvarname);
16030 }
16031
16032 /* Restore current tabpage */
16033 if (valid_tabpage(save_curtab))
16034 goto_tabpage_tp(save_curtab);
16035 }
16036}
16037
16038/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016039 * "settabwinvar()" function
16040 */
16041 static void
16042f_settabwinvar(argvars, rettv)
16043 typval_T *argvars;
16044 typval_T *rettv;
16045{
16046 setwinvar(argvars, rettv, 1);
16047}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048
16049/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016050 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016053f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016054 typval_T *argvars;
16055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016057 setwinvar(argvars, rettv, 0);
16058}
16059
16060/*
16061 * "setwinvar()" and "settabwinvar()" functions
16062 */
16063 static void
16064setwinvar(argvars, rettv, off)
16065 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016066 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016067 int off;
16068{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 win_T *win;
16070#ifdef FEAT_WINDOWS
16071 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016072 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073#endif
16074 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016075 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016077 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078
16079 if (check_restricted() || check_secure())
16080 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016081
16082#ifdef FEAT_WINDOWS
16083 if (off == 1)
16084 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16085 else
16086 tp = curtab;
16087#endif
16088 win = find_win_by_nr(&argvars[off], tp);
16089 varname = get_tv_string_chk(&argvars[off + 1]);
16090 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091
16092 if (win != NULL && varname != NULL && varp != NULL)
16093 {
16094#ifdef FEAT_WINDOWS
16095 /* set curwin to be our win, temporarily */
16096 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016097 save_curtab = curtab;
16098 goto_tabpage_tp(tp);
16099 if (!win_valid(win))
16100 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 curwin = win;
16102 curbuf = curwin->w_buffer;
16103#endif
16104
16105 if (*varname == '&')
16106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016107 long numval;
16108 char_u *strval;
16109 int error = FALSE;
16110
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016112 numval = get_tv_number_chk(varp, &error);
16113 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 if (!error && strval != NULL)
16115 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 }
16117 else
16118 {
16119 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16120 if (winvarname != NULL)
16121 {
16122 STRCPY(winvarname, "w:");
16123 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016124 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 vim_free(winvarname);
16126 }
16127 }
16128
16129#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016130 /* Restore current tabpage and window, if still valid (autocomands can
16131 * make them invalid). */
16132 if (valid_tabpage(save_curtab))
16133 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134 if (win_valid(save_curwin))
16135 {
16136 curwin = save_curwin;
16137 curbuf = curwin->w_buffer;
16138 }
16139#endif
16140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141}
16142
16143/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016144 * "shellescape({string})" function
16145 */
16146 static void
16147f_shellescape(argvars, rettv)
16148 typval_T *argvars;
16149 typval_T *rettv;
16150{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016151 rettv->vval.v_string = vim_strsave_shellescape(
16152 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016153 rettv->v_type = VAR_STRING;
16154}
16155
16156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016157 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 */
16159 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016161 typval_T *argvars;
16162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166 p = get_tv_string(&argvars[0]);
16167 rettv->vval.v_string = vim_strsave(p);
16168 simplify_filename(rettv->vval.v_string); /* simplify in place */
16169 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170}
16171
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172#ifdef FEAT_FLOAT
16173/*
16174 * "sin()" function
16175 */
16176 static void
16177f_sin(argvars, rettv)
16178 typval_T *argvars;
16179 typval_T *rettv;
16180{
16181 float_T f;
16182
16183 rettv->v_type = VAR_FLOAT;
16184 if (get_float_arg(argvars, &f) == OK)
16185 rettv->vval.v_float = sin(f);
16186 else
16187 rettv->vval.v_float = 0.0;
16188}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016189
16190/*
16191 * "sinh()" function
16192 */
16193 static void
16194f_sinh(argvars, rettv)
16195 typval_T *argvars;
16196 typval_T *rettv;
16197{
16198 float_T f;
16199
16200 rettv->v_type = VAR_FLOAT;
16201 if (get_float_arg(argvars, &f) == OK)
16202 rettv->vval.v_float = sinh(f);
16203 else
16204 rettv->vval.v_float = 0.0;
16205}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206#endif
16207
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208static int
16209#ifdef __BORLANDC__
16210 _RTLENTRYF
16211#endif
16212 item_compare __ARGS((const void *s1, const void *s2));
16213static int
16214#ifdef __BORLANDC__
16215 _RTLENTRYF
16216#endif
16217 item_compare2 __ARGS((const void *s1, const void *s2));
16218
16219static int item_compare_ic;
16220static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016221static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222#define ITEM_COMPARE_FAIL 999
16223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 static int
16228#ifdef __BORLANDC__
16229_RTLENTRYF
16230#endif
16231item_compare(s1, s2)
16232 const void *s1;
16233 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235 char_u *p1, *p2;
16236 char_u *tofree1, *tofree2;
16237 int res;
16238 char_u numbuf1[NUMBUFLEN];
16239 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016241 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16242 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016243 if (p1 == NULL)
16244 p1 = (char_u *)"";
16245 if (p2 == NULL)
16246 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 if (item_compare_ic)
16248 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250 res = STRCMP(p1, p2);
16251 vim_free(tofree1);
16252 vim_free(tofree2);
16253 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
16256 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257#ifdef __BORLANDC__
16258_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260item_compare2(s1, s2)
16261 const void *s1;
16262 const void *s2;
16263{
16264 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016265 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016266 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 /* shortcut after failure in previous call; compare all items equal */
16270 if (item_compare_func_err)
16271 return 0;
16272
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016273 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16274 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16276 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277
16278 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016279 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016280 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016281 clear_tv(&argv[0]);
16282 clear_tv(&argv[1]);
16283
16284 if (res == FAIL)
16285 res = ITEM_COMPARE_FAIL;
16286 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016287 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16288 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016289 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 clear_tv(&rettv);
16291 return res;
16292}
16293
16294/*
16295 * "sort({list})" function
16296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 typval_T *argvars;
16300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301{
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 list_T *l;
16303 listitem_T *li;
16304 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016305 long len;
16306 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 if (argvars[0].v_type != VAR_LIST)
16309 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 else
16311 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016313 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 return;
16315 rettv->vval.v_list = l;
16316 rettv->v_type = VAR_LIST;
16317 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319 len = list_len(l);
16320 if (len <= 1)
16321 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 item_compare_ic = FALSE;
16324 item_compare_func = NULL;
16325 if (argvars[1].v_type != VAR_UNKNOWN)
16326 {
16327 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329 else
16330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016331 int error = FALSE;
16332
16333 i = get_tv_number_chk(&argvars[1], &error);
16334 if (error)
16335 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336 if (i == 1)
16337 item_compare_ic = TRUE;
16338 else
16339 item_compare_func = get_tv_string(&argvars[1]);
16340 }
16341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345 if (ptrs == NULL)
16346 return;
16347 i = 0;
16348 for (li = l->lv_first; li != NULL; li = li->li_next)
16349 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 /* test the compare function */
16353 if (item_compare_func != NULL
16354 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16355 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016356 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358 {
16359 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361 item_compare_func == NULL ? item_compare : item_compare2);
16362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016363 if (!item_compare_func_err)
16364 {
16365 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016366 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016367 l->lv_len = 0;
16368 for (i = 0; i < len; ++i)
16369 list_append(l, ptrs[i]);
16370 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371 }
16372
16373 vim_free(ptrs);
16374 }
16375}
16376
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016377/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016378 * "soundfold({word})" function
16379 */
16380 static void
16381f_soundfold(argvars, rettv)
16382 typval_T *argvars;
16383 typval_T *rettv;
16384{
16385 char_u *s;
16386
16387 rettv->v_type = VAR_STRING;
16388 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016389#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016390 rettv->vval.v_string = eval_soundfold(s);
16391#else
16392 rettv->vval.v_string = vim_strsave(s);
16393#endif
16394}
16395
16396/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016397 * "spellbadword()" function
16398 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016399 static void
16400f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016401 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016402 typval_T *rettv;
16403{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016404 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016405 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016406 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016407
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016408 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016409 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016410
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016411#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016412 if (argvars[0].v_type == VAR_UNKNOWN)
16413 {
16414 /* Find the start and length of the badly spelled word. */
16415 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16416 if (len != 0)
16417 word = ml_get_cursor();
16418 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016419 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016420 {
16421 char_u *str = get_tv_string_chk(&argvars[0]);
16422 int capcol = -1;
16423
16424 if (str != NULL)
16425 {
16426 /* Check the argument for spelling. */
16427 while (*str != NUL)
16428 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016429 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016430 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016431 {
16432 word = str;
16433 break;
16434 }
16435 str += len;
16436 }
16437 }
16438 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016439#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016440
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016441 list_append_string(rettv->vval.v_list, word, len);
16442 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016443 attr == HLF_SPB ? "bad" :
16444 attr == HLF_SPR ? "rare" :
16445 attr == HLF_SPL ? "local" :
16446 attr == HLF_SPC ? "caps" :
16447 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016448}
16449
16450/*
16451 * "spellsuggest()" function
16452 */
16453 static void
16454f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016455 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016456 typval_T *rettv;
16457{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016458#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016459 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016460 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016461 int maxcount;
16462 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016463 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016464 listitem_T *li;
16465 int need_capital = FALSE;
16466#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016467
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016468 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016469 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016470
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016471#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016472 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016473 {
16474 str = get_tv_string(&argvars[0]);
16475 if (argvars[1].v_type != VAR_UNKNOWN)
16476 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016477 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016478 if (maxcount <= 0)
16479 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016480 if (argvars[2].v_type != VAR_UNKNOWN)
16481 {
16482 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16483 if (typeerr)
16484 return;
16485 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016486 }
16487 else
16488 maxcount = 25;
16489
Bram Moolenaar4770d092006-01-12 23:22:24 +000016490 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016491
16492 for (i = 0; i < ga.ga_len; ++i)
16493 {
16494 str = ((char_u **)ga.ga_data)[i];
16495
16496 li = listitem_alloc();
16497 if (li == NULL)
16498 vim_free(str);
16499 else
16500 {
16501 li->li_tv.v_type = VAR_STRING;
16502 li->li_tv.v_lock = 0;
16503 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016504 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016505 }
16506 }
16507 ga_clear(&ga);
16508 }
16509#endif
16510}
16511
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016513f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016514 typval_T *argvars;
16515 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516{
16517 char_u *str;
16518 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016519 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520 regmatch_T regmatch;
16521 char_u patbuf[NUMBUFLEN];
16522 char_u *save_cpo;
16523 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016525 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016526 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527
16528 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16529 save_cpo = p_cpo;
16530 p_cpo = (char_u *)"";
16531
16532 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016533 if (argvars[1].v_type != VAR_UNKNOWN)
16534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016535 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16536 if (pat == NULL)
16537 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016540 }
16541 if (pat == NULL || *pat == NUL)
16542 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016544 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016546 if (typeerr)
16547 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16550 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016553 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016555 if (*str == NUL)
16556 match = FALSE; /* empty item at the end */
16557 else
16558 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 if (match)
16560 end = regmatch.startp[0];
16561 else
16562 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016563 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16564 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016565 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016566 if (list_append_string(rettv->vval.v_list, str,
16567 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569 }
16570 if (!match)
16571 break;
16572 /* Advance to just after the match. */
16573 if (regmatch.endp[0] > str)
16574 col = 0;
16575 else
16576 {
16577 /* Don't get stuck at the same match. */
16578#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016579 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016580#else
16581 col = 1;
16582#endif
16583 }
16584 str = regmatch.endp[0];
16585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591}
16592
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016593#ifdef FEAT_FLOAT
16594/*
16595 * "sqrt()" function
16596 */
16597 static void
16598f_sqrt(argvars, rettv)
16599 typval_T *argvars;
16600 typval_T *rettv;
16601{
16602 float_T f;
16603
16604 rettv->v_type = VAR_FLOAT;
16605 if (get_float_arg(argvars, &f) == OK)
16606 rettv->vval.v_float = sqrt(f);
16607 else
16608 rettv->vval.v_float = 0.0;
16609}
16610
16611/*
16612 * "str2float()" function
16613 */
16614 static void
16615f_str2float(argvars, rettv)
16616 typval_T *argvars;
16617 typval_T *rettv;
16618{
16619 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16620
16621 if (*p == '+')
16622 p = skipwhite(p + 1);
16623 (void)string2float(p, &rettv->vval.v_float);
16624 rettv->v_type = VAR_FLOAT;
16625}
16626#endif
16627
Bram Moolenaar2c932302006-03-18 21:42:09 +000016628/*
16629 * "str2nr()" function
16630 */
16631 static void
16632f_str2nr(argvars, rettv)
16633 typval_T *argvars;
16634 typval_T *rettv;
16635{
16636 int base = 10;
16637 char_u *p;
16638 long n;
16639
16640 if (argvars[1].v_type != VAR_UNKNOWN)
16641 {
16642 base = get_tv_number(&argvars[1]);
16643 if (base != 8 && base != 10 && base != 16)
16644 {
16645 EMSG(_(e_invarg));
16646 return;
16647 }
16648 }
16649
16650 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016651 if (*p == '+')
16652 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016653 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16654 rettv->vval.v_number = n;
16655}
16656
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657#ifdef HAVE_STRFTIME
16658/*
16659 * "strftime({format}[, {time}])" function
16660 */
16661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016662f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016663 typval_T *argvars;
16664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665{
16666 char_u result_buf[256];
16667 struct tm *curtime;
16668 time_t seconds;
16669 char_u *p;
16670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016674 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 seconds = time(NULL);
16676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016677 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 curtime = localtime(&seconds);
16679 /* MSVC returns NULL for an invalid value of seconds. */
16680 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 else
16683 {
16684# ifdef FEAT_MBYTE
16685 vimconv_T conv;
16686 char_u *enc;
16687
16688 conv.vc_type = CONV_NONE;
16689 enc = enc_locale();
16690 convert_setup(&conv, p_enc, enc);
16691 if (conv.vc_type != CONV_NONE)
16692 p = string_convert(&conv, p, NULL);
16693# endif
16694 if (p != NULL)
16695 (void)strftime((char *)result_buf, sizeof(result_buf),
16696 (char *)p, curtime);
16697 else
16698 result_buf[0] = NUL;
16699
16700# ifdef FEAT_MBYTE
16701 if (conv.vc_type != CONV_NONE)
16702 vim_free(p);
16703 convert_setup(&conv, enc, p_enc);
16704 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016705 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 else
16707# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016708 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709
16710# ifdef FEAT_MBYTE
16711 /* Release conversion descriptors */
16712 convert_setup(&conv, NULL, NULL);
16713 vim_free(enc);
16714# endif
16715 }
16716}
16717#endif
16718
16719/*
16720 * "stridx()" function
16721 */
16722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016723f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 typval_T *argvars;
16725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726{
16727 char_u buf[NUMBUFLEN];
16728 char_u *needle;
16729 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016732 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016734 needle = get_tv_string_chk(&argvars[1]);
16735 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016736 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016737 if (needle == NULL || haystack == NULL)
16738 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739
Bram Moolenaar33570922005-01-25 22:26:29 +000016740 if (argvars[2].v_type != VAR_UNKNOWN)
16741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 int error = FALSE;
16743
16744 start_idx = get_tv_number_chk(&argvars[2], &error);
16745 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016746 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016747 if (start_idx >= 0)
16748 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016749 }
16750
16751 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16752 if (pos != NULL)
16753 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754}
16755
16756/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016757 * "string()" function
16758 */
16759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016760f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016761 typval_T *argvars;
16762 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016763{
16764 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016765 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016768 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016769 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016770 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016771 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772}
16773
16774/*
16775 * "strlen()" function
16776 */
16777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016778f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016779 typval_T *argvars;
16780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016782 rettv->vval.v_number = (varnumber_T)(STRLEN(
16783 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784}
16785
16786/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016787 * "strchars()" function
16788 */
16789 static void
16790f_strchars(argvars, rettv)
16791 typval_T *argvars;
16792 typval_T *rettv;
16793{
16794 char_u *s = get_tv_string(&argvars[0]);
16795#ifdef FEAT_MBYTE
16796 varnumber_T len = 0;
16797
16798 while (*s != NUL)
16799 {
16800 mb_cptr2char_adv(&s);
16801 ++len;
16802 }
16803 rettv->vval.v_number = len;
16804#else
16805 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16806#endif
16807}
16808
16809/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016810 * "strdisplaywidth()" function
16811 */
16812 static void
16813f_strdisplaywidth(argvars, rettv)
16814 typval_T *argvars;
16815 typval_T *rettv;
16816{
16817 char_u *s = get_tv_string(&argvars[0]);
16818 int col = 0;
16819
16820 if (argvars[1].v_type != VAR_UNKNOWN)
16821 col = get_tv_number(&argvars[1]);
16822
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016823 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016824}
16825
16826/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016827 * "strwidth()" function
16828 */
16829 static void
16830f_strwidth(argvars, rettv)
16831 typval_T *argvars;
16832 typval_T *rettv;
16833{
16834 char_u *s = get_tv_string(&argvars[0]);
16835
16836 rettv->vval.v_number = (varnumber_T)(
16837#ifdef FEAT_MBYTE
16838 mb_string2cells(s, -1)
16839#else
16840 STRLEN(s)
16841#endif
16842 );
16843}
16844
16845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 * "strpart()" function
16847 */
16848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016849f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016850 typval_T *argvars;
16851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
16853 char_u *p;
16854 int n;
16855 int len;
16856 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016857 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 slen = (int)STRLEN(p);
16861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016862 n = get_tv_number_chk(&argvars[1], &error);
16863 if (error)
16864 len = 0;
16865 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016866 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 else
16868 len = slen - n; /* default len: all bytes that are available. */
16869
16870 /*
16871 * Only return the overlap between the specified part and the actual
16872 * string.
16873 */
16874 if (n < 0)
16875 {
16876 len += n;
16877 n = 0;
16878 }
16879 else if (n > slen)
16880 n = slen;
16881 if (len < 0)
16882 len = 0;
16883 else if (n + len > slen)
16884 len = slen - n;
16885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016886 rettv->v_type = VAR_STRING;
16887 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888}
16889
16890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 * "strridx()" function
16892 */
16893 static void
16894f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016895 typval_T *argvars;
16896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897{
16898 char_u buf[NUMBUFLEN];
16899 char_u *needle;
16900 char_u *haystack;
16901 char_u *rest;
16902 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016903 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905 needle = get_tv_string_chk(&argvars[1]);
16906 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907
16908 rettv->vval.v_number = -1;
16909 if (needle == NULL || haystack == NULL)
16910 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016911
16912 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016913 if (argvars[2].v_type != VAR_UNKNOWN)
16914 {
16915 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016916 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016917 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016918 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016919 }
16920 else
16921 end_idx = haystack_len;
16922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016924 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016926 lastmatch = haystack + end_idx;
16927 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016929 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 for (rest = haystack; *rest != '\0'; ++rest)
16931 {
16932 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016933 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 break;
16935 lastmatch = rest;
16936 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016937 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938
16939 if (lastmatch == NULL)
16940 rettv->vval.v_number = -1;
16941 else
16942 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16943}
16944
16945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 * "strtrans()" function
16947 */
16948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016949f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 typval_T *argvars;
16951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953 rettv->v_type = VAR_STRING;
16954 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955}
16956
16957/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 * "submatch()" function
16959 */
16960 static void
16961f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 typval_T *argvars;
16963 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964{
16965 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016966 rettv->vval.v_string =
16967 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968}
16969
16970/*
16971 * "substitute()" function
16972 */
16973 static void
16974f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016975 typval_T *argvars;
16976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977{
16978 char_u patbuf[NUMBUFLEN];
16979 char_u subbuf[NUMBUFLEN];
16980 char_u flagsbuf[NUMBUFLEN];
16981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016982 char_u *str = get_tv_string_chk(&argvars[0]);
16983 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16984 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16985 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16986
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16989 rettv->vval.v_string = NULL;
16990 else
16991 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992}
16993
16994/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016995 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001{
17002 int id = 0;
17003#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017004 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 long col;
17006 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017007 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 lnum = get_tv_lnum(argvars); /* -1 on type error */
17010 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17011 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017014 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017015 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016#endif
17017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017018 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019}
17020
17021/*
17022 * "synIDattr(id, what [, mode])" function
17023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028{
17029 char_u *p = NULL;
17030#ifdef FEAT_SYN_HL
17031 int id;
17032 char_u *what;
17033 char_u *mode;
17034 char_u modebuf[NUMBUFLEN];
17035 int modec;
17036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037 id = get_tv_number(&argvars[0]);
17038 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017039 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017041 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017043 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 modec = 0; /* replace invalid with current */
17045 }
17046 else
17047 {
17048#ifdef FEAT_GUI
17049 if (gui.in_use)
17050 modec = 'g';
17051 else
17052#endif
17053 if (t_colors > 1)
17054 modec = 'c';
17055 else
17056 modec = 't';
17057 }
17058
17059
17060 switch (TOLOWER_ASC(what[0]))
17061 {
17062 case 'b':
17063 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17064 p = highlight_color(id, what, modec);
17065 else /* bold */
17066 p = highlight_has_attr(id, HL_BOLD, modec);
17067 break;
17068
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017069 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 p = highlight_color(id, what, modec);
17071 break;
17072
17073 case 'i':
17074 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17075 p = highlight_has_attr(id, HL_INVERSE, modec);
17076 else /* italic */
17077 p = highlight_has_attr(id, HL_ITALIC, modec);
17078 break;
17079
17080 case 'n': /* name */
17081 p = get_highlight_name(NULL, id - 1);
17082 break;
17083
17084 case 'r': /* reverse */
17085 p = highlight_has_attr(id, HL_INVERSE, modec);
17086 break;
17087
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017088 case 's':
17089 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17090 p = highlight_color(id, what, modec);
17091 else /* standout */
17092 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 break;
17094
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017095 case 'u':
17096 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17097 /* underline */
17098 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17099 else
17100 /* undercurl */
17101 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 break;
17103 }
17104
17105 if (p != NULL)
17106 p = vim_strsave(p);
17107#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017108 rettv->v_type = VAR_STRING;
17109 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110}
17111
17112/*
17113 * "synIDtrans(id)" function
17114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
17120 int id;
17121
17122#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
17125 if (id > 0)
17126 id = syn_get_final_id(id);
17127 else
17128#endif
17129 id = 0;
17130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132}
17133
17134/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017135 * "synstack(lnum, col)" function
17136 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017137 static void
17138f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017139 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017140 typval_T *rettv;
17141{
17142#ifdef FEAT_SYN_HL
17143 long lnum;
17144 long col;
17145 int i;
17146 int id;
17147#endif
17148
17149 rettv->v_type = VAR_LIST;
17150 rettv->vval.v_list = NULL;
17151
17152#ifdef FEAT_SYN_HL
17153 lnum = get_tv_lnum(argvars); /* -1 on type error */
17154 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17155
17156 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017157 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017158 && rettv_list_alloc(rettv) != FAIL)
17159 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017160 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017161 for (i = 0; ; ++i)
17162 {
17163 id = syn_get_stack_item(i);
17164 if (id < 0)
17165 break;
17166 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17167 break;
17168 }
17169 }
17170#endif
17171}
17172
17173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 * "system()" function
17175 */
17176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017178 typval_T *argvars;
17179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017181 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017183 char_u *infile = NULL;
17184 char_u buf[NUMBUFLEN];
17185 int err = FALSE;
17186 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017188 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017189 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017192 {
17193 /*
17194 * Write the string to a temp file, to be used for input of the shell
17195 * command.
17196 */
17197 if ((infile = vim_tempname('i')) == NULL)
17198 {
17199 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017200 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017201 }
17202
17203 fd = mch_fopen((char *)infile, WRITEBIN);
17204 if (fd == NULL)
17205 {
17206 EMSG2(_(e_notopen), infile);
17207 goto done;
17208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 p = get_tv_string_buf_chk(&argvars[1], buf);
17210 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017211 {
17212 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017213 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017214 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017215 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17216 err = TRUE;
17217 if (fclose(fd) != 0)
17218 err = TRUE;
17219 if (err)
17220 {
17221 EMSG(_("E677: Error writing temp file"));
17222 goto done;
17223 }
17224 }
17225
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017226 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17227 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017228
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229#ifdef USE_CR
17230 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017231 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 {
17233 char_u *s;
17234
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017235 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 {
17237 if (*s == CAR)
17238 *s = NL;
17239 }
17240 }
17241#else
17242# ifdef USE_CRNL
17243 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017244 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 {
17246 char_u *s, *d;
17247
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017248 d = res;
17249 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 {
17251 if (s[0] == CAR && s[1] == NL)
17252 ++s;
17253 *d++ = *s;
17254 }
17255 *d = NUL;
17256 }
17257# endif
17258#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017259
17260done:
17261 if (infile != NULL)
17262 {
17263 mch_remove(infile);
17264 vim_free(infile);
17265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017266 rettv->v_type = VAR_STRING;
17267 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268}
17269
17270/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017271 * "tabpagebuflist()" function
17272 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017273 static void
17274f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017275 typval_T *argvars UNUSED;
17276 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017277{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017278#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017279 tabpage_T *tp;
17280 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017281
17282 if (argvars[0].v_type == VAR_UNKNOWN)
17283 wp = firstwin;
17284 else
17285 {
17286 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17287 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017288 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017289 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017290 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017291 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017292 for (; wp != NULL; wp = wp->w_next)
17293 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017294 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017295 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017296 }
17297#endif
17298}
17299
17300
17301/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017302 * "tabpagenr()" function
17303 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017304 static void
17305f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017306 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017307 typval_T *rettv;
17308{
17309 int nr = 1;
17310#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017311 char_u *arg;
17312
17313 if (argvars[0].v_type != VAR_UNKNOWN)
17314 {
17315 arg = get_tv_string_chk(&argvars[0]);
17316 nr = 0;
17317 if (arg != NULL)
17318 {
17319 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017320 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017321 else
17322 EMSG2(_(e_invexpr2), arg);
17323 }
17324 }
17325 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017326 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017327#endif
17328 rettv->vval.v_number = nr;
17329}
17330
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017331
17332#ifdef FEAT_WINDOWS
17333static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17334
17335/*
17336 * Common code for tabpagewinnr() and winnr().
17337 */
17338 static int
17339get_winnr(tp, argvar)
17340 tabpage_T *tp;
17341 typval_T *argvar;
17342{
17343 win_T *twin;
17344 int nr = 1;
17345 win_T *wp;
17346 char_u *arg;
17347
17348 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17349 if (argvar->v_type != VAR_UNKNOWN)
17350 {
17351 arg = get_tv_string_chk(argvar);
17352 if (arg == NULL)
17353 nr = 0; /* type error; errmsg already given */
17354 else if (STRCMP(arg, "$") == 0)
17355 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17356 else if (STRCMP(arg, "#") == 0)
17357 {
17358 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17359 if (twin == NULL)
17360 nr = 0;
17361 }
17362 else
17363 {
17364 EMSG2(_(e_invexpr2), arg);
17365 nr = 0;
17366 }
17367 }
17368
17369 if (nr > 0)
17370 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17371 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017372 {
17373 if (wp == NULL)
17374 {
17375 /* didn't find it in this tabpage */
17376 nr = 0;
17377 break;
17378 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017379 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017380 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017381 return nr;
17382}
17383#endif
17384
17385/*
17386 * "tabpagewinnr()" function
17387 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017388 static void
17389f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017390 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017391 typval_T *rettv;
17392{
17393 int nr = 1;
17394#ifdef FEAT_WINDOWS
17395 tabpage_T *tp;
17396
17397 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17398 if (tp == NULL)
17399 nr = 0;
17400 else
17401 nr = get_winnr(tp, &argvars[1]);
17402#endif
17403 rettv->vval.v_number = nr;
17404}
17405
17406
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017407/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017408 * "tagfiles()" function
17409 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017410 static void
17411f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017412 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017413 typval_T *rettv;
17414{
17415 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017416 tagname_T tn;
17417 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017418
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017419 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017420 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017421
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017422 for (first = TRUE; ; first = FALSE)
17423 if (get_tagfname(&tn, first, fname) == FAIL
17424 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017425 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017426 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017427}
17428
17429/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017430 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017431 */
17432 static void
17433f_taglist(argvars, rettv)
17434 typval_T *argvars;
17435 typval_T *rettv;
17436{
17437 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017438
17439 tag_pattern = get_tv_string(&argvars[0]);
17440
17441 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017442 if (*tag_pattern == NUL)
17443 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017444
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017445 if (rettv_list_alloc(rettv) == OK)
17446 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017447}
17448
17449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 * "tempname()" function
17451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017453f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017454 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456{
17457 static int x = 'A';
17458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017459 rettv->v_type = VAR_STRING;
17460 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461
17462 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17463 * names. Skip 'I' and 'O', they are used for shell redirection. */
17464 do
17465 {
17466 if (x == 'Z')
17467 x = '0';
17468 else if (x == '9')
17469 x = 'A';
17470 else
17471 {
17472#ifdef EBCDIC
17473 if (x == 'I')
17474 x = 'J';
17475 else if (x == 'R')
17476 x = 'S';
17477 else
17478#endif
17479 ++x;
17480 }
17481 } while (x == 'I' || x == 'O');
17482}
17483
17484/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017485 * "test(list)" function: Just checking the walls...
17486 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017487 static void
17488f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017489 typval_T *argvars UNUSED;
17490 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017491{
17492 /* Used for unit testing. Change the code below to your liking. */
17493#if 0
17494 listitem_T *li;
17495 list_T *l;
17496 char_u *bad, *good;
17497
17498 if (argvars[0].v_type != VAR_LIST)
17499 return;
17500 l = argvars[0].vval.v_list;
17501 if (l == NULL)
17502 return;
17503 li = l->lv_first;
17504 if (li == NULL)
17505 return;
17506 bad = get_tv_string(&li->li_tv);
17507 li = li->li_next;
17508 if (li == NULL)
17509 return;
17510 good = get_tv_string(&li->li_tv);
17511 rettv->vval.v_number = test_edit_score(bad, good);
17512#endif
17513}
17514
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017515#ifdef FEAT_FLOAT
17516/*
17517 * "tan()" function
17518 */
17519 static void
17520f_tan(argvars, rettv)
17521 typval_T *argvars;
17522 typval_T *rettv;
17523{
17524 float_T f;
17525
17526 rettv->v_type = VAR_FLOAT;
17527 if (get_float_arg(argvars, &f) == OK)
17528 rettv->vval.v_float = tan(f);
17529 else
17530 rettv->vval.v_float = 0.0;
17531}
17532
17533/*
17534 * "tanh()" function
17535 */
17536 static void
17537f_tanh(argvars, rettv)
17538 typval_T *argvars;
17539 typval_T *rettv;
17540{
17541 float_T f;
17542
17543 rettv->v_type = VAR_FLOAT;
17544 if (get_float_arg(argvars, &f) == OK)
17545 rettv->vval.v_float = tanh(f);
17546 else
17547 rettv->vval.v_float = 0.0;
17548}
17549#endif
17550
Bram Moolenaard52d9742005-08-21 22:20:28 +000017551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 * "tolower(string)" function
17553 */
17554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 typval_T *argvars;
17557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558{
17559 char_u *p;
17560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017561 p = vim_strsave(get_tv_string(&argvars[0]));
17562 rettv->v_type = VAR_STRING;
17563 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564
17565 if (p != NULL)
17566 while (*p != NUL)
17567 {
17568#ifdef FEAT_MBYTE
17569 int l;
17570
17571 if (enc_utf8)
17572 {
17573 int c, lc;
17574
17575 c = utf_ptr2char(p);
17576 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017577 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 /* TODO: reallocate string when byte count changes. */
17579 if (utf_char2len(lc) == l)
17580 utf_char2bytes(lc, p);
17581 p += l;
17582 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017583 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 p += l; /* skip multi-byte character */
17585 else
17586#endif
17587 {
17588 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17589 ++p;
17590 }
17591 }
17592}
17593
17594/*
17595 * "toupper(string)" function
17596 */
17597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017598f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 typval_T *argvars;
17600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017602 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017603 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604}
17605
17606/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017607 * "tr(string, fromstr, tostr)" function
17608 */
17609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017610f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 typval_T *argvars;
17612 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017613{
17614 char_u *instr;
17615 char_u *fromstr;
17616 char_u *tostr;
17617 char_u *p;
17618#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017619 int inlen;
17620 int fromlen;
17621 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017622 int idx;
17623 char_u *cpstr;
17624 int cplen;
17625 int first = TRUE;
17626#endif
17627 char_u buf[NUMBUFLEN];
17628 char_u buf2[NUMBUFLEN];
17629 garray_T ga;
17630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017631 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017632 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17633 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017634
17635 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636 rettv->v_type = VAR_STRING;
17637 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017638 if (fromstr == NULL || tostr == NULL)
17639 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017640 ga_init2(&ga, (int)sizeof(char), 80);
17641
17642#ifdef FEAT_MBYTE
17643 if (!has_mbyte)
17644#endif
17645 /* not multi-byte: fromstr and tostr must be the same length */
17646 if (STRLEN(fromstr) != STRLEN(tostr))
17647 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017648#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017649error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017650#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017651 EMSG2(_(e_invarg2), fromstr);
17652 ga_clear(&ga);
17653 return;
17654 }
17655
17656 /* fromstr and tostr have to contain the same number of chars */
17657 while (*instr != NUL)
17658 {
17659#ifdef FEAT_MBYTE
17660 if (has_mbyte)
17661 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017662 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017663 cpstr = instr;
17664 cplen = inlen;
17665 idx = 0;
17666 for (p = fromstr; *p != NUL; p += fromlen)
17667 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017668 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017669 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17670 {
17671 for (p = tostr; *p != NUL; p += tolen)
17672 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017673 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017674 if (idx-- == 0)
17675 {
17676 cplen = tolen;
17677 cpstr = p;
17678 break;
17679 }
17680 }
17681 if (*p == NUL) /* tostr is shorter than fromstr */
17682 goto error;
17683 break;
17684 }
17685 ++idx;
17686 }
17687
17688 if (first && cpstr == instr)
17689 {
17690 /* Check that fromstr and tostr have the same number of
17691 * (multi-byte) characters. Done only once when a character
17692 * of instr doesn't appear in fromstr. */
17693 first = FALSE;
17694 for (p = tostr; *p != NUL; p += tolen)
17695 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017696 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017697 --idx;
17698 }
17699 if (idx != 0)
17700 goto error;
17701 }
17702
17703 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017704 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017705 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017706
17707 instr += inlen;
17708 }
17709 else
17710#endif
17711 {
17712 /* When not using multi-byte chars we can do it faster. */
17713 p = vim_strchr(fromstr, *instr);
17714 if (p != NULL)
17715 ga_append(&ga, tostr[p - fromstr]);
17716 else
17717 ga_append(&ga, *instr);
17718 ++instr;
17719 }
17720 }
17721
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017722 /* add a terminating NUL */
17723 ga_grow(&ga, 1);
17724 ga_append(&ga, NUL);
17725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017726 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017727}
17728
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017729#ifdef FEAT_FLOAT
17730/*
17731 * "trunc({float})" function
17732 */
17733 static void
17734f_trunc(argvars, rettv)
17735 typval_T *argvars;
17736 typval_T *rettv;
17737{
17738 float_T f;
17739
17740 rettv->v_type = VAR_FLOAT;
17741 if (get_float_arg(argvars, &f) == OK)
17742 /* trunc() is not in C90, use floor() or ceil() instead. */
17743 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17744 else
17745 rettv->vval.v_float = 0.0;
17746}
17747#endif
17748
Bram Moolenaar8299df92004-07-10 09:47:34 +000017749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 * "type(expr)" function
17751 */
17752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 typval_T *argvars;
17755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017757 int n;
17758
17759 switch (argvars[0].v_type)
17760 {
17761 case VAR_NUMBER: n = 0; break;
17762 case VAR_STRING: n = 1; break;
17763 case VAR_FUNC: n = 2; break;
17764 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017765 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017766#ifdef FEAT_FLOAT
17767 case VAR_FLOAT: n = 5; break;
17768#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017769 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17770 }
17771 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772}
17773
17774/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017775 * "undofile(name)" function
17776 */
17777 static void
17778f_undofile(argvars, rettv)
17779 typval_T *argvars;
17780 typval_T *rettv;
17781{
17782 rettv->v_type = VAR_STRING;
17783#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017784 {
17785 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17786
17787 if (ffname != NULL)
17788 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17789 vim_free(ffname);
17790 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017791#else
17792 rettv->vval.v_string = NULL;
17793#endif
17794}
17795
17796/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017797 * "undotree()" function
17798 */
17799 static void
17800f_undotree(argvars, rettv)
17801 typval_T *argvars UNUSED;
17802 typval_T *rettv;
17803{
17804 if (rettv_dict_alloc(rettv) == OK)
17805 {
17806 dict_T *dict = rettv->vval.v_dict;
17807 list_T *list;
17808
Bram Moolenaar730cde92010-06-27 05:18:54 +020017809 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017810 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017811 dict_add_nr_str(dict, "save_last",
17812 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017813 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17814 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017815 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017816
17817 list = list_alloc();
17818 if (list != NULL)
17819 {
17820 u_eval_tree(curbuf->b_u_oldhead, list);
17821 dict_add_list(dict, "entries", list);
17822 }
17823 }
17824}
17825
17826/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017827 * "values(dict)" function
17828 */
17829 static void
17830f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017831 typval_T *argvars;
17832 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017833{
17834 dict_list(argvars, rettv, 1);
17835}
17836
17837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 * "virtcol(string)" function
17839 */
17840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017841f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017842 typval_T *argvars;
17843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844{
17845 colnr_T vcol = 0;
17846 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017847 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017849 fp = var2fpos(&argvars[0], FALSE, &fnum);
17850 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17851 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 {
17853 getvvcol(curwin, fp, NULL, NULL, &vcol);
17854 ++vcol;
17855 }
17856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017857 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858}
17859
17860/*
17861 * "visualmode()" function
17862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017865 typval_T *argvars UNUSED;
17866 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867{
17868#ifdef FEAT_VISUAL
17869 char_u str[2];
17870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017871 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 str[0] = curbuf->b_visual_mode_eval;
17873 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017874 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875
17876 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017877 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879#endif
17880}
17881
17882/*
17883 * "winbufnr(nr)" function
17884 */
17885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017886f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017887 typval_T *argvars;
17888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889{
17890 win_T *wp;
17891
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017892 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897}
17898
17899/*
17900 * "wincol()" function
17901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017903f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906{
17907 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017908 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909}
17910
17911/*
17912 * "winheight(nr)" function
17913 */
17914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017915f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017916 typval_T *argvars;
17917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918{
17919 win_T *wp;
17920
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017921 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017923 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017925 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926}
17927
17928/*
17929 * "winline()" function
17930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017932f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017933 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935{
17936 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017937 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938}
17939
17940/*
17941 * "winnr()" function
17942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017944f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017945 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947{
17948 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017949
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017951 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954}
17955
17956/*
17957 * "winrestcmd()" function
17958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017961 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963{
17964#ifdef FEAT_WINDOWS
17965 win_T *wp;
17966 int winnr = 1;
17967 garray_T ga;
17968 char_u buf[50];
17969
17970 ga_init2(&ga, (int)sizeof(char), 70);
17971 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17972 {
17973 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17974 ga_concat(&ga, buf);
17975# ifdef FEAT_VERTSPLIT
17976 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17977 ga_concat(&ga, buf);
17978# endif
17979 ++winnr;
17980 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017981 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017983 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017985 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988}
17989
17990/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017991 * "winrestview()" function
17992 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017993 static void
17994f_winrestview(argvars, rettv)
17995 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017996 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017997{
17998 dict_T *dict;
17999
18000 if (argvars[0].v_type != VAR_DICT
18001 || (dict = argvars[0].vval.v_dict) == NULL)
18002 EMSG(_(e_invarg));
18003 else
18004 {
18005 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18006 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18007#ifdef FEAT_VIRTUALEDIT
18008 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18009#endif
18010 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018011 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018012
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018013 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018014#ifdef FEAT_DIFF
18015 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18016#endif
18017 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18018 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18019
18020 check_cursor();
18021 changed_cline_bef_curs();
18022 invalidate_botline();
18023 redraw_later(VALID);
18024
18025 if (curwin->w_topline == 0)
18026 curwin->w_topline = 1;
18027 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18028 curwin->w_topline = curbuf->b_ml.ml_line_count;
18029#ifdef FEAT_DIFF
18030 check_topfill(curwin, TRUE);
18031#endif
18032 }
18033}
18034
18035/*
18036 * "winsaveview()" function
18037 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018038 static void
18039f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018040 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018041 typval_T *rettv;
18042{
18043 dict_T *dict;
18044
Bram Moolenaara800b422010-06-27 01:15:55 +020018045 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018046 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018047 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018048
18049 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18050 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18051#ifdef FEAT_VIRTUALEDIT
18052 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18053#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018054 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018055 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18056
18057 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18058#ifdef FEAT_DIFF
18059 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18060#endif
18061 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18062 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18063}
18064
18065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 * "winwidth(nr)" function
18067 */
18068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018069f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018070 typval_T *argvars;
18071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072{
18073 win_T *wp;
18074
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018075 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 else
18079#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018080 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018082 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083#endif
18084}
18085
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018087 * "writefile()" function
18088 */
18089 static void
18090f_writefile(argvars, rettv)
18091 typval_T *argvars;
18092 typval_T *rettv;
18093{
18094 int binary = FALSE;
18095 char_u *fname;
18096 FILE *fd;
18097 listitem_T *li;
18098 char_u *s;
18099 int ret = 0;
18100 int c;
18101
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018102 if (check_restricted() || check_secure())
18103 return;
18104
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018105 if (argvars[0].v_type != VAR_LIST)
18106 {
18107 EMSG2(_(e_listarg), "writefile()");
18108 return;
18109 }
18110 if (argvars[0].vval.v_list == NULL)
18111 return;
18112
18113 if (argvars[2].v_type != VAR_UNKNOWN
18114 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18115 binary = TRUE;
18116
18117 /* Always open the file in binary mode, library functions have a mind of
18118 * their own about CR-LF conversion. */
18119 fname = get_tv_string(&argvars[1]);
18120 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18121 {
18122 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18123 ret = -1;
18124 }
18125 else
18126 {
18127 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18128 li = li->li_next)
18129 {
18130 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18131 {
18132 if (*s == '\n')
18133 c = putc(NUL, fd);
18134 else
18135 c = putc(*s, fd);
18136 if (c == EOF)
18137 {
18138 ret = -1;
18139 break;
18140 }
18141 }
18142 if (!binary || li->li_next != NULL)
18143 if (putc('\n', fd) == EOF)
18144 {
18145 ret = -1;
18146 break;
18147 }
18148 if (ret < 0)
18149 {
18150 EMSG(_(e_write));
18151 break;
18152 }
18153 }
18154 fclose(fd);
18155 }
18156
18157 rettv->vval.v_number = ret;
18158}
18159
18160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018162 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 */
18164 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018165var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018166 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018167 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018168 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018170 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018172 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173
Bram Moolenaara5525202006-03-02 22:52:09 +000018174 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018175 if (varp->v_type == VAR_LIST)
18176 {
18177 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018178 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018179 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018180 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018181
18182 l = varp->vval.v_list;
18183 if (l == NULL)
18184 return NULL;
18185
18186 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018187 pos.lnum = list_find_nr(l, 0L, &error);
18188 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018189 return NULL; /* invalid line number */
18190
18191 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018192 pos.col = list_find_nr(l, 1L, &error);
18193 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018194 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018195 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018196
18197 /* We accept "$" for the column number: last column. */
18198 li = list_find(l, 1L);
18199 if (li != NULL && li->li_tv.v_type == VAR_STRING
18200 && li->li_tv.vval.v_string != NULL
18201 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18202 pos.col = len + 1;
18203
Bram Moolenaara5525202006-03-02 22:52:09 +000018204 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018205 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018206 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018207 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018208
Bram Moolenaara5525202006-03-02 22:52:09 +000018209#ifdef FEAT_VIRTUALEDIT
18210 /* Get the virtual offset. Defaults to zero. */
18211 pos.coladd = list_find_nr(l, 2L, &error);
18212 if (error)
18213 pos.coladd = 0;
18214#endif
18215
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018216 return &pos;
18217 }
18218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018219 name = get_tv_string_chk(varp);
18220 if (name == NULL)
18221 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018222 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018224#ifdef FEAT_VISUAL
18225 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18226 {
18227 if (VIsual_active)
18228 return &VIsual;
18229 return &curwin->w_cursor;
18230 }
18231#endif
18232 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018234 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18236 return NULL;
18237 return pp;
18238 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018239
18240#ifdef FEAT_VIRTUALEDIT
18241 pos.coladd = 0;
18242#endif
18243
Bram Moolenaar477933c2007-07-17 14:32:23 +000018244 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018245 {
18246 pos.col = 0;
18247 if (name[1] == '0') /* "w0": first visible line */
18248 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018249 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018250 pos.lnum = curwin->w_topline;
18251 return &pos;
18252 }
18253 else if (name[1] == '$') /* "w$": last visible line */
18254 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018255 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018256 pos.lnum = curwin->w_botline - 1;
18257 return &pos;
18258 }
18259 }
18260 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018262 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 {
18264 pos.lnum = curbuf->b_ml.ml_line_count;
18265 pos.col = 0;
18266 }
18267 else
18268 {
18269 pos.lnum = curwin->w_cursor.lnum;
18270 pos.col = (colnr_T)STRLEN(ml_get_curline());
18271 }
18272 return &pos;
18273 }
18274 return NULL;
18275}
18276
18277/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018278 * Convert list in "arg" into a position and optional file number.
18279 * When "fnump" is NULL there is no file number, only 3 items.
18280 * Note that the column is passed on as-is, the caller may want to decrement
18281 * it to use 1 for the first column.
18282 * Return FAIL when conversion is not possible, doesn't check the position for
18283 * validity.
18284 */
18285 static int
18286list2fpos(arg, posp, fnump)
18287 typval_T *arg;
18288 pos_T *posp;
18289 int *fnump;
18290{
18291 list_T *l = arg->vval.v_list;
18292 long i = 0;
18293 long n;
18294
Bram Moolenaarbde35262006-07-23 20:12:24 +000018295 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18296 * when "fnump" isn't NULL and "coladd" is optional. */
18297 if (arg->v_type != VAR_LIST
18298 || l == NULL
18299 || l->lv_len < (fnump == NULL ? 2 : 3)
18300 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018301 return FAIL;
18302
18303 if (fnump != NULL)
18304 {
18305 n = list_find_nr(l, i++, NULL); /* fnum */
18306 if (n < 0)
18307 return FAIL;
18308 if (n == 0)
18309 n = curbuf->b_fnum; /* current buffer */
18310 *fnump = n;
18311 }
18312
18313 n = list_find_nr(l, i++, NULL); /* lnum */
18314 if (n < 0)
18315 return FAIL;
18316 posp->lnum = n;
18317
18318 n = list_find_nr(l, i++, NULL); /* col */
18319 if (n < 0)
18320 return FAIL;
18321 posp->col = n;
18322
18323#ifdef FEAT_VIRTUALEDIT
18324 n = list_find_nr(l, i, NULL);
18325 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018326 posp->coladd = 0;
18327 else
18328 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018329#endif
18330
18331 return OK;
18332}
18333
18334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 * Get the length of an environment variable name.
18336 * Advance "arg" to the first character after the name.
18337 * Return 0 for error.
18338 */
18339 static int
18340get_env_len(arg)
18341 char_u **arg;
18342{
18343 char_u *p;
18344 int len;
18345
18346 for (p = *arg; vim_isIDc(*p); ++p)
18347 ;
18348 if (p == *arg) /* no name found */
18349 return 0;
18350
18351 len = (int)(p - *arg);
18352 *arg = p;
18353 return len;
18354}
18355
18356/*
18357 * Get the length of the name of a function or internal variable.
18358 * "arg" is advanced to the first non-white character after the name.
18359 * Return 0 if something is wrong.
18360 */
18361 static int
18362get_id_len(arg)
18363 char_u **arg;
18364{
18365 char_u *p;
18366 int len;
18367
18368 /* Find the end of the name. */
18369 for (p = *arg; eval_isnamec(*p); ++p)
18370 ;
18371 if (p == *arg) /* no name found */
18372 return 0;
18373
18374 len = (int)(p - *arg);
18375 *arg = skipwhite(p);
18376
18377 return len;
18378}
18379
18380/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018381 * Get the length of the name of a variable or function.
18382 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018384 * Return -1 if curly braces expansion failed.
18385 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 * If the name contains 'magic' {}'s, expand them and return the
18387 * expanded name in an allocated string via 'alias' - caller must free.
18388 */
18389 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018390get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 char_u **arg;
18392 char_u **alias;
18393 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018394 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395{
18396 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 char_u *p;
18398 char_u *expr_start;
18399 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400
18401 *alias = NULL; /* default to no alias */
18402
18403 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18404 && (*arg)[2] == (int)KE_SNR)
18405 {
18406 /* hard coded <SNR>, already translated */
18407 *arg += 3;
18408 return get_id_len(arg) + 3;
18409 }
18410 len = eval_fname_script(*arg);
18411 if (len > 0)
18412 {
18413 /* literal "<SID>", "s:" or "<SNR>" */
18414 *arg += len;
18415 }
18416
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018420 p = find_name_end(*arg, &expr_start, &expr_end,
18421 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422 if (expr_start != NULL)
18423 {
18424 char_u *temp_string;
18425
18426 if (!evaluate)
18427 {
18428 len += (int)(p - *arg);
18429 *arg = skipwhite(p);
18430 return len;
18431 }
18432
18433 /*
18434 * Include any <SID> etc in the expanded string:
18435 * Thus the -len here.
18436 */
18437 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18438 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018439 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 *alias = temp_string;
18441 *arg = skipwhite(p);
18442 return (int)STRLEN(temp_string);
18443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444
18445 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018446 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 EMSG2(_(e_invexpr2), *arg);
18448
18449 return len;
18450}
18451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018452/*
18453 * Find the end of a variable or function name, taking care of magic braces.
18454 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18455 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018456 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457 * Return a pointer to just after the name. Equal to "arg" if there is no
18458 * valid name.
18459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018461find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 char_u *arg;
18463 char_u **expr_start;
18464 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018465 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 int mb_nest = 0;
18468 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 char_u *p;
18470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018471 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473 *expr_start = NULL;
18474 *expr_end = NULL;
18475 }
18476
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018477 /* Quick check for valid starting character. */
18478 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18479 return arg;
18480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481 for (p = arg; *p != NUL
18482 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018483 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018484 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018485 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018486 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018488 if (*p == '\'')
18489 {
18490 /* skip over 'string' to avoid counting [ and ] inside it. */
18491 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18492 ;
18493 if (*p == NUL)
18494 break;
18495 }
18496 else if (*p == '"')
18497 {
18498 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18499 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18500 if (*p == '\\' && p[1] != NUL)
18501 ++p;
18502 if (*p == NUL)
18503 break;
18504 }
18505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018506 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018508 if (*p == '[')
18509 ++br_nest;
18510 else if (*p == ']')
18511 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018514 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018516 if (*p == '{')
18517 {
18518 mb_nest++;
18519 if (expr_start != NULL && *expr_start == NULL)
18520 *expr_start = p;
18521 }
18522 else if (*p == '}')
18523 {
18524 mb_nest--;
18525 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18526 *expr_end = p;
18527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 }
18530
18531 return p;
18532}
18533
18534/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018535 * Expands out the 'magic' {}'s in a variable/function name.
18536 * Note that this can call itself recursively, to deal with
18537 * constructs like foo{bar}{baz}{bam}
18538 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18539 * "in_start" ^
18540 * "expr_start" ^
18541 * "expr_end" ^
18542 * "in_end" ^
18543 *
18544 * Returns a new allocated string, which the caller must free.
18545 * Returns NULL for failure.
18546 */
18547 static char_u *
18548make_expanded_name(in_start, expr_start, expr_end, in_end)
18549 char_u *in_start;
18550 char_u *expr_start;
18551 char_u *expr_end;
18552 char_u *in_end;
18553{
18554 char_u c1;
18555 char_u *retval = NULL;
18556 char_u *temp_result;
18557 char_u *nextcmd = NULL;
18558
18559 if (expr_end == NULL || in_end == NULL)
18560 return NULL;
18561 *expr_start = NUL;
18562 *expr_end = NUL;
18563 c1 = *in_end;
18564 *in_end = NUL;
18565
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018566 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018567 if (temp_result != NULL && nextcmd == NULL)
18568 {
18569 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18570 + (in_end - expr_end) + 1));
18571 if (retval != NULL)
18572 {
18573 STRCPY(retval, in_start);
18574 STRCAT(retval, temp_result);
18575 STRCAT(retval, expr_end + 1);
18576 }
18577 }
18578 vim_free(temp_result);
18579
18580 *in_end = c1; /* put char back for error messages */
18581 *expr_start = '{';
18582 *expr_end = '}';
18583
18584 if (retval != NULL)
18585 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018586 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018587 if (expr_start != NULL)
18588 {
18589 /* Further expansion! */
18590 temp_result = make_expanded_name(retval, expr_start,
18591 expr_end, temp_result);
18592 vim_free(retval);
18593 retval = temp_result;
18594 }
18595 }
18596
18597 return retval;
18598}
18599
18600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018602 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 */
18604 static int
18605eval_isnamec(c)
18606 int c;
18607{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018608 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18609}
18610
18611/*
18612 * Return TRUE if character "c" can be used as the first character in a
18613 * variable or function name (excluding '{' and '}').
18614 */
18615 static int
18616eval_isnamec1(c)
18617 int c;
18618{
18619 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620}
18621
18622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 * Set number v: variable to "val".
18624 */
18625 void
18626set_vim_var_nr(idx, val)
18627 int idx;
18628 long val;
18629{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018630 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631}
18632
18633/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018634 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 */
18636 long
18637get_vim_var_nr(idx)
18638 int idx;
18639{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018640 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641}
18642
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018643/*
18644 * Get string v: variable value. Uses a static buffer, can only be used once.
18645 */
18646 char_u *
18647get_vim_var_str(idx)
18648 int idx;
18649{
18650 return get_tv_string(&vimvars[idx].vv_tv);
18651}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018652
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018654 * Get List v: variable value. Caller must take care of reference count when
18655 * needed.
18656 */
18657 list_T *
18658get_vim_var_list(idx)
18659 int idx;
18660{
18661 return vimvars[idx].vv_list;
18662}
18663
18664/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018665 * Set v:char to character "c".
18666 */
18667 void
18668set_vim_var_char(c)
18669 int c;
18670{
18671#ifdef FEAT_MBYTE
18672 char_u buf[MB_MAXBYTES];
18673#else
18674 char_u buf[2];
18675#endif
18676
18677#ifdef FEAT_MBYTE
18678 if (has_mbyte)
18679 buf[(*mb_char2bytes)(c, buf)] = NUL;
18680 else
18681#endif
18682 {
18683 buf[0] = c;
18684 buf[1] = NUL;
18685 }
18686 set_vim_var_string(VV_CHAR, buf, -1);
18687}
18688
18689/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018690 * Set v:count to "count" and v:count1 to "count1".
18691 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 */
18693 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018694set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 long count;
18696 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018697 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018699 if (set_prevcount)
18700 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018701 vimvars[VV_COUNT].vv_nr = count;
18702 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703}
18704
18705/*
18706 * Set string v: variable to a copy of "val".
18707 */
18708 void
18709set_vim_var_string(idx, val, len)
18710 int idx;
18711 char_u *val;
18712 int len; /* length of "val" to use or -1 (whole string) */
18713{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018714 /* Need to do this (at least) once, since we can't initialize a union.
18715 * Will always be invoked when "v:progname" is set. */
18716 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18717
Bram Moolenaare9a41262005-01-15 22:18:47 +000018718 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018720 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018722 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018724 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725}
18726
18727/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018728 * Set List v: variable to "val".
18729 */
18730 void
18731set_vim_var_list(idx, val)
18732 int idx;
18733 list_T *val;
18734{
18735 list_unref(vimvars[idx].vv_list);
18736 vimvars[idx].vv_list = val;
18737 if (val != NULL)
18738 ++val->lv_refcount;
18739}
18740
18741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 * Set v:register if needed.
18743 */
18744 void
18745set_reg_var(c)
18746 int c;
18747{
18748 char_u regname;
18749
18750 if (c == 0 || c == ' ')
18751 regname = '"';
18752 else
18753 regname = c;
18754 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018755 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 set_vim_var_string(VV_REG, &regname, 1);
18757}
18758
18759/*
18760 * Get or set v:exception. If "oldval" == NULL, return the current value.
18761 * Otherwise, restore the value to "oldval" and return NULL.
18762 * Must always be called in pairs to save and restore v:exception! Does not
18763 * take care of memory allocations.
18764 */
18765 char_u *
18766v_exception(oldval)
18767 char_u *oldval;
18768{
18769 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018770 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771
Bram Moolenaare9a41262005-01-15 22:18:47 +000018772 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 return NULL;
18774}
18775
18776/*
18777 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18778 * Otherwise, restore the value to "oldval" and return NULL.
18779 * Must always be called in pairs to save and restore v:throwpoint! Does not
18780 * take care of memory allocations.
18781 */
18782 char_u *
18783v_throwpoint(oldval)
18784 char_u *oldval;
18785{
18786 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018787 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788
Bram Moolenaare9a41262005-01-15 22:18:47 +000018789 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 return NULL;
18791}
18792
18793#if defined(FEAT_AUTOCMD) || defined(PROTO)
18794/*
18795 * Set v:cmdarg.
18796 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18797 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18798 * Must always be called in pairs!
18799 */
18800 char_u *
18801set_cmdarg(eap, oldarg)
18802 exarg_T *eap;
18803 char_u *oldarg;
18804{
18805 char_u *oldval;
18806 char_u *newval;
18807 unsigned len;
18808
Bram Moolenaare9a41262005-01-15 22:18:47 +000018809 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018810 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018812 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018813 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018814 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 }
18816
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018817 if (eap->force_bin == FORCE_BIN)
18818 len = 6;
18819 else if (eap->force_bin == FORCE_NOBIN)
18820 len = 8;
18821 else
18822 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018823
18824 if (eap->read_edit)
18825 len += 7;
18826
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018827 if (eap->force_ff != 0)
18828 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18829# ifdef FEAT_MBYTE
18830 if (eap->force_enc != 0)
18831 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018832 if (eap->bad_char != 0)
18833 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018834# endif
18835
18836 newval = alloc(len + 1);
18837 if (newval == NULL)
18838 return NULL;
18839
18840 if (eap->force_bin == FORCE_BIN)
18841 sprintf((char *)newval, " ++bin");
18842 else if (eap->force_bin == FORCE_NOBIN)
18843 sprintf((char *)newval, " ++nobin");
18844 else
18845 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018846
18847 if (eap->read_edit)
18848 STRCAT(newval, " ++edit");
18849
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018850 if (eap->force_ff != 0)
18851 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18852 eap->cmd + eap->force_ff);
18853# ifdef FEAT_MBYTE
18854 if (eap->force_enc != 0)
18855 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18856 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018857 if (eap->bad_char == BAD_KEEP)
18858 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18859 else if (eap->bad_char == BAD_DROP)
18860 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18861 else if (eap->bad_char != 0)
18862 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018863# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018864 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018865 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866}
18867#endif
18868
18869/*
18870 * Get the value of internal variable "name".
18871 * Return OK or FAIL.
18872 */
18873 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018874get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 char_u *name;
18876 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018878 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879{
18880 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 typval_T *tv = NULL;
18882 typval_T atv;
18883 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885
18886 /* truncate the name, so that we can use strcmp() */
18887 cc = name[len];
18888 name[len] = NUL;
18889
18890 /*
18891 * Check for "b:changedtick".
18892 */
18893 if (STRCMP(name, "b:changedtick") == 0)
18894 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018895 atv.v_type = VAR_NUMBER;
18896 atv.vval.v_number = curbuf->b_changedtick;
18897 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 }
18899
18900 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 * Check for user-defined variables.
18902 */
18903 else
18904 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018905 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018907 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 }
18909
Bram Moolenaare9a41262005-01-15 22:18:47 +000018910 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018912 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018913 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 ret = FAIL;
18915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018916 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018917 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918
18919 name[len] = cc;
18920
18921 return ret;
18922}
18923
18924/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018925 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18926 * Also handle function call with Funcref variable: func(expr)
18927 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18928 */
18929 static int
18930handle_subscript(arg, rettv, evaluate, verbose)
18931 char_u **arg;
18932 typval_T *rettv;
18933 int evaluate; /* do more than finding the end */
18934 int verbose; /* give error messages */
18935{
18936 int ret = OK;
18937 dict_T *selfdict = NULL;
18938 char_u *s;
18939 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018940 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018941
18942 while (ret == OK
18943 && (**arg == '['
18944 || (**arg == '.' && rettv->v_type == VAR_DICT)
18945 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18946 && !vim_iswhite(*(*arg - 1)))
18947 {
18948 if (**arg == '(')
18949 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018950 /* need to copy the funcref so that we can clear rettv */
18951 functv = *rettv;
18952 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018953
18954 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018955 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018956 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018957 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18958 &len, evaluate, selfdict);
18959
18960 /* Clear the funcref afterwards, so that deleting it while
18961 * evaluating the arguments is possible (see test55). */
18962 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018963
18964 /* Stop the expression evaluation when immediately aborting on
18965 * error, or when an interrupt occurred or an exception was thrown
18966 * but not caught. */
18967 if (aborting())
18968 {
18969 if (ret == OK)
18970 clear_tv(rettv);
18971 ret = FAIL;
18972 }
18973 dict_unref(selfdict);
18974 selfdict = NULL;
18975 }
18976 else /* **arg == '[' || **arg == '.' */
18977 {
18978 dict_unref(selfdict);
18979 if (rettv->v_type == VAR_DICT)
18980 {
18981 selfdict = rettv->vval.v_dict;
18982 if (selfdict != NULL)
18983 ++selfdict->dv_refcount;
18984 }
18985 else
18986 selfdict = NULL;
18987 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18988 {
18989 clear_tv(rettv);
18990 ret = FAIL;
18991 }
18992 }
18993 }
18994 dict_unref(selfdict);
18995 return ret;
18996}
18997
18998/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018999 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019000 * value).
19001 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019002 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019004{
Bram Moolenaar33570922005-01-25 22:26:29 +000019005 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019006}
19007
19008/*
19009 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 * The string "s" must have been allocated, it is consumed.
19011 * Return NULL for out of memory, the variable otherwise.
19012 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019013 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019014alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 char_u *s;
19016{
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019019 rettv = alloc_tv();
19020 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019022 rettv->v_type = VAR_STRING;
19023 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 }
19025 else
19026 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019027 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028}
19029
19030/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019031 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019033 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019034free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019035 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036{
19037 if (varp != NULL)
19038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019039 switch (varp->v_type)
19040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019041 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019042 func_unref(varp->vval.v_string);
19043 /*FALLTHROUGH*/
19044 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019045 vim_free(varp->vval.v_string);
19046 break;
19047 case VAR_LIST:
19048 list_unref(varp->vval.v_list);
19049 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019050 case VAR_DICT:
19051 dict_unref(varp->vval.v_dict);
19052 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019053 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019054#ifdef FEAT_FLOAT
19055 case VAR_FLOAT:
19056#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019057 case VAR_UNKNOWN:
19058 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019059 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019060 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019061 break;
19062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 vim_free(varp);
19064 }
19065}
19066
19067/*
19068 * Free the memory for a variable value and set the value to NULL or 0.
19069 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019070 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073{
19074 if (varp != NULL)
19075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019076 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019078 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019079 func_unref(varp->vval.v_string);
19080 /*FALLTHROUGH*/
19081 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 vim_free(varp->vval.v_string);
19083 varp->vval.v_string = NULL;
19084 break;
19085 case VAR_LIST:
19086 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019087 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019088 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019089 case VAR_DICT:
19090 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019091 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019092 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019093 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019094 varp->vval.v_number = 0;
19095 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019096#ifdef FEAT_FLOAT
19097 case VAR_FLOAT:
19098 varp->vval.v_float = 0.0;
19099 break;
19100#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 case VAR_UNKNOWN:
19102 break;
19103 default:
19104 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019106 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 }
19108}
19109
19110/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019111 * Set the value of a variable to NULL without freeing items.
19112 */
19113 static void
19114init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019115 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019116{
19117 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019118 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019119}
19120
19121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 * Get the number value of a variable.
19123 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019124 * For incompatible types, return 0.
19125 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19126 * caller of incompatible types: it sets *denote to TRUE if "denote"
19127 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 */
19129 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019131 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019133 int error = FALSE;
19134
19135 return get_tv_number_chk(varp, &error); /* return 0L on error */
19136}
19137
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019138 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019139get_tv_number_chk(varp, denote)
19140 typval_T *varp;
19141 int *denote;
19142{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019145 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019147 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019148 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019149#ifdef FEAT_FLOAT
19150 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019151 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019152 break;
19153#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019154 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019155 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019156 break;
19157 case VAR_STRING:
19158 if (varp->vval.v_string != NULL)
19159 vim_str2nr(varp->vval.v_string, NULL, NULL,
19160 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019161 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019162 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019163 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019164 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019165 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019166 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019167 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019168 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019169 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019172 if (denote == NULL) /* useful for values that must be unsigned */
19173 n = -1;
19174 else
19175 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177}
19178
19179/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019180 * Get the lnum from the first argument.
19181 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019182 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 */
19184 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019186 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187{
Bram Moolenaar33570922005-01-25 22:26:29 +000019188 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 linenr_T lnum;
19190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019191 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 if (lnum == 0) /* no valid number, try using line() */
19193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194 rettv.v_type = VAR_NUMBER;
19195 f_line(argvars, &rettv);
19196 lnum = rettv.vval.v_number;
19197 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 }
19199 return lnum;
19200}
19201
19202/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019203 * Get the lnum from the first argument.
19204 * Also accepts "$", then "buf" is used.
19205 * Returns 0 on error.
19206 */
19207 static linenr_T
19208get_tv_lnum_buf(argvars, buf)
19209 typval_T *argvars;
19210 buf_T *buf;
19211{
19212 if (argvars[0].v_type == VAR_STRING
19213 && argvars[0].vval.v_string != NULL
19214 && argvars[0].vval.v_string[0] == '$'
19215 && buf != NULL)
19216 return buf->b_ml.ml_line_count;
19217 return get_tv_number_chk(&argvars[0], NULL);
19218}
19219
19220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 * Get the string value of a variable.
19222 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019223 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19224 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 * If the String variable has never been set, return an empty string.
19226 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019227 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19228 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 */
19230 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233{
19234 static char_u mybuf[NUMBUFLEN];
19235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237}
19238
19239 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019240get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019242 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019244 char_u *res = get_tv_string_buf_chk(varp, buf);
19245
19246 return res != NULL ? res : (char_u *)"";
19247}
19248
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019249 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019250get_tv_string_chk(varp)
19251 typval_T *varp;
19252{
19253 static char_u mybuf[NUMBUFLEN];
19254
19255 return get_tv_string_buf_chk(varp, mybuf);
19256}
19257
19258 static char_u *
19259get_tv_string_buf_chk(varp, buf)
19260 typval_T *varp;
19261 char_u *buf;
19262{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019263 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019265 case VAR_NUMBER:
19266 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19267 return buf;
19268 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019269 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 break;
19271 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019272 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019273 break;
19274 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019275 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019277#ifdef FEAT_FLOAT
19278 case VAR_FLOAT:
19279 EMSG(_("E806: using Float as a String"));
19280 break;
19281#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019282 case VAR_STRING:
19283 if (varp->vval.v_string != NULL)
19284 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019285 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019286 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019290 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291}
19292
19293/*
19294 * Find variable "name" in the list of variables.
19295 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019296 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019297 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019298 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019300 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019301find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019303 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019306 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307
Bram Moolenaara7043832005-01-21 11:56:39 +000019308 ht = find_var_ht(name, &varname);
19309 if (htp != NULL)
19310 *htp = ht;
19311 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019313 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314}
19315
19316/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019317 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019318 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019321find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019322 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019323 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019324 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019325{
Bram Moolenaar33570922005-01-25 22:26:29 +000019326 hashitem_T *hi;
19327
19328 if (*varname == NUL)
19329 {
19330 /* Must be something like "s:", otherwise "ht" would be NULL. */
19331 switch (varname[-2])
19332 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019333 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019334 case 'g': return &globvars_var;
19335 case 'v': return &vimvars_var;
19336 case 'b': return &curbuf->b_bufvar;
19337 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019338#ifdef FEAT_WINDOWS
19339 case 't': return &curtab->tp_winvar;
19340#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019341 case 'l': return current_funccal == NULL
19342 ? NULL : &current_funccal->l_vars_var;
19343 case 'a': return current_funccal == NULL
19344 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019345 }
19346 return NULL;
19347 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019348
19349 hi = hash_find(ht, varname);
19350 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019351 {
19352 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019353 * worked find the variable again. Don't auto-load a script if it was
19354 * loaded already, otherwise it would be loaded every time when
19355 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019356 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019357 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019358 hi = hash_find(ht, varname);
19359 if (HASHITEM_EMPTY(hi))
19360 return NULL;
19361 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019362 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019363}
19364
19365/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019366 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019367 * Set "varname" to the start of name without ':'.
19368 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019370find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371 char_u *name;
19372 char_u **varname;
19373{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019374 hashitem_T *hi;
19375
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 if (name[1] != ':')
19377 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019378 /* The name must not start with a colon or #. */
19379 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 return NULL;
19381 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019382
19383 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019384 hi = hash_find(&compat_hashtab, name);
19385 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019386 return &compat_hashtab;
19387
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 return &globvarht; /* global variable */
19390 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 }
19392 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019393 if (*name == 'g') /* global variable */
19394 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019395 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19396 */
19397 if (vim_strchr(name + 2, ':') != NULL
19398 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019399 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019401 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019404#ifdef FEAT_WINDOWS
19405 if (*name == 't') /* tab page variable */
19406 return &curtab->tp_vars.dv_hashtab;
19407#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019408 if (*name == 'v') /* v: variable */
19409 return &vimvarht;
19410 if (*name == 'a' && current_funccal != NULL) /* function argument */
19411 return &current_funccal->l_avars.dv_hashtab;
19412 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19413 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414 if (*name == 's' /* script variable */
19415 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19416 return &SCRIPT_VARS(current_SID);
19417 return NULL;
19418}
19419
19420/*
19421 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019422 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 * Returns NULL when it doesn't exist.
19424 */
19425 char_u *
19426get_var_value(name)
19427 char_u *name;
19428{
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430
Bram Moolenaara7043832005-01-21 11:56:39 +000019431 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 if (v == NULL)
19433 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019434 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435}
19436
19437/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019438 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 * sourcing this script and when executing functions defined in the script.
19440 */
19441 void
19442new_script_vars(id)
19443 scid_T id;
19444{
Bram Moolenaara7043832005-01-21 11:56:39 +000019445 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 hashtab_T *ht;
19447 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019448
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19450 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019451 /* Re-allocating ga_data means that an ht_array pointing to
19452 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019453 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019454 for (i = 1; i <= ga_scripts.ga_len; ++i)
19455 {
19456 ht = &SCRIPT_VARS(i);
19457 if (ht->ht_mask == HT_INIT_SIZE - 1)
19458 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019459 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019461 }
19462
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 while (ga_scripts.ga_len < id)
19464 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019465 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019466 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 }
19470 }
19471}
19472
19473/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019474 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19475 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 */
19477 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019478init_var_dict(dict, dict_var)
19479 dict_T *dict;
19480 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481{
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019483 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019484 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 dict_var->di_tv.vval.v_dict = dict;
19486 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019487 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019488 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19489 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490}
19491
19492/*
19493 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019494 * Frees all allocated variables and the value they contain.
19495 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 */
19497 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019498vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019499 hashtab_T *ht;
19500{
19501 vars_clear_ext(ht, TRUE);
19502}
19503
19504/*
19505 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19506 */
19507 static void
19508vars_clear_ext(ht, free_val)
19509 hashtab_T *ht;
19510 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511{
Bram Moolenaara7043832005-01-21 11:56:39 +000019512 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019513 hashitem_T *hi;
19514 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515
Bram Moolenaar33570922005-01-25 22:26:29 +000019516 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019517 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 for (hi = ht->ht_array; todo > 0; ++hi)
19519 {
19520 if (!HASHITEM_EMPTY(hi))
19521 {
19522 --todo;
19523
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019525 * ht_array might change then. hash_clear() takes care of it
19526 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019527 v = HI2DI(hi);
19528 if (free_val)
19529 clear_tv(&v->di_tv);
19530 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19531 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019532 }
19533 }
19534 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019535 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536}
19537
Bram Moolenaara7043832005-01-21 11:56:39 +000019538/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019539 * Delete a variable from hashtab "ht" at item "hi".
19540 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019543delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019544 hashtab_T *ht;
19545 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546{
Bram Moolenaar33570922005-01-25 22:26:29 +000019547 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019548
19549 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019550 clear_tv(&di->di_tv);
19551 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552}
19553
19554/*
19555 * List the value of one internal variable.
19556 */
19557 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019558list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019561 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019563 char_u *tofree;
19564 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019566
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019567 current_copyID += COPYID_INC;
19568 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019569 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019570 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572}
19573
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019575list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 char_u *prefix;
19577 char_u *name;
19578 int type;
19579 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019580 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581{
Bram Moolenaar31859182007-08-14 20:41:13 +000019582 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19583 msg_start();
19584 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 if (name != NULL) /* "a:" vars don't have a name stored */
19586 msg_puts(name);
19587 msg_putchar(' ');
19588 msg_advance(22);
19589 if (type == VAR_NUMBER)
19590 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 else if (type == VAR_FUNC)
19592 msg_putchar('*');
19593 else if (type == VAR_LIST)
19594 {
19595 msg_putchar('[');
19596 if (*string == '[')
19597 ++string;
19598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019599 else if (type == VAR_DICT)
19600 {
19601 msg_putchar('{');
19602 if (*string == '{')
19603 ++string;
19604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 else
19606 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019607
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019609
19610 if (type == VAR_FUNC)
19611 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019612 if (*first)
19613 {
19614 msg_clr_eos();
19615 *first = FALSE;
19616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617}
19618
19619/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 * If the variable already exists, the value is updated.
19622 * Otherwise the variable is created.
19623 */
19624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019625set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629{
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019632 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019633 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019635 ht = find_var_ht(name, &varname);
19636 if (ht == NULL || *varname == NUL)
19637 {
19638 EMSG2(_(e_illvar), name);
19639 return;
19640 }
19641 v = find_var_in_ht(ht, varname, TRUE);
19642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019644 {
19645 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19646 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19647 ? name[2] : name[0]))
19648 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019649 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019650 return;
19651 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019652 /* Don't allow hiding a function. When "v" is not NULL we migth be
19653 * assigning another function to the same var, the type is checked
19654 * below. */
19655 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019656 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019657 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019658 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019659 return;
19660 }
19661 }
19662
Bram Moolenaar33570922005-01-25 22:26:29 +000019663 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019665 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019666 if (var_check_ro(v->di_flags, name)
19667 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 return;
19669 if (v->di_tv.v_type != tv->v_type
19670 && !((v->di_tv.v_type == VAR_STRING
19671 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019673 || tv->v_type == VAR_NUMBER))
19674#ifdef FEAT_FLOAT
19675 && !((v->di_tv.v_type == VAR_NUMBER
19676 || v->di_tv.v_type == VAR_FLOAT)
19677 && (tv->v_type == VAR_NUMBER
19678 || tv->v_type == VAR_FLOAT))
19679#endif
19680 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019681 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019682 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019683 return;
19684 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019685
19686 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019687 * Handle setting internal v: variables separately: we don't change
19688 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019689 */
19690 if (ht == &vimvarht)
19691 {
19692 if (v->di_tv.v_type == VAR_STRING)
19693 {
19694 vim_free(v->di_tv.vval.v_string);
19695 if (copy || tv->v_type != VAR_STRING)
19696 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19697 else
19698 {
19699 /* Take over the string to avoid an extra alloc/free. */
19700 v->di_tv.vval.v_string = tv->vval.v_string;
19701 tv->vval.v_string = NULL;
19702 }
19703 }
19704 else if (v->di_tv.v_type != VAR_NUMBER)
19705 EMSG2(_(e_intern2), "set_var()");
19706 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019707 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019709 if (STRCMP(varname, "searchforward") == 0)
19710 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19711 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 return;
19713 }
19714
19715 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 }
19717 else /* add a new variable */
19718 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019719 /* Can't add "v:" variable. */
19720 if (ht == &vimvarht)
19721 {
19722 EMSG2(_(e_illvar), name);
19723 return;
19724 }
19725
Bram Moolenaar92124a32005-06-17 22:03:40 +000019726 /* Make sure the variable name is valid. */
19727 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019728 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19729 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019730 {
19731 EMSG2(_(e_illvar), varname);
19732 return;
19733 }
19734
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019735 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19736 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019737 if (v == NULL)
19738 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019740 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019742 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 return;
19744 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019745 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019747
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019748 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019749 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019750 else
19751 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019753 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756}
19757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019759 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019760 * Also give an error message.
19761 */
19762 static int
19763var_check_ro(flags, name)
19764 int flags;
19765 char_u *name;
19766{
19767 if (flags & DI_FLAGS_RO)
19768 {
19769 EMSG2(_(e_readonlyvar), name);
19770 return TRUE;
19771 }
19772 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19773 {
19774 EMSG2(_(e_readonlysbx), name);
19775 return TRUE;
19776 }
19777 return FALSE;
19778}
19779
19780/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019781 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19782 * Also give an error message.
19783 */
19784 static int
19785var_check_fixed(flags, name)
19786 int flags;
19787 char_u *name;
19788{
19789 if (flags & DI_FLAGS_FIX)
19790 {
19791 EMSG2(_("E795: Cannot delete variable %s"), name);
19792 return TRUE;
19793 }
19794 return FALSE;
19795}
19796
19797/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019798 * Return TRUE if typeval "tv" is set to be locked (immutable).
19799 * Also give an error message, using "name".
19800 */
19801 static int
19802tv_check_lock(lock, name)
19803 int lock;
19804 char_u *name;
19805{
19806 if (lock & VAR_LOCKED)
19807 {
19808 EMSG2(_("E741: Value is locked: %s"),
19809 name == NULL ? (char_u *)_("Unknown") : name);
19810 return TRUE;
19811 }
19812 if (lock & VAR_FIXED)
19813 {
19814 EMSG2(_("E742: Cannot change value of %s"),
19815 name == NULL ? (char_u *)_("Unknown") : name);
19816 return TRUE;
19817 }
19818 return FALSE;
19819}
19820
19821/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019823 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019824 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019825 * It is OK for "from" and "to" to point to the same item. This is used to
19826 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019827 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019828 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019829copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019830 typval_T *from;
19831 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019833 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019834 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019835 switch (from->v_type)
19836 {
19837 case VAR_NUMBER:
19838 to->vval.v_number = from->vval.v_number;
19839 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019840#ifdef FEAT_FLOAT
19841 case VAR_FLOAT:
19842 to->vval.v_float = from->vval.v_float;
19843 break;
19844#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019845 case VAR_STRING:
19846 case VAR_FUNC:
19847 if (from->vval.v_string == NULL)
19848 to->vval.v_string = NULL;
19849 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019850 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019851 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019852 if (from->v_type == VAR_FUNC)
19853 func_ref(to->vval.v_string);
19854 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019855 break;
19856 case VAR_LIST:
19857 if (from->vval.v_list == NULL)
19858 to->vval.v_list = NULL;
19859 else
19860 {
19861 to->vval.v_list = from->vval.v_list;
19862 ++to->vval.v_list->lv_refcount;
19863 }
19864 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019865 case VAR_DICT:
19866 if (from->vval.v_dict == NULL)
19867 to->vval.v_dict = NULL;
19868 else
19869 {
19870 to->vval.v_dict = from->vval.v_dict;
19871 ++to->vval.v_dict->dv_refcount;
19872 }
19873 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019874 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 break;
19877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878}
19879
19880/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019881 * Make a copy of an item.
19882 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019883 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19884 * reference to an already copied list/dict can be used.
19885 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019886 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019887 static int
19888item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019889 typval_T *from;
19890 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019891 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019892 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019893{
19894 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019895 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019896
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019898 {
19899 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019900 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019901 }
19902 ++recurse;
19903
19904 switch (from->v_type)
19905 {
19906 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019907#ifdef FEAT_FLOAT
19908 case VAR_FLOAT:
19909#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019910 case VAR_STRING:
19911 case VAR_FUNC:
19912 copy_tv(from, to);
19913 break;
19914 case VAR_LIST:
19915 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019916 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019917 if (from->vval.v_list == NULL)
19918 to->vval.v_list = NULL;
19919 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19920 {
19921 /* use the copy made earlier */
19922 to->vval.v_list = from->vval.v_list->lv_copylist;
19923 ++to->vval.v_list->lv_refcount;
19924 }
19925 else
19926 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19927 if (to->vval.v_list == NULL)
19928 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019929 break;
19930 case VAR_DICT:
19931 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019932 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019933 if (from->vval.v_dict == NULL)
19934 to->vval.v_dict = NULL;
19935 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19936 {
19937 /* use the copy made earlier */
19938 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19939 ++to->vval.v_dict->dv_refcount;
19940 }
19941 else
19942 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19943 if (to->vval.v_dict == NULL)
19944 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019945 break;
19946 default:
19947 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019948 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019949 }
19950 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019951 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019952}
19953
19954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 * ":echo expr1 ..." print each argument separated with a space, add a
19956 * newline at the end.
19957 * ":echon expr1 ..." print each argument plain.
19958 */
19959 void
19960ex_echo(eap)
19961 exarg_T *eap;
19962{
19963 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019964 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019965 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 char_u *p;
19967 int needclr = TRUE;
19968 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019969 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970
19971 if (eap->skip)
19972 ++emsg_skip;
19973 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19974 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019975 /* If eval1() causes an error message the text from the command may
19976 * still need to be cleared. E.g., "echo 22,44". */
19977 need_clr_eos = needclr;
19978
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019980 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 {
19982 /*
19983 * Report the invalid expression unless the expression evaluation
19984 * has been cancelled due to an aborting error, an interrupt, or an
19985 * exception.
19986 */
19987 if (!aborting())
19988 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019989 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 break;
19991 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019992 need_clr_eos = FALSE;
19993
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 if (!eap->skip)
19995 {
19996 if (atstart)
19997 {
19998 atstart = FALSE;
19999 /* Call msg_start() after eval1(), evaluating the expression
20000 * may cause a message to appear. */
20001 if (eap->cmdidx == CMD_echo)
20002 msg_start();
20003 }
20004 else if (eap->cmdidx == CMD_echo)
20005 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020006 current_copyID += COPYID_INC;
20007 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020008 if (p != NULL)
20009 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020011 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020013 if (*p != TAB && needclr)
20014 {
20015 /* remove any text still there from the command */
20016 msg_clr_eos();
20017 needclr = FALSE;
20018 }
20019 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
20021 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020022 {
20023#ifdef FEAT_MBYTE
20024 if (has_mbyte)
20025 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020026 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020027
20028 (void)msg_outtrans_len_attr(p, i, echo_attr);
20029 p += i - 1;
20030 }
20031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020033 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020036 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 arg = skipwhite(arg);
20040 }
20041 eap->nextcmd = check_nextcmd(arg);
20042
20043 if (eap->skip)
20044 --emsg_skip;
20045 else
20046 {
20047 /* remove text that may still be there from the command */
20048 if (needclr)
20049 msg_clr_eos();
20050 if (eap->cmdidx == CMD_echo)
20051 msg_end();
20052 }
20053}
20054
20055/*
20056 * ":echohl {name}".
20057 */
20058 void
20059ex_echohl(eap)
20060 exarg_T *eap;
20061{
20062 int id;
20063
20064 id = syn_name2id(eap->arg);
20065 if (id == 0)
20066 echo_attr = 0;
20067 else
20068 echo_attr = syn_id2attr(id);
20069}
20070
20071/*
20072 * ":execute expr1 ..." execute the result of an expression.
20073 * ":echomsg expr1 ..." Print a message
20074 * ":echoerr expr1 ..." Print an error
20075 * Each gets spaces around each argument and a newline at the end for
20076 * echo commands
20077 */
20078 void
20079ex_execute(eap)
20080 exarg_T *eap;
20081{
20082 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 int ret = OK;
20085 char_u *p;
20086 garray_T ga;
20087 int len;
20088 int save_did_emsg;
20089
20090 ga_init2(&ga, 1, 80);
20091
20092 if (eap->skip)
20093 ++emsg_skip;
20094 while (*arg != NUL && *arg != '|' && *arg != '\n')
20095 {
20096 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 {
20099 /*
20100 * Report the invalid expression unless the expression evaluation
20101 * has been cancelled due to an aborting error, an interrupt, or an
20102 * exception.
20103 */
20104 if (!aborting())
20105 EMSG2(_(e_invexpr2), p);
20106 ret = FAIL;
20107 break;
20108 }
20109
20110 if (!eap->skip)
20111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 len = (int)STRLEN(p);
20114 if (ga_grow(&ga, len + 2) == FAIL)
20115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020116 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 ret = FAIL;
20118 break;
20119 }
20120 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 ga.ga_len += len;
20124 }
20125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 arg = skipwhite(arg);
20128 }
20129
20130 if (ret != FAIL && ga.ga_data != NULL)
20131 {
20132 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020135 out_flush();
20136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 else if (eap->cmdidx == CMD_echoerr)
20138 {
20139 /* We don't want to abort following commands, restore did_emsg. */
20140 save_did_emsg = did_emsg;
20141 EMSG((char_u *)ga.ga_data);
20142 if (!force_abort)
20143 did_emsg = save_did_emsg;
20144 }
20145 else if (eap->cmdidx == CMD_execute)
20146 do_cmdline((char_u *)ga.ga_data,
20147 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20148 }
20149
20150 ga_clear(&ga);
20151
20152 if (eap->skip)
20153 --emsg_skip;
20154
20155 eap->nextcmd = check_nextcmd(arg);
20156}
20157
20158/*
20159 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20160 * "arg" points to the "&" or '+' when called, to "option" when returning.
20161 * Returns NULL when no option name found. Otherwise pointer to the char
20162 * after the option name.
20163 */
20164 static char_u *
20165find_option_end(arg, opt_flags)
20166 char_u **arg;
20167 int *opt_flags;
20168{
20169 char_u *p = *arg;
20170
20171 ++p;
20172 if (*p == 'g' && p[1] == ':')
20173 {
20174 *opt_flags = OPT_GLOBAL;
20175 p += 2;
20176 }
20177 else if (*p == 'l' && p[1] == ':')
20178 {
20179 *opt_flags = OPT_LOCAL;
20180 p += 2;
20181 }
20182 else
20183 *opt_flags = 0;
20184
20185 if (!ASCII_ISALPHA(*p))
20186 return NULL;
20187 *arg = p;
20188
20189 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20190 p += 4; /* termcap option */
20191 else
20192 while (ASCII_ISALPHA(*p))
20193 ++p;
20194 return p;
20195}
20196
20197/*
20198 * ":function"
20199 */
20200 void
20201ex_function(eap)
20202 exarg_T *eap;
20203{
20204 char_u *theline;
20205 int j;
20206 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 char_u *name = NULL;
20209 char_u *p;
20210 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020211 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 garray_T newargs;
20213 garray_T newlines;
20214 int varargs = FALSE;
20215 int mustend = FALSE;
20216 int flags = 0;
20217 ufunc_T *fp;
20218 int indent;
20219 int nesting;
20220 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 dictitem_T *v;
20222 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020223 static int func_nr = 0; /* number for nameless function */
20224 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020225 hashtab_T *ht;
20226 int todo;
20227 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020228 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229
20230 /*
20231 * ":function" without argument: list functions.
20232 */
20233 if (ends_excmd(*eap->arg))
20234 {
20235 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020236 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020237 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020238 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020239 {
20240 if (!HASHITEM_EMPTY(hi))
20241 {
20242 --todo;
20243 fp = HI2UF(hi);
20244 if (!isdigit(*fp->uf_name))
20245 list_func_head(fp, FALSE);
20246 }
20247 }
20248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 eap->nextcmd = check_nextcmd(eap->arg);
20250 return;
20251 }
20252
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020253 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020254 * ":function /pat": list functions matching pattern.
20255 */
20256 if (*eap->arg == '/')
20257 {
20258 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20259 if (!eap->skip)
20260 {
20261 regmatch_T regmatch;
20262
20263 c = *p;
20264 *p = NUL;
20265 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20266 *p = c;
20267 if (regmatch.regprog != NULL)
20268 {
20269 regmatch.rm_ic = p_ic;
20270
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020271 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020272 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20273 {
20274 if (!HASHITEM_EMPTY(hi))
20275 {
20276 --todo;
20277 fp = HI2UF(hi);
20278 if (!isdigit(*fp->uf_name)
20279 && vim_regexec(&regmatch, fp->uf_name, 0))
20280 list_func_head(fp, FALSE);
20281 }
20282 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020283 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020284 }
20285 }
20286 if (*p == '/')
20287 ++p;
20288 eap->nextcmd = check_nextcmd(p);
20289 return;
20290 }
20291
20292 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020293 * Get the function name. There are these situations:
20294 * func normal function name
20295 * "name" == func, "fudi.fd_dict" == NULL
20296 * dict.func new dictionary entry
20297 * "name" == NULL, "fudi.fd_dict" set,
20298 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20299 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020300 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020301 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20302 * dict.func existing dict entry that's not a Funcref
20303 * "name" == NULL, "fudi.fd_dict" set,
20304 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020307 name = trans_function_name(&p, eap->skip, 0, &fudi);
20308 paren = (vim_strchr(p, '(') != NULL);
20309 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 {
20311 /*
20312 * Return on an invalid expression in braces, unless the expression
20313 * evaluation has been cancelled due to an aborting error, an
20314 * interrupt, or an exception.
20315 */
20316 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020318 if (!eap->skip && fudi.fd_newkey != NULL)
20319 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020320 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 else
20324 eap->skip = TRUE;
20325 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020326
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 /* An error in a function call during evaluation of an expression in magic
20328 * braces should not cause the function not to be defined. */
20329 saved_did_emsg = did_emsg;
20330 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331
20332 /*
20333 * ":function func" with only function name: list function.
20334 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020335 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 {
20337 if (!ends_excmd(*skipwhite(p)))
20338 {
20339 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020340 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 }
20342 eap->nextcmd = check_nextcmd(p);
20343 if (eap->nextcmd != NULL)
20344 *p = NUL;
20345 if (!eap->skip && !got_int)
20346 {
20347 fp = find_func(name);
20348 if (fp != NULL)
20349 {
20350 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020351 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020353 if (FUNCLINE(fp, j) == NULL)
20354 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 msg_putchar('\n');
20356 msg_outnum((long)(j + 1));
20357 if (j < 9)
20358 msg_putchar(' ');
20359 if (j < 99)
20360 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020361 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 out_flush(); /* show a line at a time */
20363 ui_breakcheck();
20364 }
20365 if (!got_int)
20366 {
20367 msg_putchar('\n');
20368 msg_puts((char_u *)" endfunction");
20369 }
20370 }
20371 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020372 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020374 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 }
20376
20377 /*
20378 * ":function name(arg1, arg2)" Define function.
20379 */
20380 p = skipwhite(p);
20381 if (*p != '(')
20382 {
20383 if (!eap->skip)
20384 {
20385 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020386 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 }
20388 /* attempt to continue by skipping some text */
20389 if (vim_strchr(p, '(') != NULL)
20390 p = vim_strchr(p, '(');
20391 }
20392 p = skipwhite(p + 1);
20393
20394 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20395 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20396
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020397 if (!eap->skip)
20398 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020399 /* Check the name of the function. Unless it's a dictionary function
20400 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020401 if (name != NULL)
20402 arg = name;
20403 else
20404 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020405 if (arg != NULL && (fudi.fd_di == NULL
20406 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020407 {
20408 if (*arg == K_SPECIAL)
20409 j = 3;
20410 else
20411 j = 0;
20412 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20413 : eval_isnamec(arg[j])))
20414 ++j;
20415 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020416 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020417 }
20418 }
20419
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 /*
20421 * Isolate the arguments: "arg1, arg2, ...)"
20422 */
20423 while (*p != ')')
20424 {
20425 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20426 {
20427 varargs = TRUE;
20428 p += 3;
20429 mustend = TRUE;
20430 }
20431 else
20432 {
20433 arg = p;
20434 while (ASCII_ISALNUM(*p) || *p == '_')
20435 ++p;
20436 if (arg == p || isdigit(*arg)
20437 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20438 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20439 {
20440 if (!eap->skip)
20441 EMSG2(_("E125: Illegal argument: %s"), arg);
20442 break;
20443 }
20444 if (ga_grow(&newargs, 1) == FAIL)
20445 goto erret;
20446 c = *p;
20447 *p = NUL;
20448 arg = vim_strsave(arg);
20449 if (arg == NULL)
20450 goto erret;
20451 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20452 *p = c;
20453 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 if (*p == ',')
20455 ++p;
20456 else
20457 mustend = TRUE;
20458 }
20459 p = skipwhite(p);
20460 if (mustend && *p != ')')
20461 {
20462 if (!eap->skip)
20463 EMSG2(_(e_invarg2), eap->arg);
20464 break;
20465 }
20466 }
20467 ++p; /* skip the ')' */
20468
Bram Moolenaare9a41262005-01-15 22:18:47 +000020469 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 for (;;)
20471 {
20472 p = skipwhite(p);
20473 if (STRNCMP(p, "range", 5) == 0)
20474 {
20475 flags |= FC_RANGE;
20476 p += 5;
20477 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020478 else if (STRNCMP(p, "dict", 4) == 0)
20479 {
20480 flags |= FC_DICT;
20481 p += 4;
20482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 else if (STRNCMP(p, "abort", 5) == 0)
20484 {
20485 flags |= FC_ABORT;
20486 p += 5;
20487 }
20488 else
20489 break;
20490 }
20491
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020492 /* When there is a line break use what follows for the function body.
20493 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20494 if (*p == '\n')
20495 line_arg = p + 1;
20496 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 EMSG(_(e_trailing));
20498
20499 /*
20500 * Read the body of the function, until ":endfunction" is found.
20501 */
20502 if (KeyTyped)
20503 {
20504 /* Check if the function already exists, don't let the user type the
20505 * whole function before telling him it doesn't work! For a script we
20506 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020507 if (!eap->skip && !eap->forceit)
20508 {
20509 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20510 EMSG(_(e_funcdict));
20511 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020512 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020515 if (!eap->skip && did_emsg)
20516 goto erret;
20517
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 msg_putchar('\n'); /* don't overwrite the function name */
20519 cmdline_row = msg_row;
20520 }
20521
20522 indent = 2;
20523 nesting = 0;
20524 for (;;)
20525 {
20526 msg_scroll = TRUE;
20527 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020528 sourcing_lnum_off = sourcing_lnum;
20529
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020530 if (line_arg != NULL)
20531 {
20532 /* Use eap->arg, split up in parts by line breaks. */
20533 theline = line_arg;
20534 p = vim_strchr(theline, '\n');
20535 if (p == NULL)
20536 line_arg += STRLEN(line_arg);
20537 else
20538 {
20539 *p = NUL;
20540 line_arg = p + 1;
20541 }
20542 }
20543 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 theline = getcmdline(':', 0L, indent);
20545 else
20546 theline = eap->getline(':', eap->cookie, indent);
20547 if (KeyTyped)
20548 lines_left = Rows - 1;
20549 if (theline == NULL)
20550 {
20551 EMSG(_("E126: Missing :endfunction"));
20552 goto erret;
20553 }
20554
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020555 /* Detect line continuation: sourcing_lnum increased more than one. */
20556 if (sourcing_lnum > sourcing_lnum_off + 1)
20557 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20558 else
20559 sourcing_lnum_off = 0;
20560
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 if (skip_until != NULL)
20562 {
20563 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20564 * don't check for ":endfunc". */
20565 if (STRCMP(theline, skip_until) == 0)
20566 {
20567 vim_free(skip_until);
20568 skip_until = NULL;
20569 }
20570 }
20571 else
20572 {
20573 /* skip ':' and blanks*/
20574 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20575 ;
20576
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020577 /* Check for "endfunction". */
20578 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020580 if (line_arg == NULL)
20581 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 break;
20583 }
20584
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020585 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 * at "end". */
20587 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20588 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020589 else if (STRNCMP(p, "if", 2) == 0
20590 || STRNCMP(p, "wh", 2) == 0
20591 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 || STRNCMP(p, "try", 3) == 0)
20593 indent += 2;
20594
20595 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020596 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020598 if (*p == '!')
20599 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 p += eval_fname_script(p);
20601 if (ASCII_ISALPHA(*p))
20602 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020603 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 if (*skipwhite(p) == '(')
20605 {
20606 ++nesting;
20607 indent += 2;
20608 }
20609 }
20610 }
20611
20612 /* Check for ":append" or ":insert". */
20613 p = skip_range(p, NULL);
20614 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20615 || (p[0] == 'i'
20616 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20617 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20618 skip_until = vim_strsave((char_u *)".");
20619
20620 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20621 arg = skipwhite(skiptowhite(p));
20622 if (arg[0] == '<' && arg[1] =='<'
20623 && ((p[0] == 'p' && p[1] == 'y'
20624 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20625 || (p[0] == 'p' && p[1] == 'e'
20626 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20627 || (p[0] == 't' && p[1] == 'c'
20628 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20629 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20630 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020631 || (p[0] == 'm' && p[1] == 'z'
20632 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 ))
20634 {
20635 /* ":python <<" continues until a dot, like ":append" */
20636 p = skipwhite(arg + 2);
20637 if (*p == NUL)
20638 skip_until = vim_strsave((char_u *)".");
20639 else
20640 skip_until = vim_strsave(p);
20641 }
20642 }
20643
20644 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020645 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020646 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020647 if (line_arg == NULL)
20648 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020650 }
20651
20652 /* Copy the line to newly allocated memory. get_one_sourceline()
20653 * allocates 250 bytes per line, this saves 80% on average. The cost
20654 * is an extra alloc/free. */
20655 p = vim_strsave(theline);
20656 if (p != NULL)
20657 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020658 if (line_arg == NULL)
20659 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020660 theline = p;
20661 }
20662
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020663 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20664
20665 /* Add NULL lines for continuation lines, so that the line count is
20666 * equal to the index in the growarray. */
20667 while (sourcing_lnum_off-- > 0)
20668 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020669
20670 /* Check for end of eap->arg. */
20671 if (line_arg != NULL && *line_arg == NUL)
20672 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 }
20674
20675 /* Don't define the function when skipping commands or when an error was
20676 * detected. */
20677 if (eap->skip || did_emsg)
20678 goto erret;
20679
20680 /*
20681 * If there are no errors, add the function
20682 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020683 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020684 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020685 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020686 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020687 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020688 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020689 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020690 goto erret;
20691 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020692
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020693 fp = find_func(name);
20694 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020696 if (!eap->forceit)
20697 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020698 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020699 goto erret;
20700 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020701 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020702 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020703 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020704 name);
20705 goto erret;
20706 }
20707 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020708 ga_clear_strings(&(fp->uf_args));
20709 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020710 vim_free(name);
20711 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 }
20714 else
20715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020716 char numbuf[20];
20717
20718 fp = NULL;
20719 if (fudi.fd_newkey == NULL && !eap->forceit)
20720 {
20721 EMSG(_(e_funcdict));
20722 goto erret;
20723 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020724 if (fudi.fd_di == NULL)
20725 {
20726 /* Can't add a function to a locked dictionary */
20727 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20728 goto erret;
20729 }
20730 /* Can't change an existing function if it is locked */
20731 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20732 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020733
20734 /* Give the function a sequential number. Can only be used with a
20735 * Funcref! */
20736 vim_free(name);
20737 sprintf(numbuf, "%d", ++func_nr);
20738 name = vim_strsave((char_u *)numbuf);
20739 if (name == NULL)
20740 goto erret;
20741 }
20742
20743 if (fp == NULL)
20744 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020745 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020746 {
20747 int slen, plen;
20748 char_u *scriptname;
20749
20750 /* Check that the autoload name matches the script name. */
20751 j = FAIL;
20752 if (sourcing_name != NULL)
20753 {
20754 scriptname = autoload_name(name);
20755 if (scriptname != NULL)
20756 {
20757 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020758 plen = (int)STRLEN(p);
20759 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020760 if (slen > plen && fnamecmp(p,
20761 sourcing_name + slen - plen) == 0)
20762 j = OK;
20763 vim_free(scriptname);
20764 }
20765 }
20766 if (j == FAIL)
20767 {
20768 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20769 goto erret;
20770 }
20771 }
20772
20773 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 if (fp == NULL)
20775 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020776
20777 if (fudi.fd_dict != NULL)
20778 {
20779 if (fudi.fd_di == NULL)
20780 {
20781 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020782 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020783 if (fudi.fd_di == NULL)
20784 {
20785 vim_free(fp);
20786 goto erret;
20787 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020788 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20789 {
20790 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020791 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020792 goto erret;
20793 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020794 }
20795 else
20796 /* overwrite existing dict entry */
20797 clear_tv(&fudi.fd_di->di_tv);
20798 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020799 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020800 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020801 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020802
20803 /* behave like "dict" was used */
20804 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020805 }
20806
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020808 STRCPY(fp->uf_name, name);
20809 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020811 fp->uf_args = newargs;
20812 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020813#ifdef FEAT_PROFILE
20814 fp->uf_tml_count = NULL;
20815 fp->uf_tml_total = NULL;
20816 fp->uf_tml_self = NULL;
20817 fp->uf_profiling = FALSE;
20818 if (prof_def_func())
20819 func_do_profile(fp);
20820#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020821 fp->uf_varargs = varargs;
20822 fp->uf_flags = flags;
20823 fp->uf_calls = 0;
20824 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020825 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826
20827erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 ga_clear_strings(&newargs);
20829 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020830ret_free:
20831 vim_free(skip_until);
20832 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835}
20836
20837/*
20838 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020839 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020841 * flags:
20842 * TFN_INT: internal function name OK
20843 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 * Advances "pp" to just after the function name (if no error).
20845 */
20846 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020847trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 char_u **pp;
20849 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020850 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020851 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020853 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 char_u *start;
20855 char_u *end;
20856 int lead;
20857 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020859 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020860
20861 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020862 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020864
20865 /* Check for hard coded <SNR>: already translated function ID (from a user
20866 * command). */
20867 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20868 && (*pp)[2] == (int)KE_SNR)
20869 {
20870 *pp += 3;
20871 len = get_id_len(pp) + 3;
20872 return vim_strnsave(start, len);
20873 }
20874
20875 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20876 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020878 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020880
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020881 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20882 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020883 if (end == start)
20884 {
20885 if (!skip)
20886 EMSG(_("E129: Function name required"));
20887 goto theend;
20888 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020889 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020890 {
20891 /*
20892 * Report an invalid expression in braces, unless the expression
20893 * evaluation has been cancelled due to an aborting error, an
20894 * interrupt, or an exception.
20895 */
20896 if (!aborting())
20897 {
20898 if (end != NULL)
20899 EMSG2(_(e_invarg2), start);
20900 }
20901 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020902 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020903 goto theend;
20904 }
20905
20906 if (lv.ll_tv != NULL)
20907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020908 if (fdp != NULL)
20909 {
20910 fdp->fd_dict = lv.ll_dict;
20911 fdp->fd_newkey = lv.ll_newkey;
20912 lv.ll_newkey = NULL;
20913 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020915 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20916 {
20917 name = vim_strsave(lv.ll_tv->vval.v_string);
20918 *pp = end;
20919 }
20920 else
20921 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020922 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20923 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020924 EMSG(_(e_funcref));
20925 else
20926 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020927 name = NULL;
20928 }
20929 goto theend;
20930 }
20931
20932 if (lv.ll_name == NULL)
20933 {
20934 /* Error found, but continue after the function name. */
20935 *pp = end;
20936 goto theend;
20937 }
20938
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020939 /* Check if the name is a Funcref. If so, use the value. */
20940 if (lv.ll_exp_name != NULL)
20941 {
20942 len = (int)STRLEN(lv.ll_exp_name);
20943 name = deref_func_name(lv.ll_exp_name, &len);
20944 if (name == lv.ll_exp_name)
20945 name = NULL;
20946 }
20947 else
20948 {
20949 len = (int)(end - *pp);
20950 name = deref_func_name(*pp, &len);
20951 if (name == *pp)
20952 name = NULL;
20953 }
20954 if (name != NULL)
20955 {
20956 name = vim_strsave(name);
20957 *pp = end;
20958 goto theend;
20959 }
20960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020961 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020962 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020963 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020964 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20965 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20966 {
20967 /* When there was "s:" already or the name expanded to get a
20968 * leading "s:" then remove it. */
20969 lv.ll_name += 2;
20970 len -= 2;
20971 lead = 2;
20972 }
20973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020974 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020975 {
20976 if (lead == 2) /* skip over "s:" */
20977 lv.ll_name += 2;
20978 len = (int)(end - lv.ll_name);
20979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020980
20981 /*
20982 * Copy the function name to allocated memory.
20983 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20984 * Accept <SNR>123_name() outside a script.
20985 */
20986 if (skip)
20987 lead = 0; /* do nothing */
20988 else if (lead > 0)
20989 {
20990 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020991 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20992 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020993 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020994 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020995 if (current_SID <= 0)
20996 {
20997 EMSG(_(e_usingsid));
20998 goto theend;
20999 }
21000 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21001 lead += (int)STRLEN(sid_buf);
21002 }
21003 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021004 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021005 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021006 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021007 goto theend;
21008 }
21009 name = alloc((unsigned)(len + lead + 1));
21010 if (name != NULL)
21011 {
21012 if (lead > 0)
21013 {
21014 name[0] = K_SPECIAL;
21015 name[1] = KS_EXTRA;
21016 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021017 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021018 STRCPY(name + 3, sid_buf);
21019 }
21020 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21021 name[len + lead] = NUL;
21022 }
21023 *pp = end;
21024
21025theend:
21026 clear_lval(&lv);
21027 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028}
21029
21030/*
21031 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21032 * Return 2 if "p" starts with "s:".
21033 * Return 0 otherwise.
21034 */
21035 static int
21036eval_fname_script(p)
21037 char_u *p;
21038{
21039 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21040 || STRNICMP(p + 1, "SNR>", 4) == 0))
21041 return 5;
21042 if (p[0] == 's' && p[1] == ':')
21043 return 2;
21044 return 0;
21045}
21046
21047/*
21048 * Return TRUE if "p" starts with "<SID>" or "s:".
21049 * Only works if eval_fname_script() returned non-zero for "p"!
21050 */
21051 static int
21052eval_fname_sid(p)
21053 char_u *p;
21054{
21055 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21056}
21057
21058/*
21059 * List the head of the function: "name(arg1, arg2)".
21060 */
21061 static void
21062list_func_head(fp, indent)
21063 ufunc_T *fp;
21064 int indent;
21065{
21066 int j;
21067
21068 msg_start();
21069 if (indent)
21070 MSG_PUTS(" ");
21071 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021072 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 {
21074 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021075 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 }
21077 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021078 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021080 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 {
21082 if (j)
21083 MSG_PUTS(", ");
21084 msg_puts(FUNCARG(fp, j));
21085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021086 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 {
21088 if (j)
21089 MSG_PUTS(", ");
21090 MSG_PUTS("...");
21091 }
21092 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021093 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021094 if (p_verbose > 0)
21095 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096}
21097
21098/*
21099 * Find a function by name, return pointer to it in ufuncs.
21100 * Return NULL for unknown function.
21101 */
21102 static ufunc_T *
21103find_func(name)
21104 char_u *name;
21105{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021106 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021108 hi = hash_find(&func_hashtab, name);
21109 if (!HASHITEM_EMPTY(hi))
21110 return HI2UF(hi);
21111 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112}
21113
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021114#if defined(EXITFREE) || defined(PROTO)
21115 void
21116free_all_functions()
21117{
21118 hashitem_T *hi;
21119
21120 /* Need to start all over every time, because func_free() may change the
21121 * hash table. */
21122 while (func_hashtab.ht_used > 0)
21123 for (hi = func_hashtab.ht_array; ; ++hi)
21124 if (!HASHITEM_EMPTY(hi))
21125 {
21126 func_free(HI2UF(hi));
21127 break;
21128 }
21129}
21130#endif
21131
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021132/*
21133 * Return TRUE if a function "name" exists.
21134 */
21135 static int
21136function_exists(name)
21137 char_u *name;
21138{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021139 char_u *nm = name;
21140 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021141 int n = FALSE;
21142
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021143 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021144 nm = skipwhite(nm);
21145
21146 /* Only accept "funcname", "funcname ", "funcname (..." and
21147 * "funcname(...", not "funcname!...". */
21148 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021149 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021150 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021152 else
21153 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021154 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021155 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021156 return n;
21157}
21158
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021159/*
21160 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021161 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021162 */
21163 static int
21164builtin_function(name)
21165 char_u *name;
21166{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021167 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21168 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021169}
21170
Bram Moolenaar05159a02005-02-26 23:04:13 +000021171#if defined(FEAT_PROFILE) || defined(PROTO)
21172/*
21173 * Start profiling function "fp".
21174 */
21175 static void
21176func_do_profile(fp)
21177 ufunc_T *fp;
21178{
21179 fp->uf_tm_count = 0;
21180 profile_zero(&fp->uf_tm_self);
21181 profile_zero(&fp->uf_tm_total);
21182 if (fp->uf_tml_count == NULL)
21183 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21184 (sizeof(int) * fp->uf_lines.ga_len));
21185 if (fp->uf_tml_total == NULL)
21186 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21187 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21188 if (fp->uf_tml_self == NULL)
21189 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21190 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21191 fp->uf_tml_idx = -1;
21192 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21193 || fp->uf_tml_self == NULL)
21194 return; /* out of memory */
21195
21196 fp->uf_profiling = TRUE;
21197}
21198
21199/*
21200 * Dump the profiling results for all functions in file "fd".
21201 */
21202 void
21203func_dump_profile(fd)
21204 FILE *fd;
21205{
21206 hashitem_T *hi;
21207 int todo;
21208 ufunc_T *fp;
21209 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021210 ufunc_T **sorttab;
21211 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021213 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021214 if (todo == 0)
21215 return; /* nothing to dump */
21216
Bram Moolenaar73830342005-02-28 22:48:19 +000021217 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21218
Bram Moolenaar05159a02005-02-26 23:04:13 +000021219 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21220 {
21221 if (!HASHITEM_EMPTY(hi))
21222 {
21223 --todo;
21224 fp = HI2UF(hi);
21225 if (fp->uf_profiling)
21226 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021227 if (sorttab != NULL)
21228 sorttab[st_len++] = fp;
21229
Bram Moolenaar05159a02005-02-26 23:04:13 +000021230 if (fp->uf_name[0] == K_SPECIAL)
21231 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21232 else
21233 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21234 if (fp->uf_tm_count == 1)
21235 fprintf(fd, "Called 1 time\n");
21236 else
21237 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21238 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21239 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21240 fprintf(fd, "\n");
21241 fprintf(fd, "count total (s) self (s)\n");
21242
21243 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21244 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021245 if (FUNCLINE(fp, i) == NULL)
21246 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021247 prof_func_line(fd, fp->uf_tml_count[i],
21248 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021249 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21250 }
21251 fprintf(fd, "\n");
21252 }
21253 }
21254 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021255
21256 if (sorttab != NULL && st_len > 0)
21257 {
21258 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21259 prof_total_cmp);
21260 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21261 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21262 prof_self_cmp);
21263 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21264 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021265
21266 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021267}
Bram Moolenaar73830342005-02-28 22:48:19 +000021268
21269 static void
21270prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21271 FILE *fd;
21272 ufunc_T **sorttab;
21273 int st_len;
21274 char *title;
21275 int prefer_self; /* when equal print only self time */
21276{
21277 int i;
21278 ufunc_T *fp;
21279
21280 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21281 fprintf(fd, "count total (s) self (s) function\n");
21282 for (i = 0; i < 20 && i < st_len; ++i)
21283 {
21284 fp = sorttab[i];
21285 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21286 prefer_self);
21287 if (fp->uf_name[0] == K_SPECIAL)
21288 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21289 else
21290 fprintf(fd, " %s()\n", fp->uf_name);
21291 }
21292 fprintf(fd, "\n");
21293}
21294
21295/*
21296 * Print the count and times for one function or function line.
21297 */
21298 static void
21299prof_func_line(fd, count, total, self, prefer_self)
21300 FILE *fd;
21301 int count;
21302 proftime_T *total;
21303 proftime_T *self;
21304 int prefer_self; /* when equal print only self time */
21305{
21306 if (count > 0)
21307 {
21308 fprintf(fd, "%5d ", count);
21309 if (prefer_self && profile_equal(total, self))
21310 fprintf(fd, " ");
21311 else
21312 fprintf(fd, "%s ", profile_msg(total));
21313 if (!prefer_self && profile_equal(total, self))
21314 fprintf(fd, " ");
21315 else
21316 fprintf(fd, "%s ", profile_msg(self));
21317 }
21318 else
21319 fprintf(fd, " ");
21320}
21321
21322/*
21323 * Compare function for total time sorting.
21324 */
21325 static int
21326#ifdef __BORLANDC__
21327_RTLENTRYF
21328#endif
21329prof_total_cmp(s1, s2)
21330 const void *s1;
21331 const void *s2;
21332{
21333 ufunc_T *p1, *p2;
21334
21335 p1 = *(ufunc_T **)s1;
21336 p2 = *(ufunc_T **)s2;
21337 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21338}
21339
21340/*
21341 * Compare function for self time sorting.
21342 */
21343 static int
21344#ifdef __BORLANDC__
21345_RTLENTRYF
21346#endif
21347prof_self_cmp(s1, s2)
21348 const void *s1;
21349 const void *s2;
21350{
21351 ufunc_T *p1, *p2;
21352
21353 p1 = *(ufunc_T **)s1;
21354 p2 = *(ufunc_T **)s2;
21355 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21356}
21357
Bram Moolenaar05159a02005-02-26 23:04:13 +000021358#endif
21359
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021360/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021361 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021362 * Return TRUE if a package was loaded.
21363 */
21364 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021365script_autoload(name, reload)
21366 char_u *name;
21367 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021368{
21369 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021370 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021371 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021372 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021373
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021374 /* Return quickly when autoload disabled. */
21375 if (no_autoload)
21376 return FALSE;
21377
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021378 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021379 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021380 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021381 return FALSE;
21382
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021383 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021384
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021385 /* Find the name in the list of previously loaded package names. Skip
21386 * "autoload/", it's always the same. */
21387 for (i = 0; i < ga_loaded.ga_len; ++i)
21388 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21389 break;
21390 if (!reload && i < ga_loaded.ga_len)
21391 ret = FALSE; /* was loaded already */
21392 else
21393 {
21394 /* Remember the name if it wasn't loaded already. */
21395 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21396 {
21397 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21398 tofree = NULL;
21399 }
21400
21401 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021402 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021403 ret = TRUE;
21404 }
21405
21406 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021407 return ret;
21408}
21409
21410/*
21411 * Return the autoload script name for a function or variable name.
21412 * Returns NULL when out of memory.
21413 */
21414 static char_u *
21415autoload_name(name)
21416 char_u *name;
21417{
21418 char_u *p;
21419 char_u *scriptname;
21420
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021421 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021422 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21423 if (scriptname == NULL)
21424 return FALSE;
21425 STRCPY(scriptname, "autoload/");
21426 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021427 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021428 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021429 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021430 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021431 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021432}
21433
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21435
21436/*
21437 * Function given to ExpandGeneric() to obtain the list of user defined
21438 * function names.
21439 */
21440 char_u *
21441get_user_func_name(xp, idx)
21442 expand_T *xp;
21443 int idx;
21444{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021445 static long_u done;
21446 static hashitem_T *hi;
21447 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448
21449 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021451 done = 0;
21452 hi = func_hashtab.ht_array;
21453 }
21454 if (done < func_hashtab.ht_used)
21455 {
21456 if (done++ > 0)
21457 ++hi;
21458 while (HASHITEM_EMPTY(hi))
21459 ++hi;
21460 fp = HI2UF(hi);
21461
21462 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21463 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464
21465 cat_func_name(IObuff, fp);
21466 if (xp->xp_context != EXPAND_USER_FUNC)
21467 {
21468 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021469 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 STRCAT(IObuff, ")");
21471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 return IObuff;
21473 }
21474 return NULL;
21475}
21476
21477#endif /* FEAT_CMDL_COMPL */
21478
21479/*
21480 * Copy the function name of "fp" to buffer "buf".
21481 * "buf" must be able to hold the function name plus three bytes.
21482 * Takes care of script-local function names.
21483 */
21484 static void
21485cat_func_name(buf, fp)
21486 char_u *buf;
21487 ufunc_T *fp;
21488{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021489 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 {
21491 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021492 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 }
21494 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021495 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496}
21497
21498/*
21499 * ":delfunction {name}"
21500 */
21501 void
21502ex_delfunction(eap)
21503 exarg_T *eap;
21504{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 char_u *p;
21507 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021508 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509
21510 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021511 name = trans_function_name(&p, eap->skip, 0, &fudi);
21512 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021514 {
21515 if (fudi.fd_dict != NULL && !eap->skip)
21516 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 if (!ends_excmd(*skipwhite(p)))
21520 {
21521 vim_free(name);
21522 EMSG(_(e_trailing));
21523 return;
21524 }
21525 eap->nextcmd = check_nextcmd(p);
21526 if (eap->nextcmd != NULL)
21527 *p = NUL;
21528
21529 if (!eap->skip)
21530 fp = find_func(name);
21531 vim_free(name);
21532
21533 if (!eap->skip)
21534 {
21535 if (fp == NULL)
21536 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021537 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 return;
21539 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021540 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 {
21542 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21543 return;
21544 }
21545
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021546 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021548 /* Delete the dict item that refers to the function, it will
21549 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021550 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021552 else
21553 func_free(fp);
21554 }
21555}
21556
21557/*
21558 * Free a function and remove it from the list of functions.
21559 */
21560 static void
21561func_free(fp)
21562 ufunc_T *fp;
21563{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021564 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021565
21566 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021567 ga_clear_strings(&(fp->uf_args));
21568 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021569#ifdef FEAT_PROFILE
21570 vim_free(fp->uf_tml_count);
21571 vim_free(fp->uf_tml_total);
21572 vim_free(fp->uf_tml_self);
21573#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021574
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021575 /* remove the function from the function hashtable */
21576 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21577 if (HASHITEM_EMPTY(hi))
21578 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021579 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021580 hash_remove(&func_hashtab, hi);
21581
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021582 vim_free(fp);
21583}
21584
21585/*
21586 * Unreference a Function: decrement the reference count and free it when it
21587 * becomes zero. Only for numbered functions.
21588 */
21589 static void
21590func_unref(name)
21591 char_u *name;
21592{
21593 ufunc_T *fp;
21594
21595 if (name != NULL && isdigit(*name))
21596 {
21597 fp = find_func(name);
21598 if (fp == NULL)
21599 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021600 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021601 {
21602 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021603 * when "uf_calls" becomes zero. */
21604 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 func_free(fp);
21606 }
21607 }
21608}
21609
21610/*
21611 * Count a reference to a Function.
21612 */
21613 static void
21614func_ref(name)
21615 char_u *name;
21616{
21617 ufunc_T *fp;
21618
21619 if (name != NULL && isdigit(*name))
21620 {
21621 fp = find_func(name);
21622 if (fp == NULL)
21623 EMSG2(_(e_intern2), "func_ref()");
21624 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 }
21627}
21628
21629/*
21630 * Call a user function.
21631 */
21632 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021633call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 ufunc_T *fp; /* pointer to function */
21635 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 typval_T *argvars; /* arguments */
21637 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 linenr_T firstline; /* first line of range */
21639 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021640 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641{
Bram Moolenaar33570922005-01-25 22:26:29 +000021642 char_u *save_sourcing_name;
21643 linenr_T save_sourcing_lnum;
21644 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021645 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021646 int save_did_emsg;
21647 static int depth = 0;
21648 dictitem_T *v;
21649 int fixvar_idx = 0; /* index in fixvar[] */
21650 int i;
21651 int ai;
21652 char_u numbuf[NUMBUFLEN];
21653 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021654#ifdef FEAT_PROFILE
21655 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021656 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658
21659 /* If depth of calling is getting too high, don't execute the function */
21660 if (depth >= p_mfd)
21661 {
21662 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021663 rettv->v_type = VAR_NUMBER;
21664 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 return;
21666 }
21667 ++depth;
21668
21669 line_breakcheck(); /* check for CTRL-C hit */
21670
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021671 fc = (funccall_T *)alloc(sizeof(funccall_T));
21672 fc->caller = current_funccal;
21673 current_funccal = fc;
21674 fc->func = fp;
21675 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021676 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021677 fc->linenr = 0;
21678 fc->returned = FALSE;
21679 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021681 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21682 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021685 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021686 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21687 * each argument variable and saves a lot of time.
21688 */
21689 /*
21690 * Init l: variables.
21691 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021692 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021693 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021694 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021695 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21696 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021697 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021698 name = v->di_key;
21699 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021700 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021701 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021702 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021703 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021704 v->di_tv.vval.v_dict = selfdict;
21705 ++selfdict->dv_refcount;
21706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021707
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 /*
21709 * Init a: variables.
21710 * Set a:0 to "argcount".
21711 * Set a:000 to a list with room for the "..." arguments.
21712 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021713 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21714 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021715 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021716 /* Use "name" to avoid a warning from some compiler that checks the
21717 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021718 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021719 name = v->di_key;
21720 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021722 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021724 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021725 v->di_tv.vval.v_list = &fc->l_varlist;
21726 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21727 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21728 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021729
21730 /*
21731 * Set a:firstline to "firstline" and a:lastline to "lastline".
21732 * Set a:name to named arguments.
21733 * Set a:N to the "..." arguments.
21734 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021735 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021737 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 (varnumber_T)lastline);
21739 for (i = 0; i < argcount; ++i)
21740 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 if (ai < 0)
21743 /* named argument a:name */
21744 name = FUNCARG(fp, i);
21745 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021746 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021747 /* "..." argument a:1, a:2, etc. */
21748 sprintf((char *)numbuf, "%d", ai + 1);
21749 name = numbuf;
21750 }
21751 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21752 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021753 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21755 }
21756 else
21757 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021758 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21759 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 if (v == NULL)
21761 break;
21762 v->di_flags = DI_FLAGS_RO;
21763 }
21764 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021765 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021767 /* Note: the values are copied directly to avoid alloc/free.
21768 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021770 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021771
21772 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21773 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021774 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21775 fc->l_listitems[ai].li_tv = argvars[i];
21776 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021777 }
21778 }
21779
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 /* Don't redraw while executing the function. */
21781 ++RedrawingDisabled;
21782 save_sourcing_name = sourcing_name;
21783 save_sourcing_lnum = sourcing_lnum;
21784 sourcing_lnum = 1;
21785 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021786 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 if (sourcing_name != NULL)
21788 {
21789 if (save_sourcing_name != NULL
21790 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21791 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21792 else
21793 STRCPY(sourcing_name, "function ");
21794 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21795
21796 if (p_verbose >= 12)
21797 {
21798 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021799 verbose_enter_scroll();
21800
Bram Moolenaar555b2802005-05-19 21:08:39 +000021801 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 if (p_verbose >= 14)
21803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021805 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021806 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021807 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808
21809 msg_puts((char_u *)"(");
21810 for (i = 0; i < argcount; ++i)
21811 {
21812 if (i > 0)
21813 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021814 if (argvars[i].v_type == VAR_NUMBER)
21815 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 else
21817 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021818 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21819 if (s != NULL)
21820 {
21821 trunc_string(s, buf, MSG_BUF_CLEN);
21822 msg_puts(buf);
21823 vim_free(tofree);
21824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 }
21826 }
21827 msg_puts((char_u *)")");
21828 }
21829 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021830
21831 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 --no_wait_return;
21833 }
21834 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021835#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021836 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021837 {
21838 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21839 func_do_profile(fp);
21840 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021841 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021842 {
21843 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021844 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021845 profile_zero(&fp->uf_tm_children);
21846 }
21847 script_prof_save(&wait_start);
21848 }
21849#endif
21850
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021852 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 save_did_emsg = did_emsg;
21854 did_emsg = FALSE;
21855
21856 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021857 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21859
21860 --RedrawingDisabled;
21861
21862 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021863 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021865 clear_tv(rettv);
21866 rettv->v_type = VAR_NUMBER;
21867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 }
21869
Bram Moolenaar05159a02005-02-26 23:04:13 +000021870#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021871 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021872 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021873 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021874 profile_end(&call_start);
21875 profile_sub_wait(&wait_start, &call_start);
21876 profile_add(&fp->uf_tm_total, &call_start);
21877 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021878 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021879 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021880 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21881 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021882 }
21883 }
21884#endif
21885
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 /* when being verbose, mention the return value */
21887 if (p_verbose >= 12)
21888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021890 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021893 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021894 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021895 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021896 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021899 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021900 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021901 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021902 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021903
Bram Moolenaar555b2802005-05-19 21:08:39 +000021904 /* The value may be very long. Skip the middle part, so that we
21905 * have some idea how it starts and ends. smsg() would always
21906 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021907 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021908 if (s != NULL)
21909 {
21910 trunc_string(s, buf, MSG_BUF_CLEN);
21911 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21912 vim_free(tofree);
21913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914 }
21915 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021916
21917 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 --no_wait_return;
21919 }
21920
21921 vim_free(sourcing_name);
21922 sourcing_name = save_sourcing_name;
21923 sourcing_lnum = save_sourcing_lnum;
21924 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021925#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021927 script_prof_restore(&wait_start);
21928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929
21930 if (p_verbose >= 12 && sourcing_name != NULL)
21931 {
21932 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021933 verbose_enter_scroll();
21934
Bram Moolenaar555b2802005-05-19 21:08:39 +000021935 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021937
21938 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 --no_wait_return;
21940 }
21941
21942 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021943 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021945
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021946 /* If the a:000 list and the l: and a: dicts are not referenced we can
21947 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021948 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21949 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21950 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21951 {
21952 free_funccal(fc, FALSE);
21953 }
21954 else
21955 {
21956 hashitem_T *hi;
21957 listitem_T *li;
21958 int todo;
21959
21960 /* "fc" is still in use. This can happen when returning "a:000" or
21961 * assigning "l:" to a global variable.
21962 * Link "fc" in the list for garbage collection later. */
21963 fc->caller = previous_funccal;
21964 previous_funccal = fc;
21965
21966 /* Make a copy of the a: variables, since we didn't do that above. */
21967 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21968 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21969 {
21970 if (!HASHITEM_EMPTY(hi))
21971 {
21972 --todo;
21973 v = HI2DI(hi);
21974 copy_tv(&v->di_tv, &v->di_tv);
21975 }
21976 }
21977
21978 /* Make a copy of the a:000 items, since we didn't do that above. */
21979 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21980 copy_tv(&li->li_tv, &li->li_tv);
21981 }
21982}
21983
21984/*
21985 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021986 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021987 */
21988 static int
21989can_free_funccal(fc, copyID)
21990 funccall_T *fc;
21991 int copyID;
21992{
21993 return (fc->l_varlist.lv_copyID != copyID
21994 && fc->l_vars.dv_copyID != copyID
21995 && fc->l_avars.dv_copyID != copyID);
21996}
21997
21998/*
21999 * Free "fc" and what it contains.
22000 */
22001 static void
22002free_funccal(fc, free_val)
22003 funccall_T *fc;
22004 int free_val; /* a: vars were allocated */
22005{
22006 listitem_T *li;
22007
22008 /* The a: variables typevals may not have been allocated, only free the
22009 * allocated variables. */
22010 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22011
22012 /* free all l: variables */
22013 vars_clear(&fc->l_vars.dv_hashtab);
22014
22015 /* Free the a:000 variables if they were allocated. */
22016 if (free_val)
22017 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22018 clear_tv(&li->li_tv);
22019
22020 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021}
22022
22023/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 * Add a number variable "name" to dict "dp" with value "nr".
22025 */
22026 static void
22027add_nr_var(dp, v, name, nr)
22028 dict_T *dp;
22029 dictitem_T *v;
22030 char *name;
22031 varnumber_T nr;
22032{
22033 STRCPY(v->di_key, name);
22034 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22035 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22036 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022037 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022038 v->di_tv.vval.v_number = nr;
22039}
22040
22041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 * ":return [expr]"
22043 */
22044 void
22045ex_return(eap)
22046 exarg_T *eap;
22047{
22048 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022049 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 int returning = FALSE;
22051
22052 if (current_funccal == NULL)
22053 {
22054 EMSG(_("E133: :return not inside a function"));
22055 return;
22056 }
22057
22058 if (eap->skip)
22059 ++emsg_skip;
22060
22061 eap->nextcmd = NULL;
22062 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022063 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 {
22065 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022066 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022068 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
22070 /* It's safer to return also on error. */
22071 else if (!eap->skip)
22072 {
22073 /*
22074 * Return unless the expression evaluation has been cancelled due to an
22075 * aborting error, an interrupt, or an exception.
22076 */
22077 if (!aborting())
22078 returning = do_return(eap, FALSE, TRUE, NULL);
22079 }
22080
22081 /* When skipping or the return gets pending, advance to the next command
22082 * in this line (!returning). Otherwise, ignore the rest of the line.
22083 * Following lines will be ignored by get_func_line(). */
22084 if (returning)
22085 eap->nextcmd = NULL;
22086 else if (eap->nextcmd == NULL) /* no argument */
22087 eap->nextcmd = check_nextcmd(arg);
22088
22089 if (eap->skip)
22090 --emsg_skip;
22091}
22092
22093/*
22094 * Return from a function. Possibly makes the return pending. Also called
22095 * for a pending return at the ":endtry" or after returning from an extra
22096 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022097 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022098 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * FALSE when the return gets pending.
22100 */
22101 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022102do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 exarg_T *eap;
22104 int reanimate;
22105 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022106 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107{
22108 int idx;
22109 struct condstack *cstack = eap->cstack;
22110
22111 if (reanimate)
22112 /* Undo the return. */
22113 current_funccal->returned = FALSE;
22114
22115 /*
22116 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22117 * not in its finally clause (which then is to be executed next) is found.
22118 * In this case, make the ":return" pending for execution at the ":endtry".
22119 * Otherwise, return normally.
22120 */
22121 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22122 if (idx >= 0)
22123 {
22124 cstack->cs_pending[idx] = CSTP_RETURN;
22125
22126 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022127 /* A pending return again gets pending. "rettv" points to an
22128 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022130 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 else
22132 {
22133 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022134 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022136 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022138 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 {
22140 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022141 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022142 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 else
22144 EMSG(_(e_outofmem));
22145 }
22146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022147 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148
22149 if (reanimate)
22150 {
22151 /* The pending return value could be overwritten by a ":return"
22152 * without argument in a finally clause; reset the default
22153 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022154 current_funccal->rettv->v_type = VAR_NUMBER;
22155 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 }
22157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022158 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 }
22160 else
22161 {
22162 current_funccal->returned = TRUE;
22163
22164 /* If the return is carried out now, store the return value. For
22165 * a return immediately after reanimation, the value is already
22166 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022167 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022169 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022172 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 }
22174 }
22175
22176 return idx < 0;
22177}
22178
22179/*
22180 * Free the variable with a pending return value.
22181 */
22182 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022183discard_pending_return(rettv)
22184 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187}
22188
22189/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022190 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 * is an allocated string. Used by report_pending() for verbose messages.
22192 */
22193 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022194get_return_cmd(rettv)
22195 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022197 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022198 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022199 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022201 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022202 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022203 if (s == NULL)
22204 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205
22206 STRCPY(IObuff, ":return ");
22207 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22208 if (STRLEN(s) + 8 >= IOSIZE)
22209 STRCPY(IObuff + IOSIZE - 4, "...");
22210 vim_free(tofree);
22211 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212}
22213
22214/*
22215 * Get next function line.
22216 * Called by do_cmdline() to get the next line.
22217 * Returns allocated string, or NULL for end of function.
22218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 char_u *
22220get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022221 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022223 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224{
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022226 ufunc_T *fp = fcp->func;
22227 char_u *retval;
22228 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229
22230 /* If breakpoints have been added/deleted need to check for it. */
22231 if (fcp->dbg_tick != debug_tick)
22232 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022233 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 sourcing_lnum);
22235 fcp->dbg_tick = debug_tick;
22236 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022237#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022238 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022239 func_line_end(cookie);
22240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241
Bram Moolenaar05159a02005-02-26 23:04:13 +000022242 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022243 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22244 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 retval = NULL;
22246 else
22247 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022248 /* Skip NULL lines (continuation lines). */
22249 while (fcp->linenr < gap->ga_len
22250 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22251 ++fcp->linenr;
22252 if (fcp->linenr >= gap->ga_len)
22253 retval = NULL;
22254 else
22255 {
22256 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22257 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022258#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022259 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022260 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022261#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 }
22264
22265 /* Did we encounter a breakpoint? */
22266 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22267 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022268 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022270 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 sourcing_lnum);
22272 fcp->dbg_tick = debug_tick;
22273 }
22274
22275 return retval;
22276}
22277
Bram Moolenaar05159a02005-02-26 23:04:13 +000022278#if defined(FEAT_PROFILE) || defined(PROTO)
22279/*
22280 * Called when starting to read a function line.
22281 * "sourcing_lnum" must be correct!
22282 * When skipping lines it may not actually be executed, but we won't find out
22283 * until later and we need to store the time now.
22284 */
22285 void
22286func_line_start(cookie)
22287 void *cookie;
22288{
22289 funccall_T *fcp = (funccall_T *)cookie;
22290 ufunc_T *fp = fcp->func;
22291
22292 if (fp->uf_profiling && sourcing_lnum >= 1
22293 && sourcing_lnum <= fp->uf_lines.ga_len)
22294 {
22295 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022296 /* Skip continuation lines. */
22297 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22298 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022299 fp->uf_tml_execed = FALSE;
22300 profile_start(&fp->uf_tml_start);
22301 profile_zero(&fp->uf_tml_children);
22302 profile_get_wait(&fp->uf_tml_wait);
22303 }
22304}
22305
22306/*
22307 * Called when actually executing a function line.
22308 */
22309 void
22310func_line_exec(cookie)
22311 void *cookie;
22312{
22313 funccall_T *fcp = (funccall_T *)cookie;
22314 ufunc_T *fp = fcp->func;
22315
22316 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22317 fp->uf_tml_execed = TRUE;
22318}
22319
22320/*
22321 * Called when done with a function line.
22322 */
22323 void
22324func_line_end(cookie)
22325 void *cookie;
22326{
22327 funccall_T *fcp = (funccall_T *)cookie;
22328 ufunc_T *fp = fcp->func;
22329
22330 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22331 {
22332 if (fp->uf_tml_execed)
22333 {
22334 ++fp->uf_tml_count[fp->uf_tml_idx];
22335 profile_end(&fp->uf_tml_start);
22336 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022337 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022338 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22339 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022340 }
22341 fp->uf_tml_idx = -1;
22342 }
22343}
22344#endif
22345
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346/*
22347 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022348 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 */
22350 int
22351func_has_ended(cookie)
22352 void *cookie;
22353{
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355
22356 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22357 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022358 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 || fcp->returned);
22360}
22361
22362/*
22363 * return TRUE if cookie indicates a function which "abort"s on errors.
22364 */
22365 int
22366func_has_abort(cookie)
22367 void *cookie;
22368{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022369 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370}
22371
22372#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22373typedef enum
22374{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022375 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22376 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22377 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378} var_flavour_T;
22379
22380static var_flavour_T var_flavour __ARGS((char_u *varname));
22381
22382 static var_flavour_T
22383var_flavour(varname)
22384 char_u *varname;
22385{
22386 char_u *p = varname;
22387
22388 if (ASCII_ISUPPER(*p))
22389 {
22390 while (*(++p))
22391 if (ASCII_ISLOWER(*p))
22392 return VAR_FLAVOUR_SESSION;
22393 return VAR_FLAVOUR_VIMINFO;
22394 }
22395 else
22396 return VAR_FLAVOUR_DEFAULT;
22397}
22398#endif
22399
22400#if defined(FEAT_VIMINFO) || defined(PROTO)
22401/*
22402 * Restore global vars that start with a capital from the viminfo file
22403 */
22404 int
22405read_viminfo_varlist(virp, writing)
22406 vir_T *virp;
22407 int writing;
22408{
22409 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022410 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412
22413 if (!writing && (find_viminfo_parameter('!') != NULL))
22414 {
22415 tab = vim_strchr(virp->vir_line + 1, '\t');
22416 if (tab != NULL)
22417 {
22418 *tab++ = '\0'; /* isolate the variable name */
22419 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022420 type = VAR_STRING;
22421#ifdef FEAT_FLOAT
22422 else if (*tab == 'F')
22423 type = VAR_FLOAT;
22424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425
22426 tab = vim_strchr(tab, '\t');
22427 if (tab != NULL)
22428 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022429 tv.v_type = type;
22430 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022431 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022433#ifdef FEAT_FLOAT
22434 else if (type == VAR_FLOAT)
22435 (void)string2float(tab + 1, &tv.vval.v_float);
22436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022438 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022439 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022440 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022441 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 }
22443 }
22444 }
22445
22446 return viminfo_readline(virp);
22447}
22448
22449/*
22450 * Write global vars that start with a capital to the viminfo file
22451 */
22452 void
22453write_viminfo_varlist(fp)
22454 FILE *fp;
22455{
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 hashitem_T *hi;
22457 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022458 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022459 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022460 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022462 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463
22464 if (find_viminfo_parameter('!') == NULL)
22465 return;
22466
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022467 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022468
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022469 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022470 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022472 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022474 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 this_var = HI2DI(hi);
22476 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022477 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022478 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022479 {
22480 case VAR_STRING: s = "STR"; break;
22481 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022482#ifdef FEAT_FLOAT
22483 case VAR_FLOAT: s = "FLO"; break;
22484#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022485 default: continue;
22486 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022488 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022489 if (p != NULL)
22490 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022491 vim_free(tofree);
22492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 }
22494 }
22495}
22496#endif
22497
22498#if defined(FEAT_SESSION) || defined(PROTO)
22499 int
22500store_session_globals(fd)
22501 FILE *fd;
22502{
Bram Moolenaar33570922005-01-25 22:26:29 +000022503 hashitem_T *hi;
22504 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022505 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 char_u *p, *t;
22507
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022508 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022509 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022511 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022513 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 this_var = HI2DI(hi);
22515 if ((this_var->di_tv.v_type == VAR_NUMBER
22516 || this_var->di_tv.v_type == VAR_STRING)
22517 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022518 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022519 /* Escape special characters with a backslash. Turn a LF and
22520 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022521 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022522 (char_u *)"\\\"\n\r");
22523 if (p == NULL) /* out of memory */
22524 break;
22525 for (t = p; *t != NUL; ++t)
22526 if (*t == '\n')
22527 *t = 'n';
22528 else if (*t == '\r')
22529 *t = 'r';
22530 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022531 this_var->di_key,
22532 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22533 : ' ',
22534 p,
22535 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22536 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022537 || put_eol(fd) == FAIL)
22538 {
22539 vim_free(p);
22540 return FAIL;
22541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 vim_free(p);
22543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022544#ifdef FEAT_FLOAT
22545 else if (this_var->di_tv.v_type == VAR_FLOAT
22546 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22547 {
22548 float_T f = this_var->di_tv.vval.v_float;
22549 int sign = ' ';
22550
22551 if (f < 0)
22552 {
22553 f = -f;
22554 sign = '-';
22555 }
22556 if ((fprintf(fd, "let %s = %c&%f",
22557 this_var->di_key, sign, f) < 0)
22558 || put_eol(fd) == FAIL)
22559 return FAIL;
22560 }
22561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 }
22563 }
22564 return OK;
22565}
22566#endif
22567
Bram Moolenaar661b1822005-07-28 22:36:45 +000022568/*
22569 * Display script name where an item was last set.
22570 * Should only be invoked when 'verbose' is non-zero.
22571 */
22572 void
22573last_set_msg(scriptID)
22574 scid_T scriptID;
22575{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022576 char_u *p;
22577
Bram Moolenaar661b1822005-07-28 22:36:45 +000022578 if (scriptID != 0)
22579 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022580 p = home_replace_save(NULL, get_scriptname(scriptID));
22581 if (p != NULL)
22582 {
22583 verbose_enter();
22584 MSG_PUTS(_("\n\tLast set from "));
22585 MSG_PUTS(p);
22586 vim_free(p);
22587 verbose_leave();
22588 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022589 }
22590}
22591
Bram Moolenaard812df62008-11-09 12:46:09 +000022592/*
22593 * List v:oldfiles in a nice way.
22594 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022595 void
22596ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022597 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022598{
22599 list_T *l = vimvars[VV_OLDFILES].vv_list;
22600 listitem_T *li;
22601 int nr = 0;
22602
22603 if (l == NULL)
22604 msg((char_u *)_("No old files"));
22605 else
22606 {
22607 msg_start();
22608 msg_scroll = TRUE;
22609 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22610 {
22611 msg_outnum((long)++nr);
22612 MSG_PUTS(": ");
22613 msg_outtrans(get_tv_string(&li->li_tv));
22614 msg_putchar('\n');
22615 out_flush(); /* output one line at a time */
22616 ui_breakcheck();
22617 }
22618 /* Assume "got_int" was set to truncate the listing. */
22619 got_int = FALSE;
22620
22621#ifdef FEAT_BROWSE_CMD
22622 if (cmdmod.browse)
22623 {
22624 quit_more = FALSE;
22625 nr = prompt_for_number(FALSE);
22626 msg_starthere();
22627 if (nr > 0)
22628 {
22629 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22630 (long)nr);
22631
22632 if (p != NULL)
22633 {
22634 p = expand_env_save(p);
22635 eap->arg = p;
22636 eap->cmdidx = CMD_edit;
22637 cmdmod.browse = FALSE;
22638 do_exedit(eap, NULL);
22639 vim_free(p);
22640 }
22641 }
22642 }
22643#endif
22644 }
22645}
22646
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647#endif /* FEAT_EVAL */
22648
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022650#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651
22652#ifdef WIN3264
22653/*
22654 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22655 */
22656static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22657static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22658static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22659
22660/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022661 * Get the short path (8.3) for the filename in "fnamep".
22662 * Only works for a valid file name.
22663 * When the path gets longer "fnamep" is changed and the allocated buffer
22664 * is put in "bufp".
22665 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22666 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 */
22668 static int
22669get_short_pathname(fnamep, bufp, fnamelen)
22670 char_u **fnamep;
22671 char_u **bufp;
22672 int *fnamelen;
22673{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022674 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 char_u *newbuf;
22676
22677 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 l = GetShortPathName(*fnamep, *fnamep, len);
22679 if (l > len - 1)
22680 {
22681 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022682 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 newbuf = vim_strnsave(*fnamep, l);
22684 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022685 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686
22687 vim_free(*bufp);
22688 *fnamep = *bufp = newbuf;
22689
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022690 /* Really should always succeed, as the buffer is big enough. */
22691 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 }
22693
22694 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022695 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696}
22697
22698/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022699 * Get the short path (8.3) for the filename in "fname". The converted
22700 * path is returned in "bufp".
22701 *
22702 * Some of the directories specified in "fname" may not exist. This function
22703 * will shorten the existing directories at the beginning of the path and then
22704 * append the remaining non-existing path.
22705 *
22706 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022707 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022708 * bufp - Pointer to an allocated buffer for the filename.
22709 * fnamelen - Length of the filename pointed to by fname
22710 *
22711 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 */
22713 static int
22714shortpath_for_invalid_fname(fname, bufp, fnamelen)
22715 char_u **fname;
22716 char_u **bufp;
22717 int *fnamelen;
22718{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022719 char_u *short_fname, *save_fname, *pbuf_unused;
22720 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022722 int old_len, len;
22723 int new_len, sfx_len;
22724 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725
22726 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022727 old_len = *fnamelen;
22728 save_fname = vim_strnsave(*fname, old_len);
22729 pbuf_unused = NULL;
22730 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022732 endp = save_fname + old_len - 1; /* Find the end of the copy */
22733 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022735 /*
22736 * Try shortening the supplied path till it succeeds by removing one
22737 * directory at a time from the tail of the path.
22738 */
22739 len = 0;
22740 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022742 /* go back one path-separator */
22743 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22744 --endp;
22745 if (endp <= save_fname)
22746 break; /* processed the complete path */
22747
22748 /*
22749 * Replace the path separator with a NUL and try to shorten the
22750 * resulting path.
22751 */
22752 ch = *endp;
22753 *endp = 0;
22754 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022755 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022756 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22757 {
22758 retval = FAIL;
22759 goto theend;
22760 }
22761 *endp = ch; /* preserve the string */
22762
22763 if (len > 0)
22764 break; /* successfully shortened the path */
22765
22766 /* failed to shorten the path. Skip the path separator */
22767 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 }
22769
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022770 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022772 /*
22773 * Succeeded in shortening the path. Now concatenate the shortened
22774 * path with the remaining path at the tail.
22775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022777 /* Compute the length of the new path. */
22778 sfx_len = (int)(save_endp - endp) + 1;
22779 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022781 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022783 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022785 /* There is not enough space in the currently allocated string,
22786 * copy it to a buffer big enough. */
22787 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022789 {
22790 retval = FAIL;
22791 goto theend;
22792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 }
22794 else
22795 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022796 /* Transfer short_fname to the main buffer (it's big enough),
22797 * unless get_short_pathname() did its work in-place. */
22798 *fname = *bufp = save_fname;
22799 if (short_fname != save_fname)
22800 vim_strncpy(save_fname, short_fname, len);
22801 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022803
22804 /* concat the not-shortened part of the path */
22805 vim_strncpy(*fname + len, endp, sfx_len);
22806 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022808
22809theend:
22810 vim_free(pbuf_unused);
22811 vim_free(save_fname);
22812
22813 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814}
22815
22816/*
22817 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022818 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 */
22820 static int
22821shortpath_for_partial(fnamep, bufp, fnamelen)
22822 char_u **fnamep;
22823 char_u **bufp;
22824 int *fnamelen;
22825{
22826 int sepcount, len, tflen;
22827 char_u *p;
22828 char_u *pbuf, *tfname;
22829 int hasTilde;
22830
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022831 /* Count up the path separators from the RHS.. so we know which part
22832 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022834 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 if (vim_ispathsep(*p))
22836 ++sepcount;
22837
22838 /* Need full path first (use expand_env() to remove a "~/") */
22839 hasTilde = (**fnamep == '~');
22840 if (hasTilde)
22841 pbuf = tfname = expand_env_save(*fnamep);
22842 else
22843 pbuf = tfname = FullName_save(*fnamep, FALSE);
22844
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022845 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022847 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849
22850 if (len == 0)
22851 {
22852 /* Don't have a valid filename, so shorten the rest of the
22853 * path if we can. This CAN give us invalid 8.3 filenames, but
22854 * there's not a lot of point in guessing what it might be.
22855 */
22856 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022857 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 }
22860
22861 /* Count the paths backward to find the beginning of the desired string. */
22862 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022863 {
22864#ifdef FEAT_MBYTE
22865 if (has_mbyte)
22866 p -= mb_head_off(tfname, p);
22867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 if (vim_ispathsep(*p))
22869 {
22870 if (sepcount == 0 || (hasTilde && sepcount == 1))
22871 break;
22872 else
22873 sepcount --;
22874 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 if (hasTilde)
22877 {
22878 --p;
22879 if (p >= tfname)
22880 *p = '~';
22881 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022882 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 }
22884 else
22885 ++p;
22886
22887 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22888 vim_free(*bufp);
22889 *fnamelen = (int)STRLEN(p);
22890 *bufp = pbuf;
22891 *fnamep = p;
22892
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022893 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894}
22895#endif /* WIN3264 */
22896
22897/*
22898 * Adjust a filename, according to a string of modifiers.
22899 * *fnamep must be NUL terminated when called. When returning, the length is
22900 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022901 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 * When there is an error, *fnamep is set to NULL.
22903 */
22904 int
22905modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22906 char_u *src; /* string with modifiers */
22907 int *usedlen; /* characters after src that are used */
22908 char_u **fnamep; /* file name so far */
22909 char_u **bufp; /* buffer for allocated file name or NULL */
22910 int *fnamelen; /* length of fnamep */
22911{
22912 int valid = 0;
22913 char_u *tail;
22914 char_u *s, *p, *pbuf;
22915 char_u dirname[MAXPATHL];
22916 int c;
22917 int has_fullname = 0;
22918#ifdef WIN3264
22919 int has_shortname = 0;
22920#endif
22921
22922repeat:
22923 /* ":p" - full path/file_name */
22924 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22925 {
22926 has_fullname = 1;
22927
22928 valid |= VALID_PATH;
22929 *usedlen += 2;
22930
22931 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22932 if ((*fnamep)[0] == '~'
22933#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22934 && ((*fnamep)[1] == '/'
22935# ifdef BACKSLASH_IN_FILENAME
22936 || (*fnamep)[1] == '\\'
22937# endif
22938 || (*fnamep)[1] == NUL)
22939
22940#endif
22941 )
22942 {
22943 *fnamep = expand_env_save(*fnamep);
22944 vim_free(*bufp); /* free any allocated file name */
22945 *bufp = *fnamep;
22946 if (*fnamep == NULL)
22947 return -1;
22948 }
22949
22950 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022951 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 {
22953 if (vim_ispathsep(*p)
22954 && p[1] == '.'
22955 && (p[2] == NUL
22956 || vim_ispathsep(p[2])
22957 || (p[2] == '.'
22958 && (p[3] == NUL || vim_ispathsep(p[3])))))
22959 break;
22960 }
22961
22962 /* FullName_save() is slow, don't use it when not needed. */
22963 if (*p != NUL || !vim_isAbsName(*fnamep))
22964 {
22965 *fnamep = FullName_save(*fnamep, *p != NUL);
22966 vim_free(*bufp); /* free any allocated file name */
22967 *bufp = *fnamep;
22968 if (*fnamep == NULL)
22969 return -1;
22970 }
22971
22972 /* Append a path separator to a directory. */
22973 if (mch_isdir(*fnamep))
22974 {
22975 /* Make room for one or two extra characters. */
22976 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22977 vim_free(*bufp); /* free any allocated file name */
22978 *bufp = *fnamep;
22979 if (*fnamep == NULL)
22980 return -1;
22981 add_pathsep(*fnamep);
22982 }
22983 }
22984
22985 /* ":." - path relative to the current directory */
22986 /* ":~" - path relative to the home directory */
22987 /* ":8" - shortname path - postponed till after */
22988 while (src[*usedlen] == ':'
22989 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22990 {
22991 *usedlen += 2;
22992 if (c == '8')
22993 {
22994#ifdef WIN3264
22995 has_shortname = 1; /* Postpone this. */
22996#endif
22997 continue;
22998 }
22999 pbuf = NULL;
23000 /* Need full path first (use expand_env() to remove a "~/") */
23001 if (!has_fullname)
23002 {
23003 if (c == '.' && **fnamep == '~')
23004 p = pbuf = expand_env_save(*fnamep);
23005 else
23006 p = pbuf = FullName_save(*fnamep, FALSE);
23007 }
23008 else
23009 p = *fnamep;
23010
23011 has_fullname = 0;
23012
23013 if (p != NULL)
23014 {
23015 if (c == '.')
23016 {
23017 mch_dirname(dirname, MAXPATHL);
23018 s = shorten_fname(p, dirname);
23019 if (s != NULL)
23020 {
23021 *fnamep = s;
23022 if (pbuf != NULL)
23023 {
23024 vim_free(*bufp); /* free any allocated file name */
23025 *bufp = pbuf;
23026 pbuf = NULL;
23027 }
23028 }
23029 }
23030 else
23031 {
23032 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23033 /* Only replace it when it starts with '~' */
23034 if (*dirname == '~')
23035 {
23036 s = vim_strsave(dirname);
23037 if (s != NULL)
23038 {
23039 *fnamep = s;
23040 vim_free(*bufp);
23041 *bufp = s;
23042 }
23043 }
23044 }
23045 vim_free(pbuf);
23046 }
23047 }
23048
23049 tail = gettail(*fnamep);
23050 *fnamelen = (int)STRLEN(*fnamep);
23051
23052 /* ":h" - head, remove "/file_name", can be repeated */
23053 /* Don't remove the first "/" or "c:\" */
23054 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23055 {
23056 valid |= VALID_HEAD;
23057 *usedlen += 2;
23058 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023059 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023060 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 *fnamelen = (int)(tail - *fnamep);
23062#ifdef VMS
23063 if (*fnamelen > 0)
23064 *fnamelen += 1; /* the path separator is part of the path */
23065#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023066 if (*fnamelen == 0)
23067 {
23068 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23069 p = vim_strsave((char_u *)".");
23070 if (p == NULL)
23071 return -1;
23072 vim_free(*bufp);
23073 *bufp = *fnamep = tail = p;
23074 *fnamelen = 1;
23075 }
23076 else
23077 {
23078 while (tail > s && !after_pathsep(s, tail))
23079 mb_ptr_back(*fnamep, tail);
23080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 }
23082
23083 /* ":8" - shortname */
23084 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23085 {
23086 *usedlen += 2;
23087#ifdef WIN3264
23088 has_shortname = 1;
23089#endif
23090 }
23091
23092#ifdef WIN3264
23093 /* Check shortname after we have done 'heads' and before we do 'tails'
23094 */
23095 if (has_shortname)
23096 {
23097 pbuf = NULL;
23098 /* Copy the string if it is shortened by :h */
23099 if (*fnamelen < (int)STRLEN(*fnamep))
23100 {
23101 p = vim_strnsave(*fnamep, *fnamelen);
23102 if (p == 0)
23103 return -1;
23104 vim_free(*bufp);
23105 *bufp = *fnamep = p;
23106 }
23107
23108 /* Split into two implementations - makes it easier. First is where
23109 * there isn't a full name already, second is where there is.
23110 */
23111 if (!has_fullname && !vim_isAbsName(*fnamep))
23112 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023113 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 return -1;
23115 }
23116 else
23117 {
23118 int l;
23119
23120 /* Simple case, already have the full-name
23121 * Nearly always shorter, so try first time. */
23122 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023123 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 return -1;
23125
23126 if (l == 0)
23127 {
23128 /* Couldn't find the filename.. search the paths.
23129 */
23130 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023131 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 return -1;
23133 }
23134 *fnamelen = l;
23135 }
23136 }
23137#endif /* WIN3264 */
23138
23139 /* ":t" - tail, just the basename */
23140 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23141 {
23142 *usedlen += 2;
23143 *fnamelen -= (int)(tail - *fnamep);
23144 *fnamep = tail;
23145 }
23146
23147 /* ":e" - extension, can be repeated */
23148 /* ":r" - root, without extension, can be repeated */
23149 while (src[*usedlen] == ':'
23150 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23151 {
23152 /* find a '.' in the tail:
23153 * - for second :e: before the current fname
23154 * - otherwise: The last '.'
23155 */
23156 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23157 s = *fnamep - 2;
23158 else
23159 s = *fnamep + *fnamelen - 1;
23160 for ( ; s > tail; --s)
23161 if (s[0] == '.')
23162 break;
23163 if (src[*usedlen + 1] == 'e') /* :e */
23164 {
23165 if (s > tail)
23166 {
23167 *fnamelen += (int)(*fnamep - (s + 1));
23168 *fnamep = s + 1;
23169#ifdef VMS
23170 /* cut version from the extension */
23171 s = *fnamep + *fnamelen - 1;
23172 for ( ; s > *fnamep; --s)
23173 if (s[0] == ';')
23174 break;
23175 if (s > *fnamep)
23176 *fnamelen = s - *fnamep;
23177#endif
23178 }
23179 else if (*fnamep <= tail)
23180 *fnamelen = 0;
23181 }
23182 else /* :r */
23183 {
23184 if (s > tail) /* remove one extension */
23185 *fnamelen = (int)(s - *fnamep);
23186 }
23187 *usedlen += 2;
23188 }
23189
23190 /* ":s?pat?foo?" - substitute */
23191 /* ":gs?pat?foo?" - global substitute */
23192 if (src[*usedlen] == ':'
23193 && (src[*usedlen + 1] == 's'
23194 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23195 {
23196 char_u *str;
23197 char_u *pat;
23198 char_u *sub;
23199 int sep;
23200 char_u *flags;
23201 int didit = FALSE;
23202
23203 flags = (char_u *)"";
23204 s = src + *usedlen + 2;
23205 if (src[*usedlen + 1] == 'g')
23206 {
23207 flags = (char_u *)"g";
23208 ++s;
23209 }
23210
23211 sep = *s++;
23212 if (sep)
23213 {
23214 /* find end of pattern */
23215 p = vim_strchr(s, sep);
23216 if (p != NULL)
23217 {
23218 pat = vim_strnsave(s, (int)(p - s));
23219 if (pat != NULL)
23220 {
23221 s = p + 1;
23222 /* find end of substitution */
23223 p = vim_strchr(s, sep);
23224 if (p != NULL)
23225 {
23226 sub = vim_strnsave(s, (int)(p - s));
23227 str = vim_strnsave(*fnamep, *fnamelen);
23228 if (sub != NULL && str != NULL)
23229 {
23230 *usedlen = (int)(p + 1 - src);
23231 s = do_string_sub(str, pat, sub, flags);
23232 if (s != NULL)
23233 {
23234 *fnamep = s;
23235 *fnamelen = (int)STRLEN(s);
23236 vim_free(*bufp);
23237 *bufp = s;
23238 didit = TRUE;
23239 }
23240 }
23241 vim_free(sub);
23242 vim_free(str);
23243 }
23244 vim_free(pat);
23245 }
23246 }
23247 /* after using ":s", repeat all the modifiers */
23248 if (didit)
23249 goto repeat;
23250 }
23251 }
23252
23253 return valid;
23254}
23255
23256/*
23257 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23258 * "flags" can be "g" to do a global substitute.
23259 * Returns an allocated string, NULL for error.
23260 */
23261 char_u *
23262do_string_sub(str, pat, sub, flags)
23263 char_u *str;
23264 char_u *pat;
23265 char_u *sub;
23266 char_u *flags;
23267{
23268 int sublen;
23269 regmatch_T regmatch;
23270 int i;
23271 int do_all;
23272 char_u *tail;
23273 garray_T ga;
23274 char_u *ret;
23275 char_u *save_cpo;
23276
23277 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23278 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023279 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280
23281 ga_init2(&ga, 1, 200);
23282
23283 do_all = (flags[0] == 'g');
23284
23285 regmatch.rm_ic = p_ic;
23286 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23287 if (regmatch.regprog != NULL)
23288 {
23289 tail = str;
23290 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23291 {
23292 /*
23293 * Get some space for a temporary buffer to do the substitution
23294 * into. It will contain:
23295 * - The text up to where the match is.
23296 * - The substituted text.
23297 * - The text after the match.
23298 */
23299 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23300 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23301 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23302 {
23303 ga_clear(&ga);
23304 break;
23305 }
23306
23307 /* copy the text up to where the match is */
23308 i = (int)(regmatch.startp[0] - tail);
23309 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23310 /* add the substituted text */
23311 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23312 + ga.ga_len + i, TRUE, TRUE, FALSE);
23313 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 /* avoid getting stuck on a match with an empty string */
23315 if (tail == regmatch.endp[0])
23316 {
23317 if (*tail == NUL)
23318 break;
23319 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23320 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 }
23322 else
23323 {
23324 tail = regmatch.endp[0];
23325 if (*tail == NUL)
23326 break;
23327 }
23328 if (!do_all)
23329 break;
23330 }
23331
23332 if (ga.ga_data != NULL)
23333 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23334
23335 vim_free(regmatch.regprog);
23336 }
23337
23338 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23339 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023340 if (p_cpo == empty_option)
23341 p_cpo = save_cpo;
23342 else
23343 /* Darn, evaluating {sub} expression changed the value. */
23344 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345
23346 return ret;
23347}
23348
23349#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */