blob: 8a26b6690cf9fd56704c472067f5099535f9d255 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200129/* When using exists() don't auto-load a script. */
130static int no_autoload = FALSE;
131
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 * When recursively copying lists and dicts we need to remember which ones we
134 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136 */
137static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000138#define COPYID_INC 2
139#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140
141/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000142 * Array to hold the hashtab with variables local to each sourced script.
143 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000145typedef struct
146{
147 dictitem_T sv_var;
148 dict_T sv_dict;
149} scriptvar_T;
150
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200151static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
152#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
153#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155static int echo_attr = 0; /* attributes used for ":echo" */
156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000157/* Values for trans_function_name() argument: */
158#define TFN_INT 1 /* internal function name OK */
159#define TFN_QUIET 2 /* no error messages */
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Structure to hold info for a user function.
163 */
164typedef struct ufunc ufunc_T;
165
166struct ufunc
167{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000168 int uf_varargs; /* variable nr of arguments */
169 int uf_flags;
170 int uf_calls; /* nr of active calls */
171 garray_T uf_args; /* arguments */
172 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173#ifdef FEAT_PROFILE
174 int uf_profiling; /* TRUE when func is being profiled */
175 /* profiling the function as a whole */
176 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000177 proftime_T uf_tm_total; /* time spent in function + children */
178 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000179 proftime_T uf_tm_children; /* time spent in children this call */
180 /* profiling the function per line */
181 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T *uf_tml_total; /* time spent in a line + children */
183 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tml_start; /* start time for current line */
185 proftime_T uf_tml_children; /* time spent in children for this line */
186 proftime_T uf_tml_wait; /* start wait time for current line */
187 int uf_tml_idx; /* index of line being timed; -1 if none */
188 int uf_tml_execed; /* line being timed was executed */
189#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000190 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 int uf_refcount; /* for numbered function: reference count */
193 char_u uf_name[1]; /* name of function (actually longer); can
194 start with <SNR>123_ (<SNR> is K_SPECIAL
195 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196};
197
198/* function flags */
199#define FC_ABORT 1 /* abort function on error */
200#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000201#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000206static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000208/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000209static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
210
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000211/* list heads for garbage collection */
212static dict_T *first_dict = NULL; /* list of all dicts */
213static list_T *first_list = NULL; /* list of all lists */
214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000215/* From user function to hashitem and back. */
216static ufunc_T dumuf;
217#define UF2HIKEY(fp) ((fp)->uf_name)
218#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
219#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
220
221#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
222#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar33570922005-01-25 22:26:29 +0000224#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
225#define VAR_SHORT_LEN 20 /* short variable name length */
226#define FIXVAR_CNT 12 /* number of fixed variables */
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000229typedef struct funccall_S funccall_T;
230
231struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 ufunc_T *func; /* function being called */
234 int linenr; /* next line to be executed */
235 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000236 struct /* fixed variables for arguments */
237 {
238 dictitem_T var; /* variable (without room for name) */
239 char_u room[VAR_SHORT_LEN]; /* room for the name */
240 } fixvar[FIXVAR_CNT];
241 dict_T l_vars; /* l: local function variables */
242 dictitem_T l_vars_var; /* variable for l: scope */
243 dict_T l_avars; /* a: argument variables */
244 dictitem_T l_avars_var; /* variable for a: scope */
245 list_T l_varlist; /* list for a:000 */
246 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 linenr_T breakpoint; /* next line with breakpoint or zero */
249 int dbg_tick; /* debug_tick when breakpoint was set */
250 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000251#ifdef FEAT_PROFILE
252 proftime_T prof_child; /* time spent in a child */
253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000254 funccall_T *caller; /* calling function or NULL */
255};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258 * Info used by a ":for" loop.
259 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261{
262 int fi_semicolon; /* TRUE if ending in '; var]' */
263 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 listwatch_T fi_lw; /* keep an eye on the item used. */
265 list_T *fi_list; /* list being used */
266} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000269 * Struct used by trans_function_name()
270 */
271typedef struct
272{
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000274 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dictitem_T *fd_di; /* Dictionary item used */
276} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000277
Bram Moolenaara7043832005-01-21 11:56:39 +0000278
279/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 * Array to hold the value of v: variables.
281 * The value is in a dictitem, so that it can also be used in the v: scope.
282 * The reason to use this table anyway is for very quick access to the
283 * variables with the VV_ defines.
284 */
285#include "version.h"
286
287/* values for vv_flags: */
288#define VV_COMPAT 1 /* compatible, also used without "v:" */
289#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000290#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000292#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294static struct vimvar
295{
296 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297 dictitem_T vv_di; /* value and name for key */
298 char vv_filler[16]; /* space for LONGEST name below!!! */
299 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300} vimvars[VV_LEN] =
301{
302 /*
303 * The order here must match the VV_ defines in vim.h!
304 * Initializing a union does not work, leave tv.vval empty to get zero's.
305 */
306 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("count1", VAR_NUMBER), VV_RO},
308 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
309 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
310 {VV_NAME("warningmsg", VAR_STRING), 0},
311 {VV_NAME("statusmsg", VAR_STRING), 0},
312 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
313 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
314 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
316 {VV_NAME("termresponse", VAR_STRING), VV_RO},
317 {VV_NAME("fname", VAR_STRING), VV_RO},
318 {VV_NAME("lang", VAR_STRING), VV_RO},
319 {VV_NAME("lc_time", VAR_STRING), VV_RO},
320 {VV_NAME("ctype", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
323 {VV_NAME("fname_in", VAR_STRING), VV_RO},
324 {VV_NAME("fname_out", VAR_STRING), VV_RO},
325 {VV_NAME("fname_new", VAR_STRING), VV_RO},
326 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
327 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
328 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
331 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("progname", VAR_STRING), VV_RO},
333 {VV_NAME("servername", VAR_STRING), VV_RO},
334 {VV_NAME("dying", VAR_NUMBER), VV_RO},
335 {VV_NAME("exception", VAR_STRING), VV_RO},
336 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
337 {VV_NAME("register", VAR_STRING), VV_RO},
338 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
339 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000340 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
341 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000342 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000343 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
344 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000345 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000350 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000351 {VV_NAME("swapname", VAR_STRING), VV_RO},
352 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000353 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000354 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000355 {VV_NAME("mouse_win", VAR_NUMBER), 0},
356 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
357 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000358 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000359 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000360 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
371/*
372 * The v: variables are stored in dictionary "vimvardict".
373 * "vimvars_var" is the variable that is used for the "l:" scope.
374 */
375static dict_T vimvardict;
376static dictitem_T vimvars_var;
377#define vimvarht vimvardict.dv_hashtab
378
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
380static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
381#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
382static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
383#endif
384static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
385static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
386static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
388static void list_glob_vars __ARGS((int *first));
389static void list_buf_vars __ARGS((int *first));
390static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_vim_vars __ARGS((int *first));
395static void list_script_vars __ARGS((int *first));
396static void list_func_vars __ARGS((int *first));
397static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000398static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
399static int check_changedtick __ARGS((char_u *arg));
400static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
401static void clear_lval __ARGS((lval_T *lp));
402static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
403static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
404static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
405static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
406static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
407static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
408static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
409static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
410static void item_lock __ARGS((typval_T *tv, int deep, int lock));
411static int tv_islocked __ARGS((typval_T *tv));
412
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
414static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000419static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
420static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000422static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000427static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static listitem_T *listitem_alloc __ARGS((void));
429static void listitem_free __ARGS((listitem_T *item));
430static void listitem_remove __ARGS((list_T *l, listitem_T *item));
431static long list_len __ARGS((list_T *l));
432static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
433static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
434static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000436static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000439static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
441static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
442static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *list2string __ARGS((typval_T *tv, int copyID));
446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000453static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
455static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000456static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
461static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
464static int string2float __ARGS((char_u *text, float_T *value));
465#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
467static int find_internal_func __ARGS((char_u *name));
468static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
469static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200470static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000471static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000472static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000473
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
475static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636#ifdef vim_mkdir
637static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100640#ifdef FEAT_MZSCHEME
641static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000645static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000646#ifdef FEAT_FLOAT
647static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000650static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000651static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000654static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
667static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000687static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200691static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000694static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000695static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#ifdef FEAT_FLOAT
699static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
701#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000702static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200703static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704#ifdef HAVE_STRFTIME
705static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
707static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200713static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200714static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000720static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200721static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000723static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000724static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000726static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000727static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000729static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200730#ifdef FEAT_FLOAT
731static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#ifdef FEAT_FLOAT
738static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200741static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200742static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000757static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000758static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static int get_env_len __ARGS((char_u **arg));
760static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000762static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
763#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
764#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
765 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000766static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000769static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
770static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static typval_T *alloc_tv __ARGS((void));
772static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void init_tv __ARGS((typval_T *varp));
774static long get_tv_number __ARGS((typval_T *varp));
775static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000776static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static char_u *get_tv_string __ARGS((typval_T *varp));
778static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000779static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000781static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
783static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
784static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000785static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
786static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
788static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000789static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
819static void func_unref __ARGS((char_u *name));
820static void func_ref __ARGS((char_u *name));
821static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000822static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
823static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
826static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000827static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000828static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200831
832#ifdef EBCDIC
833static int compare_func_name __ARGS((const void *s1, const void *s2));
834static void sortFunctions __ARGS(());
835#endif
836
837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838/* Character used as separated in autoload function/variable names. */
839#define AUTOLOAD_CHAR '#'
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
850 init_var_dict(&globvardict, &globvars_var);
851 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000852 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000853 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000854
855 for (i = 0; i < VV_LEN; ++i)
856 {
857 p = &vimvars[i];
858 STRCPY(p->vv_di.di_key, p->vv_name);
859 if (p->vv_flags & VV_RO)
860 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
861 else if (p->vv_flags & VV_RO_SBX)
862 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
863 else
864 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000865
866 /* add to v: scope dict, unless the value is not always available */
867 if (p->vv_type != VAR_UNKNOWN)
868 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000870 /* add to compat scope dict */
871 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000873 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874
875#ifdef EBCDIC
876 /*
877 * Sort the function table, to enable binary sort.
878 */
879 sortFunctions();
880#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000881}
882
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000883#if defined(EXITFREE) || defined(PROTO)
884 void
885eval_clear()
886{
887 int i;
888 struct vimvar *p;
889
890 for (i = 0; i < VV_LEN; ++i)
891 {
892 p = &vimvars[i];
893 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000894 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000895 vim_free(p->vv_str);
896 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000897 }
898 else if (p->vv_di.di_tv.v_type == VAR_LIST)
899 {
900 list_unref(p->vv_list);
901 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000903 }
904 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000905 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906 hash_clear(&compat_hashtab);
907
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909
910 /* global variables */
911 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000913 /* autoloaded script names */
914 ga_clear_strings(&ga_loaded);
915
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200916 /* script-local variables */
917 for (i = 1; i <= ga_scripts.ga_len; ++i)
918 {
919 vars_clear(&SCRIPT_VARS(i));
920 vim_free(SCRIPT_SV(i));
921 }
922 ga_clear(&ga_scripts);
923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000924 /* unreferenced lists and dicts */
925 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000926
927 /* functions */
928 free_all_functions();
929 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930}
931#endif
932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Return the name of the executed function.
935 */
936 char_u *
937func_name(cookie)
938 void *cookie;
939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942
943/*
944 * Return the address holding the next breakpoint line for a funccall cookie.
945 */
946 linenr_T *
947func_breakpoint(cookie)
948 void *cookie;
949{
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951}
952
953/*
954 * Return the address holding the debug tick for a funccall cookie.
955 */
956 int *
957func_dbg_tick(cookie)
958 void *cookie;
959{
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the nesting level for a funccall cookie.
965 */
966 int
967func_level(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000974funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000976/* pointer to list of previously used funccal, still around because some
977 * item in it is still being used. */
978funccall_T *previous_funccal = NULL;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
981 * Return TRUE when a function was ended by a ":return" command.
982 */
983 int
984current_func_returned()
985{
986 return current_funccal->returned;
987}
988
989
990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * Set an internal variable to a string value. Creates the variable if it does
992 * not already exist.
993 */
994 void
995set_internal_string_var(name, value)
996 char_u *name;
997 char_u *value;
998{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 val = vim_strsave(value);
1003 if (val != NULL)
1004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012}
1013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001014static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static char_u *redir_endp = NULL;
1017static char_u *redir_varname = NULL;
1018
1019/*
1020 * Start recording command output to a variable
1021 * Returns OK if successfully completed the setup. FAIL otherwise.
1022 */
1023 int
1024var_redir_start(name, append)
1025 char_u *name;
1026 int append; /* append to an existing variable */
1027{
1028 int save_emsg;
1029 int err;
1030 typval_T tv;
1031
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001032 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001033 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 {
1035 EMSG(_(e_invarg));
1036 return FAIL;
1037 }
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 redir_varname = vim_strsave(name);
1041 if (redir_varname == NULL)
1042 return FAIL;
1043
1044 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1045 if (redir_lval == NULL)
1046 {
1047 var_redir_stop();
1048 return FAIL;
1049 }
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 /* The output is stored in growarray "redir_ga" until redirection ends. */
1052 ga_init2(&redir_ga, (int)sizeof(char), 500);
1053
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1056 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1058 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001059 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp != NULL && *redir_endp != NUL)
1061 /* Trailing characters are present after the variable name */
1062 EMSG(_(e_trailing));
1063 else
1064 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001065 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 var_redir_stop();
1067 return FAIL;
1068 }
1069
1070 /* check if we can write to the variable: set it to or append an empty
1071 * string */
1072 save_emsg = did_emsg;
1073 did_emsg = FALSE;
1074 tv.v_type = VAR_STRING;
1075 tv.vval.v_string = (char_u *)"";
1076 if (append)
1077 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1078 else
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001080 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001082 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 if (err)
1084 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 var_redir_stop();
1087 return FAIL;
1088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089
1090 return OK;
1091}
1092
1093/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 * Append "value[value_len]" to the variable set by var_redir_start().
1095 * The actual appending is postponed until redirection ends, because the value
1096 * appended may in fact be the string we write to, changing it may cause freed
1097 * memory to be used:
1098 * :redir => foo
1099 * :let foo
1100 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 */
1102 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001107 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108
1109 if (redir_lval == NULL)
1110 return;
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 {
1119 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 }
1122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124}
1125
1126/*
1127 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
1131var_redir_stop()
1132{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 typval_T tv;
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_lval != NULL)
1136 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 /* If there was no error: assign the text to the variable. */
1138 if (redir_endp != NULL)
1139 {
1140 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 /* Call get_lval() again, if it's inside a Dict or List it may
1144 * have changed. */
1145 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1146 FALSE, FALSE, FALSE, FNE_CHECK_START);
1147 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1148 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1149 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* free the collected output */
1153 vim_free(redir_ga.ga_data);
1154 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 vim_free(redir_lval);
1157 redir_lval = NULL;
1158 }
1159 vim_free(redir_varname);
1160 redir_varname = NULL;
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163# if defined(FEAT_MBYTE) || defined(PROTO)
1164 int
1165eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1166 char_u *enc_from;
1167 char_u *enc_to;
1168 char_u *fname_from;
1169 char_u *fname_to;
1170{
1171 int err = FALSE;
1172
1173 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1174 set_vim_var_string(VV_CC_TO, enc_to, -1);
1175 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1176 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1177 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1178 err = TRUE;
1179 set_vim_var_string(VV_CC_FROM, NULL, -1);
1180 set_vim_var_string(VV_CC_TO, NULL, -1);
1181 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1182 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1183
1184 if (err)
1185 return FAIL;
1186 return OK;
1187}
1188# endif
1189
1190# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1191 int
1192eval_printexpr(fname, args)
1193 char_u *fname;
1194 char_u *args;
1195{
1196 int err = FALSE;
1197
1198 set_vim_var_string(VV_FNAME_IN, fname, -1);
1199 set_vim_var_string(VV_CMDARG, args, -1);
1200 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1201 err = TRUE;
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_CMDARG, NULL, -1);
1204
1205 if (err)
1206 {
1207 mch_remove(fname);
1208 return FAIL;
1209 }
1210 return OK;
1211}
1212# endif
1213
1214# if defined(FEAT_DIFF) || defined(PROTO)
1215 void
1216eval_diff(origfile, newfile, outfile)
1217 char_u *origfile;
1218 char_u *newfile;
1219 char_u *outfile;
1220{
1221 int err = FALSE;
1222
1223 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1224 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1225 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1226 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1229 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1230}
1231
1232 void
1233eval_patch(origfile, difffile, outfile)
1234 char_u *origfile;
1235 char_u *difffile;
1236 char_u *outfile;
1237{
1238 int err;
1239
1240 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1241 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1242 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1243 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1244 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1246 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1247}
1248# endif
1249
1250/*
1251 * Top level evaluation function, returning a boolean.
1252 * Sets "error" to TRUE if there was an error.
1253 * Return TRUE or FALSE.
1254 */
1255 int
1256eval_to_bool(arg, error, nextcmd, skip)
1257 char_u *arg;
1258 int *error;
1259 char_u **nextcmd;
1260 int skip; /* only parse, don't execute */
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval = FALSE;
1264
1265 if (skip)
1266 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 else
1270 {
1271 *error = FALSE;
1272 if (!skip)
1273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001275 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 }
1278 if (skip)
1279 --emsg_skip;
1280
1281 return retval;
1282}
1283
1284/*
1285 * Top level evaluation function, returning a string. If "skip" is TRUE,
1286 * only parsing to "nextcmd" is done, without reporting errors. Return
1287 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1288 */
1289 char_u *
1290eval_to_string_skip(arg, nextcmd, skip)
1291 char_u *arg;
1292 char_u **nextcmd;
1293 int skip; /* only parse, don't execute */
1294{
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 char_u *retval;
1297
1298 if (skip)
1299 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 retval = NULL;
1302 else
1303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 retval = vim_strsave(get_tv_string(&tv));
1305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 if (skip)
1308 --emsg_skip;
1309
1310 return retval;
1311}
1312
1313/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001314 * Skip over an expression at "*pp".
1315 * Return FAIL for an error, OK otherwise.
1316 */
1317 int
1318skip_expr(pp)
1319 char_u **pp;
1320{
Bram Moolenaar33570922005-01-25 22:26:29 +00001321 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322
1323 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325}
1326
1327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329 * When "convert" is TRUE convert a List into a sequence of lines and convert
1330 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Return pointer to allocated memory, or NULL for failure.
1332 */
1333 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *arg;
1336 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001342#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 retval = NULL;
1348 else
1349 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001351 {
1352 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001353 if (tv.vval.v_list != NULL)
1354 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001355 ga_append(&ga, NUL);
1356 retval = (char_u *)ga.ga_data;
1357 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358#ifdef FEAT_FLOAT
1359 else if (convert && tv.v_type == VAR_FLOAT)
1360 {
1361 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1362 retval = vim_strsave(numbuf);
1363 }
1364#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 else
1366 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369
1370 return retval;
1371}
1372
1373/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001374 * Call eval_to_string() without using current local variables and using
1375 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 */
1377 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 char_u *arg;
1380 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 char_u *retval;
1384 void *save_funccalp;
1385
1386 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 if (use_sandbox)
1388 ++sandbox;
1389 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 --sandbox;
1393 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 restore_funccal(save_funccalp);
1395 return retval;
1396}
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398/*
1399 * Top level evaluation function, returning a number.
1400 * Evaluates "expr" silently.
1401 * Returns -1 for an error.
1402 */
1403 int
1404eval_to_number(expr)
1405 char_u *expr;
1406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001409 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 ++emsg_off;
1412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 retval = -1;
1415 else
1416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001417 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 --emsg_off;
1421
1422 return retval;
1423}
1424
Bram Moolenaara40058a2005-07-11 22:42:07 +00001425/*
1426 * Prepare v: variable "idx" to be used.
1427 * Save the current typeval in "save_tv".
1428 * When not used yet add the variable to the v: hashtable.
1429 */
1430 static void
1431prepare_vimvar(idx, save_tv)
1432 int idx;
1433 typval_T *save_tv;
1434{
1435 *save_tv = vimvars[idx].vv_tv;
1436 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1437 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1438}
1439
1440/*
1441 * Restore v: variable "idx" to typeval "save_tv".
1442 * When no longer defined, remove the variable from the v: hashtable.
1443 */
1444 static void
1445restore_vimvar(idx, save_tv)
1446 int idx;
1447 typval_T *save_tv;
1448{
1449 hashitem_T *hi;
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451 vimvars[idx].vv_tv = *save_tv;
1452 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1453 {
1454 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1455 if (HASHITEM_EMPTY(hi))
1456 EMSG2(_(e_intern2), "restore_vimvar()");
1457 else
1458 hash_remove(&vimvarht, hi);
1459 }
1460}
1461
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001462#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001463/*
1464 * Evaluate an expression to a list with suggestions.
1465 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001466 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467 */
1468 list_T *
1469eval_spell_expr(badword, expr)
1470 char_u *badword;
1471 char_u *expr;
1472{
1473 typval_T save_val;
1474 typval_T rettv;
1475 list_T *list = NULL;
1476 char_u *p = skipwhite(expr);
1477
1478 /* Set "v:val" to the bad word. */
1479 prepare_vimvar(VV_VAL, &save_val);
1480 vimvars[VV_VAL].vv_type = VAR_STRING;
1481 vimvars[VV_VAL].vv_str = badword;
1482 if (p_verbose == 0)
1483 ++emsg_off;
1484
1485 if (eval1(&p, &rettv, TRUE) == OK)
1486 {
1487 if (rettv.v_type != VAR_LIST)
1488 clear_tv(&rettv);
1489 else
1490 list = rettv.vval.v_list;
1491 }
1492
1493 if (p_verbose == 0)
1494 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001495 restore_vimvar(VV_VAL, &save_val);
1496
1497 return list;
1498}
1499
1500/*
1501 * "list" is supposed to contain two items: a word and a number. Return the
1502 * word in "pp" and the number as the return value.
1503 * Return -1 if anything isn't right.
1504 * Used to get the good word and score from the eval_spell_expr() result.
1505 */
1506 int
1507get_spellword(list, pp)
1508 list_T *list;
1509 char_u **pp;
1510{
1511 listitem_T *li;
1512
1513 li = list->lv_first;
1514 if (li == NULL)
1515 return -1;
1516 *pp = get_tv_string(&li->li_tv);
1517
1518 li = li->li_next;
1519 if (li == NULL)
1520 return -1;
1521 return get_tv_number(&li->li_tv);
1522}
1523#endif
1524
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001525/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001526 * Top level evaluation function.
1527 * Returns an allocated typval_T with the result.
1528 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529 */
1530 typval_T *
1531eval_expr(arg, nextcmd)
1532 char_u *arg;
1533 char_u **nextcmd;
1534{
1535 typval_T *tv;
1536
1537 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001538 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 {
1540 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001541 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 }
1543
1544 return tv;
1545}
1546
1547
Bram Moolenaar4f688582007-07-24 12:34:30 +00001548#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1549 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001551 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001552 * Uses argv[argc] for the function arguments. Only Number and String
1553 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 static int
1557call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 char_u *func;
1559 int argc;
1560 char_u **argv;
1561 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 long n;
1566 int len;
1567 int i;
1568 int doesrange;
1569 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001572 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 for (i = 0; i < argc; i++)
1577 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 /* Pass a NULL or empty argument as an empty string */
1579 if (argv[i] == NULL || *argv[i] == NUL)
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_STRING;
1582 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 continue;
1584 }
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 /* Recognize a number argument, the others must be strings. */
1587 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1588 if (len != 0 && len == (int)STRLEN(argv[i]))
1589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001590 argvars[i].v_type = VAR_NUMBER;
1591 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 else
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_STRING;
1596 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
1599
1600 if (safe)
1601 {
1602 save_funccalp = save_funccal();
1603 ++sandbox;
1604 }
1605
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1607 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (safe)
1611 {
1612 --sandbox;
1613 restore_funccal(save_funccalp);
1614 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 vim_free(argvars);
1616
1617 if (ret == FAIL)
1618 clear_tv(rettv);
1619
1620 return ret;
1621}
1622
Bram Moolenaar4f688582007-07-24 12:34:30 +00001623# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001625 * Call vimL function "func" and return the result as a string.
1626 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 * Uses argv[argc] for the function arguments.
1628 */
1629 void *
1630call_func_retstr(func, argc, argv, safe)
1631 char_u *func;
1632 int argc;
1633 char_u **argv;
1634 int safe; /* use the sandbox */
1635{
1636 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001637 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638
1639 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1640 return NULL;
1641
1642 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 return retval;
1645}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001646# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647
Bram Moolenaar4f688582007-07-24 12:34:30 +00001648# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001650 * Call vimL function "func" and return the result as a number.
1651 * Returns -1 when calling the function fails.
1652 * Uses argv[argc] for the function arguments.
1653 */
1654 long
1655call_func_retnr(func, argc, argv, safe)
1656 char_u *func;
1657 int argc;
1658 char_u **argv;
1659 int safe; /* use the sandbox */
1660{
1661 typval_T rettv;
1662 long retval;
1663
1664 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1665 return -1;
1666
1667 retval = get_tv_number_chk(&rettv, NULL);
1668 clear_tv(&rettv);
1669 return retval;
1670}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001672
1673/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001674 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001676 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 */
1678 void *
1679call_func_retlist(func, argc, argv, safe)
1680 char_u *func;
1681 int argc;
1682 char_u **argv;
1683 int safe; /* use the sandbox */
1684{
1685 typval_T rettv;
1686
1687 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1688 return NULL;
1689
1690 if (rettv.v_type != VAR_LIST)
1691 {
1692 clear_tv(&rettv);
1693 return NULL;
1694 }
1695
1696 return rettv.vval.v_list;
1697}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
1699
Bram Moolenaar4f688582007-07-24 12:34:30 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Save the current function call pointer, and set it to NULL.
1703 * Used when executing autocommands and for ":source".
1704 */
1705 void *
1706save_funccal()
1707{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001708 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 current_funccal = NULL;
1711 return (void *)fc;
1712}
1713
1714 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001715restore_funccal(vfc)
1716 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = (funccall_T *)vfc;
1719
1720 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721}
1722
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723#if defined(FEAT_PROFILE) || defined(PROTO)
1724/*
1725 * Prepare profiling for entering a child or something else that is not
1726 * counted for the script/function itself.
1727 * Should always be called in pair with prof_child_exit().
1728 */
1729 void
1730prof_child_enter(tm)
1731 proftime_T *tm; /* place to store waittime */
1732{
1733 funccall_T *fc = current_funccal;
1734
1735 if (fc != NULL && fc->func->uf_profiling)
1736 profile_start(&fc->prof_child);
1737 script_prof_save(tm);
1738}
1739
1740/*
1741 * Take care of time spent in a child.
1742 * Should always be called after prof_child_enter().
1743 */
1744 void
1745prof_child_exit(tm)
1746 proftime_T *tm; /* where waittime was stored */
1747{
1748 funccall_T *fc = current_funccal;
1749
1750 if (fc != NULL && fc->func->uf_profiling)
1751 {
1752 profile_end(&fc->prof_child);
1753 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1754 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1755 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1756 }
1757 script_prof_restore(tm);
1758}
1759#endif
1760
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762#ifdef FEAT_FOLDING
1763/*
1764 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1765 * it in "*cp". Doesn't give error messages.
1766 */
1767 int
1768eval_foldexpr(arg, cp)
1769 char_u *arg;
1770 int *cp;
1771{
Bram Moolenaar33570922005-01-25 22:26:29 +00001772 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 int retval;
1774 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001775 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1776 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001779 if (use_sandbox)
1780 ++sandbox;
1781 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 retval = 0;
1785 else
1786 {
1787 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (tv.v_type == VAR_NUMBER)
1789 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001790 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 retval = 0;
1792 else
1793 {
1794 /* If the result is a string, check if there is a non-digit before
1795 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (!VIM_ISDIGIT(*s) && *s != '-')
1798 *cp = *s++;
1799 retval = atol((char *)s);
1800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001804 if (use_sandbox)
1805 --sandbox;
1806 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 return retval;
1809}
1810#endif
1811
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 * ":let" list all variable values
1814 * ":let var1 var2" list variable values
1815 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001816 * ":let var += expr" assignment command.
1817 * ":let var -= expr" assignment command.
1818 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 void
1822ex_let(eap)
1823 exarg_T *eap;
1824{
1825 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 int var_count = 0;
1830 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001832 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001833 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaardb552d602006-03-23 22:59:57 +00001835 argend = skip_var_list(arg, &var_count, &semicolon);
1836 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001838 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1839 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001843 /*
1844 * ":let" without "=": list variables
1845 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (*arg == '[')
1847 EMSG(_(e_invarg));
1848 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001850 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 list_glob_vars(&first);
1855 list_buf_vars(&first);
1856 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001857#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001859#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_script_vars(&first);
1861 list_func_vars(&first);
1862 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 eap->nextcmd = check_nextcmd(arg);
1865 }
1866 else
1867 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 op[0] = '=';
1869 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001870 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 {
1872 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1873 op[0] = expr[-1]; /* +=, -= or .= */
1874 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (eap->skip)
1878 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (eap->skip)
1881 {
1882 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 --emsg_skip;
1885 }
1886 else if (i != FAIL)
1887 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892 }
1893}
1894
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895/*
1896 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1897 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 * When "nextchars" is not NULL it points to a string with characters that
1899 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1900 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 * Returns OK or FAIL;
1902 */
1903 static int
1904ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1905 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 int copy; /* copy values from "tv", don't move */
1908 int semicolon; /* from skip_var_list() */
1909 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911{
1912 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 listitem_T *item;
1916 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917
1918 if (*arg != '[')
1919 {
1920 /*
1921 * ":let var = expr" or ":for var in list"
1922 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 return FAIL;
1925 return OK;
1926 }
1927
1928 /*
1929 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1930 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001931 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 {
1933 EMSG(_(e_listreq));
1934 return FAIL;
1935 }
1936
1937 i = list_len(l);
1938 if (semicolon == 0 && var_count < i)
1939 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001940 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return FAIL;
1942 }
1943 if (var_count - semicolon > i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948
1949 item = l->lv_first;
1950 while (*arg != ']')
1951 {
1952 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 item = item->li_next;
1955 if (arg == NULL)
1956 return FAIL;
1957
1958 arg = skipwhite(arg);
1959 if (*arg == ';')
1960 {
1961 /* Put the rest of the list (may be empty) in the var after ';'.
1962 * Create a new list for this. */
1963 l = list_alloc();
1964 if (l == NULL)
1965 return FAIL;
1966 while (item != NULL)
1967 {
1968 list_append_tv(l, &item->li_tv);
1969 item = item->li_next;
1970 }
1971
1972 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001973 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 ltv.vval.v_list = l;
1975 l->lv_refcount = 1;
1976
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1978 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 clear_tv(&ltv);
1980 if (arg == NULL)
1981 return FAIL;
1982 break;
1983 }
1984 else if (*arg != ',' && *arg != ']')
1985 {
1986 EMSG2(_(e_intern2), "ex_let_vars()");
1987 return FAIL;
1988 }
1989 }
1990
1991 return OK;
1992}
1993
1994/*
1995 * Skip over assignable variable "var" or list of variables "[var, var]".
1996 * Used for ":let varvar = expr" and ":for varvar in expr".
1997 * For "[var, var]" increment "*var_count" for each variable.
1998 * for "[var, var; var]" set "semicolon".
1999 * Return NULL for an error.
2000 */
2001 static char_u *
2002skip_var_list(arg, var_count, semicolon)
2003 char_u *arg;
2004 int *var_count;
2005 int *semicolon;
2006{
2007 char_u *p, *s;
2008
2009 if (*arg == '[')
2010 {
2011 /* "[var, var]": find the matching ']'. */
2012 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002013 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 {
2015 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2016 s = skip_var_one(p);
2017 if (s == p)
2018 {
2019 EMSG2(_(e_invarg2), p);
2020 return NULL;
2021 }
2022 ++*var_count;
2023
2024 p = skipwhite(s);
2025 if (*p == ']')
2026 break;
2027 else if (*p == ';')
2028 {
2029 if (*semicolon == 1)
2030 {
2031 EMSG(_("Double ; in list of variables"));
2032 return NULL;
2033 }
2034 *semicolon = 1;
2035 }
2036 else if (*p != ',')
2037 {
2038 EMSG2(_(e_invarg2), p);
2039 return NULL;
2040 }
2041 }
2042 return p + 1;
2043 }
2044 else
2045 return skip_var_one(arg);
2046}
2047
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002048/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002049 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002050 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002051 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 static char_u *
2053skip_var_one(arg)
2054 char_u *arg;
2055{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 if (*arg == '@' && arg[1] != NUL)
2057 return arg + 2;
2058 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2059 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002060}
2061
Bram Moolenaara7043832005-01-21 11:56:39 +00002062/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002063 * List variables for hashtab "ht" with prefix "prefix".
2064 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002067list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002072{
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashitem_T *hi;
2074 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 int todo;
2076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2079 {
2080 if (!HASHITEM_EMPTY(hi))
2081 {
2082 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 di = HI2DI(hi);
2084 if (empty || di->di_tv.v_type != VAR_STRING
2085 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
2088 }
2089}
2090
2091/*
2092 * List global variables.
2093 */
2094 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095list_glob_vars(first)
2096 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002097{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002099}
2100
2101/*
2102 * List buffer variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_buf_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108 char_u numbuf[NUMBUFLEN];
2109
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2111 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112
2113 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2115 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002116}
2117
2118/*
2119 * List window variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_win_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2126 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002129#ifdef FEAT_WINDOWS
2130/*
2131 * List tab page variables.
2132 */
2133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134list_tab_vars(first)
2135 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002136{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2138 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139}
2140#endif
2141
Bram Moolenaara7043832005-01-21 11:56:39 +00002142/*
2143 * List Vim variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_vim_vars(first)
2147 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150}
2151
2152/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002153 * List script-local variables, if there is a script.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_script_vars(first)
2157 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158{
2159 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2161 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162}
2163
2164/*
2165 * List function variables, if there is a function.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_func_vars(first)
2169 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170{
2171 if (current_funccal != NULL)
2172 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174}
2175
2176/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 * List variables in "arg".
2178 */
2179 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 exarg_T *eap;
2182 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184{
2185 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 char_u *name_start;
2189 char_u *arg_subsc;
2190 char_u *tofree;
2191 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192
2193 while (!ends_excmd(*arg) && !got_int)
2194 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002197 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2199 {
2200 emsg_severe = TRUE;
2201 EMSG(_(e_trailing));
2202 break;
2203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 /* get_name_len() takes care of expanding curly braces */
2208 name_start = name = arg;
2209 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2210 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* This is mainly to keep test 49 working: when expanding
2213 * curly braces fails overrule the exception error message. */
2214 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 emsg_severe = TRUE;
2217 EMSG2(_(e_invarg2), arg);
2218 break;
2219 }
2220 error = TRUE;
2221 }
2222 else
2223 {
2224 if (tofree != NULL)
2225 name = tofree;
2226 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 else
2229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* handle d.key, l[idx], f(expr) */
2231 arg_subsc = arg;
2232 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 case 'g': list_glob_vars(first); break;
2241 case 'b': list_buf_vars(first); break;
2242 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'v': list_vim_vars(first); break;
2247 case 's': list_script_vars(first); break;
2248 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 default:
2250 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002252 }
2253 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 {
2255 char_u numbuf[NUMBUFLEN];
2256 char_u *tf;
2257 int c;
2258 char_u *s;
2259
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002260 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 c = *arg;
2262 *arg = NUL;
2263 list_one_var_a((char_u *)"",
2264 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002265 tv.v_type,
2266 s == NULL ? (char_u *)"" : s,
2267 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 *arg = c;
2269 vim_free(tf);
2270 }
2271 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 }
2274 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275
2276 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278
2279 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 }
2281
2282 return arg;
2283}
2284
2285/*
2286 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2287 * Returns a pointer to the char just after the var name.
2288 * Returns NULL if there is an error.
2289 */
2290 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002295 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297{
2298 int c1;
2299 char_u *name;
2300 char_u *p;
2301 char_u *arg_end = NULL;
2302 int len;
2303 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305
2306 /*
2307 * ":let $VAR = expr": Set environment variable.
2308 */
2309 if (*arg == '$')
2310 {
2311 /* Find the end of the name. */
2312 ++arg;
2313 name = arg;
2314 len = get_env_len(&arg);
2315 if (len == 0)
2316 EMSG2(_(e_invarg2), name - 1);
2317 else
2318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 if (op != NULL && (*op == '+' || *op == '-'))
2320 EMSG2(_(e_letwrong), op);
2321 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 EMSG(_(e_letunexp));
2324 else
2325 {
2326 c1 = name[len];
2327 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002328 p = get_tv_string_chk(tv);
2329 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 {
2331 int mustfree = FALSE;
2332 char_u *s = vim_getenv(name, &mustfree);
2333
2334 if (s != NULL)
2335 {
2336 p = tofree = concat_str(s, p);
2337 if (mustfree)
2338 vim_free(s);
2339 }
2340 }
2341 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 if (STRICMP(name, "HOME") == 0)
2345 init_homedir();
2346 else if (didset_vim && STRICMP(name, "VIM") == 0)
2347 didset_vim = FALSE;
2348 else if (didset_vimruntime
2349 && STRICMP(name, "VIMRUNTIME") == 0)
2350 didset_vimruntime = FALSE;
2351 arg_end = arg;
2352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 }
2356 }
2357 }
2358
2359 /*
2360 * ":let &option = expr": Set option value.
2361 * ":let &l:option = expr": Set local option value.
2362 * ":let &g:option = expr": Set global option value.
2363 */
2364 else if (*arg == '&')
2365 {
2366 /* Find the end of the name. */
2367 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 if (p == NULL || (endchars != NULL
2369 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 EMSG(_(e_letunexp));
2371 else
2372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 long n;
2374 int opt_type;
2375 long numval;
2376 char_u *stringval = NULL;
2377 char_u *s;
2378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 c1 = *p;
2380 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381
2382 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002383 s = get_tv_string_chk(tv); /* != NULL if number or string */
2384 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 {
2386 opt_type = get_option_value(arg, &numval,
2387 &stringval, opt_flags);
2388 if ((opt_type == 1 && *op == '.')
2389 || (opt_type == 0 && *op != '.'))
2390 EMSG2(_(e_letwrong), op);
2391 else
2392 {
2393 if (opt_type == 1) /* number */
2394 {
2395 if (*op == '+')
2396 n = numval + n;
2397 else
2398 n = numval - n;
2399 }
2400 else if (opt_type == 0 && stringval != NULL) /* string */
2401 {
2402 s = concat_str(stringval, s);
2403 vim_free(stringval);
2404 stringval = s;
2405 }
2406 }
2407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (s != NULL)
2409 {
2410 set_option_value(arg, n, s, opt_flags);
2411 arg_end = p;
2412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 }
2416 }
2417
2418 /*
2419 * ":let @r = expr": Set register contents.
2420 */
2421 else if (*arg == '@')
2422 {
2423 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 if (op != NULL && (*op == '+' || *op == '-'))
2425 EMSG2(_(e_letwrong), op);
2426 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002431 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 char_u *s;
2433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002434 p = get_tv_string_chk(tv);
2435 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002437 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 if (s != NULL)
2439 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002440 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(s);
2442 }
2443 }
2444 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 arg_end = arg + 1;
2448 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 }
2451 }
2452
2453 /*
2454 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2465 EMSG(_(e_letunexp));
2466 else
2467 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474
2475 else
2476 EMSG2(_(e_invarg2), arg);
2477
2478 return arg_end;
2479}
2480
2481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2483 */
2484 static int
2485check_changedtick(arg)
2486 char_u *arg;
2487{
2488 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2489 {
2490 EMSG2(_(e_readonlyvar), arg);
2491 return TRUE;
2492 }
2493 return FALSE;
2494}
2495
2496/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * Get an lval: variable, Dict item or List item that can be assigned a value
2498 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2499 * "name.key", "name.key[expr]" etc.
2500 * Indexing only works if "name" is an existing List or Dictionary.
2501 * "name" points to the start of the name.
2502 * If "rettv" is not NULL it points to the value to be assigned.
2503 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2504 * wrong; must end in space or cmd separator.
2505 *
2506 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002507 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 */
2510 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002511get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 typval_T *rettv;
2514 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 int unlet;
2516 int skip;
2517 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 char_u *expr_start, *expr_end;
2522 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 dictitem_T *v;
2524 typval_T var1;
2525 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534
2535 if (skip)
2536 {
2537 /* When skipping just find the end of the name. */
2538 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002539 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 }
2541
2542 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002543 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (expr_start != NULL)
2545 {
2546 /* Don't expand the name when we already know there is an error. */
2547 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2548 && *p != '[' && *p != '.')
2549 {
2550 EMSG(_(e_trailing));
2551 return NULL;
2552 }
2553
2554 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2555 if (lp->ll_exp_name == NULL)
2556 {
2557 /* Report an invalid expression in braces, unless the
2558 * expression evaluation has been cancelled due to an
2559 * aborting error, an interrupt, or an exception. */
2560 if (!aborting() && !quiet)
2561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002562 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 EMSG2(_(e_invarg2), name);
2564 return NULL;
2565 }
2566 }
2567 lp->ll_name = lp->ll_exp_name;
2568 }
2569 else
2570 lp->ll_name = name;
2571
2572 /* Without [idx] or .key we are done. */
2573 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2574 return p;
2575
2576 cc = *p;
2577 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002578 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (v == NULL && !quiet)
2580 EMSG2(_(e_undefvar), lp->ll_name);
2581 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 if (v == NULL)
2583 return NULL;
2584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 /*
2586 * Loop until no more [idx] or .key is following.
2587 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2592 && !(lp->ll_tv->v_type == VAR_DICT
2593 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (!quiet)
2596 EMSG(_("E689: Can only index a List or Dictionary"));
2597 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E708: [:] must come last"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 len = -1;
2607 if (*p == '.')
2608 {
2609 key = p + 1;
2610 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2611 ;
2612 if (len == 0)
2613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!quiet)
2615 EMSG(_(e_emptykey));
2616 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 }
2618 p = key + len;
2619 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 else
2621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 if (*p == ':')
2625 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 empty1 = FALSE;
2629 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002631 if (get_tv_string_chk(&var1) == NULL)
2632 {
2633 /* not a number or string */
2634 clear_tv(&var1);
2635 return NULL;
2636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638
2639 /* Optionally get the second index [ :expr]. */
2640 if (*p == ':')
2641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002645 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 if (!empty1)
2647 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (rettv != NULL && (rettv->v_type != VAR_LIST
2651 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 if (!empty1)
2656 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659 p = skipwhite(p + 1);
2660 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 else
2663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (!empty1)
2668 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002671 if (get_tv_string_chk(&var2) == NULL)
2672 {
2673 /* not a number or string */
2674 if (!empty1)
2675 clear_tv(&var1);
2676 clear_tv(&var2);
2677 return NULL;
2678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (!quiet)
2688 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (!empty1)
2690 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
2695
2696 /* Skip to past ']'. */
2697 ++p;
2698 }
2699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
2702 if (len == -1)
2703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002705 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (*key == NUL)
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
2713 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 lp->ll_list = NULL;
2715 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002716 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002719 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (len == -1)
2725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
2728 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (len == -1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 p = NULL;
2736 break;
2737 }
2738 if (len == -1)
2739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742 else
2743 {
2744 /*
2745 * Get the number and item for the only or first index of the List.
2746 */
2747 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 else
2750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var1);
2753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002754 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_list = lp->ll_tv->vval.v_list;
2756 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2757 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002759 if (lp->ll_n1 < 0)
2760 {
2761 lp->ll_n1 = 0;
2762 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2763 }
2764 }
2765 if (lp->ll_li == NULL)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771
2772 /*
2773 * May need to find the item or absolute index for the second
2774 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 * When no index given: "lp->ll_empty2" is TRUE.
2776 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 }
2789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2791 if (lp->ll_n1 < 0)
2792 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2793 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002795 }
2796
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 return p;
2802}
2803
2804/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 */
2807 static void
2808clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810{
2811 vim_free(lp->ll_exp_name);
2812 vim_free(lp->ll_newkey);
2813}
2814
2815/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002816 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 */
2820 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002826 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827{
2828 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 listitem_T *ri;
2830 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831
2832 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 cc = *endp;
2837 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 if (op != NULL && *op != '=')
2839 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002840 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002841
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002842 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002843 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002844 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 {
2846 if (tv_op(&tv, rettv, op) == OK)
2847 set_var(lp->ll_name, &tv, FALSE);
2848 clear_tv(&tv);
2849 }
2850 }
2851 else
2852 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002856 else if (tv_check_lock(lp->ll_newkey == NULL
2857 ? lp->ll_tv->v_lock
2858 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2859 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 else if (lp->ll_range)
2861 {
2862 /*
2863 * Assign the List values to the list items.
2864 */
2865 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002866 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 if (op != NULL && *op != '=')
2868 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2869 else
2870 {
2871 clear_tv(&lp->ll_li->li_tv);
2872 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2873 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ri = ri->li_next;
2875 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2876 break;
2877 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002880 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 ri = NULL;
2883 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002885 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_li = lp->ll_li->li_next;
2887 ++lp->ll_n1;
2888 }
2889 if (ri != NULL)
2890 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 else if (lp->ll_empty2
2892 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 : lp->ll_n1 != lp->ll_n2)
2894 EMSG(_("E711: List value has not enough items"));
2895 }
2896 else
2897 {
2898 /*
2899 * Assign to a List or Dictionary item.
2900 */
2901 if (lp->ll_newkey != NULL)
2902 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 if (op != NULL && *op != '=')
2904 {
2905 EMSG2(_(e_letwrong), op);
2906 return;
2907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002910 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 if (di == NULL)
2912 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002913 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2914 {
2915 vim_free(di);
2916 return;
2917 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 else if (op != NULL && *op != '=')
2921 {
2922 tv_op(lp->ll_tv, rettv, op);
2923 return;
2924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002925 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 /*
2929 * Assign the value to the variable or list item.
2930 */
2931 if (copy)
2932 copy_tv(rettv, lp->ll_tv);
2933 else
2934 {
2935 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002936 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 }
2939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940}
2941
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2944 * Returns OK or FAIL.
2945 */
2946 static int
2947tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 typval_T *tv1;
2949 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 char_u *op;
2951{
2952 long n;
2953 char_u numbuf[NUMBUFLEN];
2954 char_u *s;
2955
2956 /* Can't do anything with a Funcref or a Dict on the right. */
2957 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2958 {
2959 switch (tv1->v_type)
2960 {
2961 case VAR_DICT:
2962 case VAR_FUNC:
2963 break;
2964
2965 case VAR_LIST:
2966 if (*op != '+' || tv2->v_type != VAR_LIST)
2967 break;
2968 /* List += List */
2969 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2970 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2971 return OK;
2972
2973 case VAR_NUMBER:
2974 case VAR_STRING:
2975 if (tv2->v_type == VAR_LIST)
2976 break;
2977 if (*op == '+' || *op == '-')
2978 {
2979 /* nr += nr or nr -= nr*/
2980 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981#ifdef FEAT_FLOAT
2982 if (tv2->v_type == VAR_FLOAT)
2983 {
2984 float_T f = n;
2985
2986 if (*op == '+')
2987 f += tv2->vval.v_float;
2988 else
2989 f -= tv2->vval.v_float;
2990 clear_tv(tv1);
2991 tv1->v_type = VAR_FLOAT;
2992 tv1->vval.v_float = f;
2993 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002995#endif
2996 {
2997 if (*op == '+')
2998 n += get_tv_number(tv2);
2999 else
3000 n -= get_tv_number(tv2);
3001 clear_tv(tv1);
3002 tv1->v_type = VAR_NUMBER;
3003 tv1->vval.v_number = n;
3004 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 }
3006 else
3007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003008 if (tv2->v_type == VAR_FLOAT)
3009 break;
3010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 /* str .= str */
3012 s = get_tv_string(tv1);
3013 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3014 clear_tv(tv1);
3015 tv1->v_type = VAR_STRING;
3016 tv1->vval.v_string = s;
3017 }
3018 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019
3020#ifdef FEAT_FLOAT
3021 case VAR_FLOAT:
3022 {
3023 float_T f;
3024
3025 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3026 && tv2->v_type != VAR_NUMBER
3027 && tv2->v_type != VAR_STRING))
3028 break;
3029 if (tv2->v_type == VAR_FLOAT)
3030 f = tv2->vval.v_float;
3031 else
3032 f = get_tv_number(tv2);
3033 if (*op == '+')
3034 tv1->vval.v_float += f;
3035 else
3036 tv1->vval.v_float -= f;
3037 }
3038 return OK;
3039#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 }
3041 }
3042
3043 EMSG2(_(e_letwrong), op);
3044 return FAIL;
3045}
3046
3047/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 * Add a watcher to a list.
3049 */
3050 static void
3051list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003052 list_T *l;
3053 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054{
3055 lw->lw_next = l->lv_watch;
3056 l->lv_watch = lw;
3057}
3058
3059/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061 * No warning when it isn't found...
3062 */
3063 static void
3064list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003065 list_T *l;
3066 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003067{
Bram Moolenaar33570922005-01-25 22:26:29 +00003068 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069
3070 lwp = &l->lv_watch;
3071 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3072 {
3073 if (lw == lwrem)
3074 {
3075 *lwp = lw->lw_next;
3076 break;
3077 }
3078 lwp = &lw->lw_next;
3079 }
3080}
3081
3082/*
3083 * Just before removing an item from a list: advance watchers to the next
3084 * item.
3085 */
3086 static void
3087list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 list_T *l;
3089 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090{
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092
3093 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3094 if (lw->lw_item == item)
3095 lw->lw_item = item->li_next;
3096}
3097
3098/*
3099 * Evaluate the expression used in a ":for var in expr" command.
3100 * "arg" points to "var".
3101 * Set "*errp" to TRUE for an error, FALSE otherwise;
3102 * Return a pointer that holds the info. Null when there is an error.
3103 */
3104 void *
3105eval_for_line(arg, errp, nextcmdp, skip)
3106 char_u *arg;
3107 int *errp;
3108 char_u **nextcmdp;
3109 int skip;
3110{
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 typval_T tv;
3114 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115
3116 *errp = TRUE; /* default: there is an error */
3117
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 if (fi == NULL)
3120 return NULL;
3121
3122 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3123 if (expr == NULL)
3124 return fi;
3125
3126 expr = skipwhite(expr);
3127 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3128 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003129 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 return fi;
3131 }
3132
3133 if (skip)
3134 ++emsg_skip;
3135 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3136 {
3137 *errp = FALSE;
3138 if (!skip)
3139 {
3140 l = tv.vval.v_list;
3141 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003142 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003144 clear_tv(&tv);
3145 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 else
3147 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003148 /* No need to increment the refcount, it's already set for the
3149 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 fi->fi_list = l;
3151 list_add_watch(l, &fi->fi_lw);
3152 fi->fi_lw.lw_item = l->lv_first;
3153 }
3154 }
3155 }
3156 if (skip)
3157 --emsg_skip;
3158
3159 return fi;
3160}
3161
3162/*
3163 * Use the first item in a ":for" list. Advance to the next.
3164 * Assign the values to the variable (list). "arg" points to the first one.
3165 * Return TRUE when a valid item was found, FALSE when at end of list or
3166 * something wrong.
3167 */
3168 int
3169next_for_item(fi_void, arg)
3170 void *fi_void;
3171 char_u *arg;
3172{
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176
3177 item = fi->fi_lw.lw_item;
3178 if (item == NULL)
3179 result = FALSE;
3180 else
3181 {
3182 fi->fi_lw.lw_item = item->li_next;
3183 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3184 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3185 }
3186 return result;
3187}
3188
3189/*
3190 * Free the structure used to store info used by ":for".
3191 */
3192 void
3193free_for_info(fi_void)
3194 void *fi_void;
3195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003198 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 list_unref(fi->fi_list);
3202 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203 vim_free(fi);
3204}
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3207
3208 void
3209set_context_for_expression(xp, arg, cmdidx)
3210 expand_T *xp;
3211 char_u *arg;
3212 cmdidx_T cmdidx;
3213{
3214 int got_eq = FALSE;
3215 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 if (cmdidx == CMD_let)
3219 {
3220 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003221 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 {
3223 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003224 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 {
3226 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 if (vim_iswhite(*p))
3229 break;
3230 }
3231 return;
3232 }
3233 }
3234 else
3235 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3236 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 while ((xp->xp_pattern = vim_strpbrk(arg,
3238 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3239 {
3240 c = *xp->xp_pattern;
3241 if (c == '&')
3242 {
3243 c = xp->xp_pattern[1];
3244 if (c == '&')
3245 {
3246 ++xp->xp_pattern;
3247 xp->xp_context = cmdidx != CMD_let || got_eq
3248 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3249 }
3250 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003253 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3254 xp->xp_pattern += 2;
3255
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 else if (c == '$')
3259 {
3260 /* environment variable */
3261 xp->xp_context = EXPAND_ENV_VARS;
3262 }
3263 else if (c == '=')
3264 {
3265 got_eq = TRUE;
3266 xp->xp_context = EXPAND_EXPRESSION;
3267 }
3268 else if (c == '<'
3269 && xp->xp_context == EXPAND_FUNCTIONS
3270 && vim_strchr(xp->xp_pattern, '(') == NULL)
3271 {
3272 /* Function name can start with "<SNR>" */
3273 break;
3274 }
3275 else if (cmdidx != CMD_let || got_eq)
3276 {
3277 if (c == '"') /* string */
3278 {
3279 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3280 if (c == '\\' && xp->xp_pattern[1] != NUL)
3281 ++xp->xp_pattern;
3282 xp->xp_context = EXPAND_NOTHING;
3283 }
3284 else if (c == '\'') /* literal string */
3285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3288 /* skip */ ;
3289 xp->xp_context = EXPAND_NOTHING;
3290 }
3291 else if (c == '|')
3292 {
3293 if (xp->xp_pattern[1] == '|')
3294 {
3295 ++xp->xp_pattern;
3296 xp->xp_context = EXPAND_EXPRESSION;
3297 }
3298 else
3299 xp->xp_context = EXPAND_COMMANDS;
3300 }
3301 else
3302 xp->xp_context = EXPAND_EXPRESSION;
3303 }
3304 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 /* Doesn't look like something valid, expand as an expression
3306 * anyway. */
3307 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 arg = xp->xp_pattern;
3309 if (*arg != NUL)
3310 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3311 /* skip */ ;
3312 }
3313 xp->xp_pattern = arg;
3314}
3315
3316#endif /* FEAT_CMDL_COMPL */
3317
3318/*
3319 * ":1,25call func(arg1, arg2)" function call.
3320 */
3321 void
3322ex_call(eap)
3323 exarg_T *eap;
3324{
3325 char_u *arg = eap->arg;
3326 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003328 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 linenr_T lnum;
3332 int doesrange;
3333 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003334 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003336 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003337 if (fudi.fd_newkey != NULL)
3338 {
3339 /* Still need to give an error message for missing key. */
3340 EMSG2(_(e_dictkey), fudi.fd_newkey);
3341 vim_free(fudi.fd_newkey);
3342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003343 if (tofree == NULL)
3344 return;
3345
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003346 /* Increase refcount on dictionary, it could get deleted when evaluating
3347 * the arguments. */
3348 if (fudi.fd_dict != NULL)
3349 ++fudi.fd_dict->dv_refcount;
3350
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003351 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003352 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003353 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaar532c7802005-01-27 14:44:31 +00003355 /* Skip white space to allow ":call func ()". Not good, but required for
3356 * backward compatibility. */
3357 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 if (*startarg != '(')
3361 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003362 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 goto end;
3364 }
3365
3366 /*
3367 * When skipping, evaluate the function once, to find the end of the
3368 * arguments.
3369 * When the function takes a range, this is discovered after the first
3370 * call, and the loop is broken.
3371 */
3372 if (eap->skip)
3373 {
3374 ++emsg_skip;
3375 lnum = eap->line2; /* do it once, also with an invalid range */
3376 }
3377 else
3378 lnum = eap->line1;
3379 for ( ; lnum <= eap->line2; ++lnum)
3380 {
3381 if (!eap->skip && eap->addr_count > 0)
3382 {
3383 curwin->w_cursor.lnum = lnum;
3384 curwin->w_cursor.col = 0;
3385 }
3386 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003387 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003388 eap->line1, eap->line2, &doesrange,
3389 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 failed = TRUE;
3392 break;
3393 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003394
3395 /* Handle a function returning a Funcref, Dictionary or List. */
3396 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3397 {
3398 failed = TRUE;
3399 break;
3400 }
3401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (doesrange || eap->skip)
3404 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003409 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (aborting())
3411 break;
3412 }
3413 if (eap->skip)
3414 --emsg_skip;
3415
3416 if (!failed)
3417 {
3418 /* Check for trailing illegal characters and a following command. */
3419 if (!ends_excmd(*arg))
3420 {
3421 emsg_severe = TRUE;
3422 EMSG(_(e_trailing));
3423 }
3424 else
3425 eap->nextcmd = check_nextcmd(arg);
3426 }
3427
3428end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003429 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003430 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431}
3432
3433/*
3434 * ":unlet[!] var1 ... " command.
3435 */
3436 void
3437ex_unlet(eap)
3438 exarg_T *eap;
3439{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003440 ex_unletlock(eap, eap->arg, 0);
3441}
3442
3443/*
3444 * ":lockvar" and ":unlockvar" commands
3445 */
3446 void
3447ex_lockvar(eap)
3448 exarg_T *eap;
3449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003451 int deep = 2;
3452
3453 if (eap->forceit)
3454 deep = -1;
3455 else if (vim_isdigit(*arg))
3456 {
3457 deep = getdigits(&arg);
3458 arg = skipwhite(arg);
3459 }
3460
3461 ex_unletlock(eap, arg, deep);
3462}
3463
3464/*
3465 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3466 */
3467 static void
3468ex_unletlock(eap, argstart, deep)
3469 exarg_T *eap;
3470 char_u *argstart;
3471 int deep;
3472{
3473 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003476 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 do
3479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003480 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003481 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3482 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003483 if (lv.ll_name == NULL)
3484 error = TRUE; /* error but continue parsing */
3485 if (name_end == NULL || (!vim_iswhite(*name_end)
3486 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003488 if (name_end != NULL)
3489 {
3490 emsg_severe = TRUE;
3491 EMSG(_(e_trailing));
3492 }
3493 if (!(eap->skip || error))
3494 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 break;
3496 }
3497
3498 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003499 {
3500 if (eap->cmdidx == CMD_unlet)
3501 {
3502 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3503 error = TRUE;
3504 }
3505 else
3506 {
3507 if (do_lock_var(&lv, name_end, deep,
3508 eap->cmdidx == CMD_lockvar) == FAIL)
3509 error = TRUE;
3510 }
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003513 if (!eap->skip)
3514 clear_lval(&lv);
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 arg = skipwhite(name_end);
3517 } while (!ends_excmd(*arg));
3518
3519 eap->nextcmd = check_nextcmd(arg);
3520}
3521
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003523do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003525 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 int forceit;
3527{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528 int ret = OK;
3529 int cc;
3530
3531 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003533 cc = *name_end;
3534 *name_end = NUL;
3535
3536 /* Normal name or expanded name. */
3537 if (check_changedtick(lp->ll_name))
3538 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003539 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003541 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003543 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3544 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 else if (lp->ll_range)
3546 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003548
3549 /* Delete a range of List items. */
3550 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3551 {
3552 li = lp->ll_li->li_next;
3553 listitem_remove(lp->ll_list, lp->ll_li);
3554 lp->ll_li = li;
3555 ++lp->ll_n1;
3556 }
3557 }
3558 else
3559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 /* unlet a List item. */
3562 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003565 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 }
3567
3568 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569}
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571/*
3572 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 */
3575 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 hashtab_T *ht;
3581 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003582 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003583 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 ht = find_var_ht(name, &varname);
3586 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003588 hi = hash_find(ht, varname);
3589 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003590 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003591 di = HI2DI(hi);
3592 if (var_check_fixed(di->di_flags, name)
3593 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 return FAIL;
3595 delete_var(ht, hi);
3596 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 if (forceit)
3600 return OK;
3601 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 return FAIL;
3603}
3604
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605/*
3606 * Lock or unlock variable indicated by "lp".
3607 * "deep" is the levels to go (-1 for unlimited);
3608 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3609 */
3610 static int
3611do_lock_var(lp, name_end, deep, lock)
3612 lval_T *lp;
3613 char_u *name_end;
3614 int deep;
3615 int lock;
3616{
3617 int ret = OK;
3618 int cc;
3619 dictitem_T *di;
3620
3621 if (deep == 0) /* nothing to do */
3622 return OK;
3623
3624 if (lp->ll_tv == NULL)
3625 {
3626 cc = *name_end;
3627 *name_end = NUL;
3628
3629 /* Normal name or expanded name. */
3630 if (check_changedtick(lp->ll_name))
3631 ret = FAIL;
3632 else
3633 {
3634 di = find_var(lp->ll_name, NULL);
3635 if (di == NULL)
3636 ret = FAIL;
3637 else
3638 {
3639 if (lock)
3640 di->di_flags |= DI_FLAGS_LOCK;
3641 else
3642 di->di_flags &= ~DI_FLAGS_LOCK;
3643 item_lock(&di->di_tv, deep, lock);
3644 }
3645 }
3646 *name_end = cc;
3647 }
3648 else if (lp->ll_range)
3649 {
3650 listitem_T *li = lp->ll_li;
3651
3652 /* (un)lock a range of List items. */
3653 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3654 {
3655 item_lock(&li->li_tv, deep, lock);
3656 li = li->li_next;
3657 ++lp->ll_n1;
3658 }
3659 }
3660 else if (lp->ll_list != NULL)
3661 /* (un)lock a List item. */
3662 item_lock(&lp->ll_li->li_tv, deep, lock);
3663 else
3664 /* un(lock) a Dictionary item. */
3665 item_lock(&lp->ll_di->di_tv, deep, lock);
3666
3667 return ret;
3668}
3669
3670/*
3671 * Lock or unlock an item. "deep" is nr of levels to go.
3672 */
3673 static void
3674item_lock(tv, deep, lock)
3675 typval_T *tv;
3676 int deep;
3677 int lock;
3678{
3679 static int recurse = 0;
3680 list_T *l;
3681 listitem_T *li;
3682 dict_T *d;
3683 hashitem_T *hi;
3684 int todo;
3685
3686 if (recurse >= DICT_MAXNEST)
3687 {
3688 EMSG(_("E743: variable nested too deep for (un)lock"));
3689 return;
3690 }
3691 if (deep == 0)
3692 return;
3693 ++recurse;
3694
3695 /* lock/unlock the item itself */
3696 if (lock)
3697 tv->v_lock |= VAR_LOCKED;
3698 else
3699 tv->v_lock &= ~VAR_LOCKED;
3700
3701 switch (tv->v_type)
3702 {
3703 case VAR_LIST:
3704 if ((l = tv->vval.v_list) != NULL)
3705 {
3706 if (lock)
3707 l->lv_lock |= VAR_LOCKED;
3708 else
3709 l->lv_lock &= ~VAR_LOCKED;
3710 if (deep < 0 || deep > 1)
3711 /* recursive: lock/unlock the items the List contains */
3712 for (li = l->lv_first; li != NULL; li = li->li_next)
3713 item_lock(&li->li_tv, deep - 1, lock);
3714 }
3715 break;
3716 case VAR_DICT:
3717 if ((d = tv->vval.v_dict) != NULL)
3718 {
3719 if (lock)
3720 d->dv_lock |= VAR_LOCKED;
3721 else
3722 d->dv_lock &= ~VAR_LOCKED;
3723 if (deep < 0 || deep > 1)
3724 {
3725 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003726 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3728 {
3729 if (!HASHITEM_EMPTY(hi))
3730 {
3731 --todo;
3732 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3733 }
3734 }
3735 }
3736 }
3737 }
3738 --recurse;
3739}
3740
Bram Moolenaara40058a2005-07-11 22:42:07 +00003741/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003742 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3743 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003744 */
3745 static int
3746tv_islocked(tv)
3747 typval_T *tv;
3748{
3749 return (tv->v_lock & VAR_LOCKED)
3750 || (tv->v_type == VAR_LIST
3751 && tv->vval.v_list != NULL
3752 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3753 || (tv->v_type == VAR_DICT
3754 && tv->vval.v_dict != NULL
3755 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3756}
3757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3759/*
3760 * Delete all "menutrans_" variables.
3761 */
3762 void
3763del_menutrans_vars()
3764{
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003769 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 {
3772 if (!HASHITEM_EMPTY(hi))
3773 {
3774 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3776 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003777 }
3778 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780}
3781#endif
3782
3783#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3784
3785/*
3786 * Local string buffer for the next two functions to store a variable name
3787 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3788 * get_user_var_name().
3789 */
3790
3791static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3792
3793static char_u *varnamebuf = NULL;
3794static int varnamebuflen = 0;
3795
3796/*
3797 * Function to concatenate a prefix and a variable name.
3798 */
3799 static char_u *
3800cat_prefix_varname(prefix, name)
3801 int prefix;
3802 char_u *name;
3803{
3804 int len;
3805
3806 len = (int)STRLEN(name) + 3;
3807 if (len > varnamebuflen)
3808 {
3809 vim_free(varnamebuf);
3810 len += 10; /* some additional space */
3811 varnamebuf = alloc(len);
3812 if (varnamebuf == NULL)
3813 {
3814 varnamebuflen = 0;
3815 return NULL;
3816 }
3817 varnamebuflen = len;
3818 }
3819 *varnamebuf = prefix;
3820 varnamebuf[1] = ':';
3821 STRCPY(varnamebuf + 2, name);
3822 return varnamebuf;
3823}
3824
3825/*
3826 * Function given to ExpandGeneric() to obtain the list of user defined
3827 * (global/buffer/window/built-in) variable names.
3828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 char_u *
3830get_user_var_name(xp, idx)
3831 expand_T *xp;
3832 int idx;
3833{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003834 static long_u gdone;
3835 static long_u bdone;
3836 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003837#ifdef FEAT_WINDOWS
3838 static long_u tdone;
3839#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003840 static int vidx;
3841 static hashitem_T *hi;
3842 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003845 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003847#ifdef FEAT_WINDOWS
3848 tdone = 0;
3849#endif
3850 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003851
3852 /* Global variables */
3853 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003857 else
3858 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003859 while (HASHITEM_EMPTY(hi))
3860 ++hi;
3861 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3862 return cat_prefix_varname('g', hi->hi_key);
3863 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003865
3866 /* b: variables */
3867 ht = &curbuf->b_vars.dv_hashtab;
3868 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003872 else
3873 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003874 while (HASHITEM_EMPTY(hi))
3875 ++hi;
3876 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003878 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003880 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return (char_u *)"b:changedtick";
3882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003883
3884 /* w: variables */
3885 ht = &curwin->w_vars.dv_hashtab;
3886 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003888 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003890 else
3891 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003892 while (HASHITEM_EMPTY(hi))
3893 ++hi;
3894 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003896
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003897#ifdef FEAT_WINDOWS
3898 /* t: variables */
3899 ht = &curtab->tp_vars.dv_hashtab;
3900 if (tdone < ht->ht_used)
3901 {
3902 if (tdone++ == 0)
3903 hi = ht->ht_array;
3904 else
3905 ++hi;
3906 while (HASHITEM_EMPTY(hi))
3907 ++hi;
3908 return cat_prefix_varname('t', hi->hi_key);
3909 }
3910#endif
3911
Bram Moolenaar33570922005-01-25 22:26:29 +00003912 /* v: variables */
3913 if (vidx < VV_LEN)
3914 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 vim_free(varnamebuf);
3917 varnamebuf = NULL;
3918 varnamebuflen = 0;
3919 return NULL;
3920}
3921
3922#endif /* FEAT_CMDL_COMPL */
3923
3924/*
3925 * types for expressions.
3926 */
3927typedef enum
3928{
3929 TYPE_UNKNOWN = 0
3930 , TYPE_EQUAL /* == */
3931 , TYPE_NEQUAL /* != */
3932 , TYPE_GREATER /* > */
3933 , TYPE_GEQUAL /* >= */
3934 , TYPE_SMALLER /* < */
3935 , TYPE_SEQUAL /* <= */
3936 , TYPE_MATCH /* =~ */
3937 , TYPE_NOMATCH /* !~ */
3938} exptype_T;
3939
3940/*
3941 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3944 */
3945
3946/*
3947 * Handle zero level expression.
3948 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003950 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 * Return OK or FAIL.
3952 */
3953 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 char_u **nextcmd;
3958 int evaluate;
3959{
3960 int ret;
3961 char_u *p;
3962
3963 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (ret == FAIL || !ends_excmd(*p))
3966 {
3967 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 /*
3970 * Report the invalid expression unless the expression evaluation has
3971 * been cancelled due to an aborting error, an interrupt, or an
3972 * exception.
3973 */
3974 if (!aborting())
3975 EMSG2(_(e_invexpr2), arg);
3976 ret = FAIL;
3977 }
3978 if (nextcmd != NULL)
3979 *nextcmd = check_nextcmd(p);
3980
3981 return ret;
3982}
3983
3984/*
3985 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003986 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 *
3988 * "arg" must point to the first non-white of the expression.
3989 * "arg" is advanced to the next non-white after the recognized expression.
3990 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003991 * Note: "rettv.v_lock" is not set.
3992 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * Return OK or FAIL.
3994 */
3995 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int evaluate;
4000{
4001 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 /*
4005 * Get the first variable.
4006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return FAIL;
4009
4010 if ((*arg)[0] == '?')
4011 {
4012 result = FALSE;
4013 if (evaluate)
4014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 int error = FALSE;
4016
4017 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 if (error)
4021 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023
4024 /*
4025 * Get the second variable.
4026 */
4027 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030
4031 /*
4032 * Check for the ":".
4033 */
4034 if ((*arg)[0] != ':')
4035 {
4036 EMSG(_("E109: Missing ':' after '?'"));
4037 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 return FAIL;
4040 }
4041
4042 /*
4043 * Get the third variable.
4044 */
4045 *arg = skipwhite(*arg + 1);
4046 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4047 {
4048 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return FAIL;
4051 }
4052 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055
4056 return OK;
4057}
4058
4059/*
4060 * Handle first level expression:
4061 * expr2 || expr2 || expr2 logical OR
4062 *
4063 * "arg" must point to the first non-white of the expression.
4064 * "arg" is advanced to the next non-white after the recognized expression.
4065 *
4066 * Return OK or FAIL.
4067 */
4068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int evaluate;
4073{
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 long result;
4076 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 /*
4080 * Get the first variable.
4081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return FAIL;
4084
4085 /*
4086 * Repeat until there is no following "||".
4087 */
4088 first = TRUE;
4089 result = FALSE;
4090 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4091 {
4092 if (evaluate && first)
4093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004097 if (error)
4098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 first = FALSE;
4100 }
4101
4102 /*
4103 * Get the second variable.
4104 */
4105 *arg = skipwhite(*arg + 2);
4106 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4107 return FAIL;
4108
4109 /*
4110 * Compute the result.
4111 */
4112 if (evaluate && !result)
4113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117 if (error)
4118 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 if (evaluate)
4121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 rettv->v_type = VAR_NUMBER;
4123 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 }
4126
4127 return OK;
4128}
4129
4130/*
4131 * Handle second level expression:
4132 * expr3 && expr3 && expr3 logical AND
4133 *
4134 * "arg" must point to the first non-white of the expression.
4135 * "arg" is advanced to the next non-white after the recognized expression.
4136 *
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 long result;
4147 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 /*
4151 * Get the first variable.
4152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 return FAIL;
4155
4156 /*
4157 * Repeat until there is no following "&&".
4158 */
4159 first = TRUE;
4160 result = TRUE;
4161 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4162 {
4163 if (evaluate && first)
4164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (error)
4169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 first = FALSE;
4171 }
4172
4173 /*
4174 * Get the second variable.
4175 */
4176 *arg = skipwhite(*arg + 2);
4177 if (eval4(arg, &var2, evaluate && result) == FAIL)
4178 return FAIL;
4179
4180 /*
4181 * Compute the result.
4182 */
4183 if (evaluate && result)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 if (evaluate)
4192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 rettv->v_type = VAR_NUMBER;
4194 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
4197
4198 return OK;
4199}
4200
4201/*
4202 * Handle third level expression:
4203 * var1 == var2
4204 * var1 =~ var2
4205 * var1 != var2
4206 * var1 !~ var2
4207 * var1 > var2
4208 * var1 >= var2
4209 * var1 < var2
4210 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004211 * var1 is var2
4212 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 *
4214 * "arg" must point to the first non-white of the expression.
4215 * "arg" is advanced to the next non-white after the recognized expression.
4216 *
4217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 int evaluate;
4224{
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u *p;
4227 int i;
4228 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004229 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 int len = 2;
4231 long n1, n2;
4232 char_u *s1, *s2;
4233 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4234 regmatch_T regmatch;
4235 int ic;
4236 char_u *save_cpo;
4237
4238 /*
4239 * Get the first variable.
4240 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 p = *arg;
4245 switch (p[0])
4246 {
4247 case '=': if (p[1] == '=')
4248 type = TYPE_EQUAL;
4249 else if (p[1] == '~')
4250 type = TYPE_MATCH;
4251 break;
4252 case '!': if (p[1] == '=')
4253 type = TYPE_NEQUAL;
4254 else if (p[1] == '~')
4255 type = TYPE_NOMATCH;
4256 break;
4257 case '>': if (p[1] != '=')
4258 {
4259 type = TYPE_GREATER;
4260 len = 1;
4261 }
4262 else
4263 type = TYPE_GEQUAL;
4264 break;
4265 case '<': if (p[1] != '=')
4266 {
4267 type = TYPE_SMALLER;
4268 len = 1;
4269 }
4270 else
4271 type = TYPE_SEQUAL;
4272 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004273 case 'i': if (p[1] == 's')
4274 {
4275 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4276 len = 5;
4277 if (!vim_isIDc(p[len]))
4278 {
4279 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4280 type_is = TRUE;
4281 }
4282 }
4283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004287 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 */
4289 if (type != TYPE_UNKNOWN)
4290 {
4291 /* extra question mark appended: ignore case */
4292 if (p[len] == '?')
4293 {
4294 ic = TRUE;
4295 ++len;
4296 }
4297 /* extra '#' appended: match case */
4298 else if (p[len] == '#')
4299 {
4300 ic = FALSE;
4301 ++len;
4302 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004303 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 else
4305 ic = p_ic;
4306
4307 /*
4308 * Get the second variable.
4309 */
4310 *arg = skipwhite(p + len);
4311 if (eval5(arg, &var2, evaluate) == FAIL)
4312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315 }
4316
4317 if (evaluate)
4318 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004319 if (type_is && rettv->v_type != var2.v_type)
4320 {
4321 /* For "is" a different type always means FALSE, for "notis"
4322 * it means TRUE. */
4323 n1 = (type == TYPE_NEQUAL);
4324 }
4325 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4326 {
4327 if (type_is)
4328 {
4329 n1 = (rettv->v_type == var2.v_type
4330 && rettv->vval.v_list == var2.vval.v_list);
4331 if (type == TYPE_NEQUAL)
4332 n1 = !n1;
4333 }
4334 else if (rettv->v_type != var2.v_type
4335 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4336 {
4337 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004338 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004340 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004341 clear_tv(rettv);
4342 clear_tv(&var2);
4343 return FAIL;
4344 }
4345 else
4346 {
4347 /* Compare two Lists for being equal or unequal. */
4348 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4349 if (type == TYPE_NEQUAL)
4350 n1 = !n1;
4351 }
4352 }
4353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004354 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4355 {
4356 if (type_is)
4357 {
4358 n1 = (rettv->v_type == var2.v_type
4359 && rettv->vval.v_dict == var2.vval.v_dict);
4360 if (type == TYPE_NEQUAL)
4361 n1 = !n1;
4362 }
4363 else if (rettv->v_type != var2.v_type
4364 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4365 {
4366 if (rettv->v_type != var2.v_type)
4367 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4368 else
4369 EMSG(_("E736: Invalid operation for Dictionary"));
4370 clear_tv(rettv);
4371 clear_tv(&var2);
4372 return FAIL;
4373 }
4374 else
4375 {
4376 /* Compare two Dictionaries for being equal or unequal. */
4377 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4378 if (type == TYPE_NEQUAL)
4379 n1 = !n1;
4380 }
4381 }
4382
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4384 {
4385 if (rettv->v_type != var2.v_type
4386 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4387 {
4388 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004389 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004391 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 clear_tv(rettv);
4393 clear_tv(&var2);
4394 return FAIL;
4395 }
4396 else
4397 {
4398 /* Compare two Funcrefs for being equal or unequal. */
4399 if (rettv->vval.v_string == NULL
4400 || var2.vval.v_string == NULL)
4401 n1 = FALSE;
4402 else
4403 n1 = STRCMP(rettv->vval.v_string,
4404 var2.vval.v_string) == 0;
4405 if (type == TYPE_NEQUAL)
4406 n1 = !n1;
4407 }
4408 }
4409
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004410#ifdef FEAT_FLOAT
4411 /*
4412 * If one of the two variables is a float, compare as a float.
4413 * When using "=~" or "!~", always compare as string.
4414 */
4415 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4416 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4417 {
4418 float_T f1, f2;
4419
4420 if (rettv->v_type == VAR_FLOAT)
4421 f1 = rettv->vval.v_float;
4422 else
4423 f1 = get_tv_number(rettv);
4424 if (var2.v_type == VAR_FLOAT)
4425 f2 = var2.vval.v_float;
4426 else
4427 f2 = get_tv_number(&var2);
4428 n1 = FALSE;
4429 switch (type)
4430 {
4431 case TYPE_EQUAL: n1 = (f1 == f2); break;
4432 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4433 case TYPE_GREATER: n1 = (f1 > f2); break;
4434 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4435 case TYPE_SMALLER: n1 = (f1 < f2); break;
4436 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4437 case TYPE_UNKNOWN:
4438 case TYPE_MATCH:
4439 case TYPE_NOMATCH: break; /* avoid gcc warning */
4440 }
4441 }
4442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 /*
4445 * If one of the two variables is a number, compare as a number.
4446 * When using "=~" or "!~", always compare as string.
4447 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 n1 = get_tv_number(rettv);
4452 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 switch (type)
4454 {
4455 case TYPE_EQUAL: n1 = (n1 == n2); break;
4456 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4457 case TYPE_GREATER: n1 = (n1 > n2); break;
4458 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4459 case TYPE_SMALLER: n1 = (n1 < n2); break;
4460 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4461 case TYPE_UNKNOWN:
4462 case TYPE_MATCH:
4463 case TYPE_NOMATCH: break; /* avoid gcc warning */
4464 }
4465 }
4466 else
4467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 s1 = get_tv_string_buf(rettv, buf1);
4469 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4471 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4472 else
4473 i = 0;
4474 n1 = FALSE;
4475 switch (type)
4476 {
4477 case TYPE_EQUAL: n1 = (i == 0); break;
4478 case TYPE_NEQUAL: n1 = (i != 0); break;
4479 case TYPE_GREATER: n1 = (i > 0); break;
4480 case TYPE_GEQUAL: n1 = (i >= 0); break;
4481 case TYPE_SMALLER: n1 = (i < 0); break;
4482 case TYPE_SEQUAL: n1 = (i <= 0); break;
4483
4484 case TYPE_MATCH:
4485 case TYPE_NOMATCH:
4486 /* avoid 'l' flag in 'cpoptions' */
4487 save_cpo = p_cpo;
4488 p_cpo = (char_u *)"";
4489 regmatch.regprog = vim_regcomp(s2,
4490 RE_MAGIC + RE_STRING);
4491 regmatch.rm_ic = ic;
4492 if (regmatch.regprog != NULL)
4493 {
4494 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4495 vim_free(regmatch.regprog);
4496 if (type == TYPE_NOMATCH)
4497 n1 = !n1;
4498 }
4499 p_cpo = save_cpo;
4500 break;
4501
4502 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4503 }
4504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 clear_tv(rettv);
4506 clear_tv(&var2);
4507 rettv->v_type = VAR_NUMBER;
4508 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 }
4511
4512 return OK;
4513}
4514
4515/*
4516 * Handle fourth level expression:
4517 * + number addition
4518 * - number subtraction
4519 * . string concatenation
4520 *
4521 * "arg" must point to the first non-white of the expression.
4522 * "arg" is advanced to the next non-white after the recognized expression.
4523 *
4524 * Return OK or FAIL.
4525 */
4526 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 int evaluate;
4531{
Bram Moolenaar33570922005-01-25 22:26:29 +00004532 typval_T var2;
4533 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 int op;
4535 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536#ifdef FEAT_FLOAT
4537 float_T f1 = 0, f2 = 0;
4538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 char_u *s1, *s2;
4540 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4541 char_u *p;
4542
4543 /*
4544 * Get the first variable.
4545 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004546 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 return FAIL;
4548
4549 /*
4550 * Repeat computing, until no '+', '-' or '.' is following.
4551 */
4552 for (;;)
4553 {
4554 op = **arg;
4555 if (op != '+' && op != '-' && op != '.')
4556 break;
4557
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004558 if ((op != '+' || rettv->v_type != VAR_LIST)
4559#ifdef FEAT_FLOAT
4560 && (op == '.' || rettv->v_type != VAR_FLOAT)
4561#endif
4562 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 {
4564 /* For "list + ...", an illegal use of the first operand as
4565 * a number cannot be determined before evaluating the 2nd
4566 * operand: if this is also a list, all is ok.
4567 * For "something . ...", "something - ..." or "non-list + ...",
4568 * we know that the first operand needs to be a string or number
4569 * without evaluating the 2nd operand. So check before to avoid
4570 * side effects after an error. */
4571 if (evaluate && get_tv_string_chk(rettv) == NULL)
4572 {
4573 clear_tv(rettv);
4574 return FAIL;
4575 }
4576 }
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /*
4579 * Get the second variable.
4580 */
4581 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004582 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 return FAIL;
4586 }
4587
4588 if (evaluate)
4589 {
4590 /*
4591 * Compute the result.
4592 */
4593 if (op == '.')
4594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4596 s2 = get_tv_string_buf_chk(&var2, buf2);
4597 if (s2 == NULL) /* type error ? */
4598 {
4599 clear_tv(rettv);
4600 clear_tv(&var2);
4601 return FAIL;
4602 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004603 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 clear_tv(rettv);
4605 rettv->v_type = VAR_STRING;
4606 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004608 else if (op == '+' && rettv->v_type == VAR_LIST
4609 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004610 {
4611 /* concatenate Lists */
4612 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4613 &var3) == FAIL)
4614 {
4615 clear_tv(rettv);
4616 clear_tv(&var2);
4617 return FAIL;
4618 }
4619 clear_tv(rettv);
4620 *rettv = var3;
4621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 else
4623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 int error = FALSE;
4625
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004626#ifdef FEAT_FLOAT
4627 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629 f1 = rettv->vval.v_float;
4630 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 else
4633#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635 n1 = get_tv_number_chk(rettv, &error);
4636 if (error)
4637 {
4638 /* This can only happen for "list + non-list". For
4639 * "non-list + ..." or "something - ...", we returned
4640 * before evaluating the 2nd operand. */
4641 clear_tv(rettv);
4642 return FAIL;
4643 }
4644#ifdef FEAT_FLOAT
4645 if (var2.v_type == VAR_FLOAT)
4646 f1 = n1;
4647#endif
4648 }
4649#ifdef FEAT_FLOAT
4650 if (var2.v_type == VAR_FLOAT)
4651 {
4652 f2 = var2.vval.v_float;
4653 n2 = 0;
4654 }
4655 else
4656#endif
4657 {
4658 n2 = get_tv_number_chk(&var2, &error);
4659 if (error)
4660 {
4661 clear_tv(rettv);
4662 clear_tv(&var2);
4663 return FAIL;
4664 }
4665#ifdef FEAT_FLOAT
4666 if (rettv->v_type == VAR_FLOAT)
4667 f2 = n2;
4668#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004671
4672#ifdef FEAT_FLOAT
4673 /* If there is a float on either side the result is a float. */
4674 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4675 {
4676 if (op == '+')
4677 f1 = f1 + f2;
4678 else
4679 f1 = f1 - f2;
4680 rettv->v_type = VAR_FLOAT;
4681 rettv->vval.v_float = f1;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684#endif
4685 {
4686 if (op == '+')
4687 n1 = n1 + n2;
4688 else
4689 n1 = n1 - n2;
4690 rettv->v_type = VAR_NUMBER;
4691 rettv->vval.v_number = n1;
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 }
4697 return OK;
4698}
4699
4700/*
4701 * Handle fifth level expression:
4702 * * number multiplication
4703 * / number division
4704 * % number modulo
4705 *
4706 * "arg" must point to the first non-white of the expression.
4707 * "arg" is advanced to the next non-white after the recognized expression.
4708 *
4709 * Return OK or FAIL.
4710 */
4711 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004716 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int op;
4720 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721#ifdef FEAT_FLOAT
4722 int use_float = FALSE;
4723 float_T f1 = 0, f2;
4724#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Get the first variable.
4729 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004730 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 return FAIL;
4732
4733 /*
4734 * Repeat computing, until no '*', '/' or '%' is following.
4735 */
4736 for (;;)
4737 {
4738 op = **arg;
4739 if (op != '*' && op != '/' && op != '%')
4740 break;
4741
4742 if (evaluate)
4743 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744#ifdef FEAT_FLOAT
4745 if (rettv->v_type == VAR_FLOAT)
4746 {
4747 f1 = rettv->vval.v_float;
4748 use_float = TRUE;
4749 n1 = 0;
4750 }
4751 else
4752#endif
4753 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 if (error)
4756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758 else
4759 n1 = 0;
4760
4761 /*
4762 * Get the second variable.
4763 */
4764 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004765 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 return FAIL;
4767
4768 if (evaluate)
4769 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 if (var2.v_type == VAR_FLOAT)
4772 {
4773 if (!use_float)
4774 {
4775 f1 = n1;
4776 use_float = TRUE;
4777 }
4778 f2 = var2.vval.v_float;
4779 n2 = 0;
4780 }
4781 else
4782#endif
4783 {
4784 n2 = get_tv_number_chk(&var2, &error);
4785 clear_tv(&var2);
4786 if (error)
4787 return FAIL;
4788#ifdef FEAT_FLOAT
4789 if (use_float)
4790 f2 = n2;
4791#endif
4792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 /*
4795 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798#ifdef FEAT_FLOAT
4799 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 if (op == '*')
4802 f1 = f1 * f2;
4803 else if (op == '/')
4804 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004805# ifdef VMS
4806 /* VMS crashes on divide by zero, work around it */
4807 if (f2 == 0.0)
4808 {
4809 if (f1 == 0)
4810 f1 = -0x7fffffffL - 1L; /* similar to NaN */
4811 else if (f1 < 0)
4812 f1 = -0x7fffffffL;
4813 else
4814 f1 = 0x7fffffffL;
4815 }
4816 else
4817 f1 = f1 / f2;
4818# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819 /* We rely on the floating point library to handle divide
4820 * by zero to result in "inf" and not a crash. */
4821 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004822# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004826 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 return FAIL;
4828 }
4829 rettv->v_type = VAR_FLOAT;
4830 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835 if (op == '*')
4836 n1 = n1 * n2;
4837 else if (op == '/')
4838 {
4839 if (n2 == 0) /* give an error message? */
4840 {
4841 if (n1 == 0)
4842 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4843 else if (n1 < 0)
4844 n1 = -0x7fffffffL;
4845 else
4846 n1 = 0x7fffffffL;
4847 }
4848 else
4849 n1 = n1 / n2;
4850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852 {
4853 if (n2 == 0) /* give an error message? */
4854 n1 = 0;
4855 else
4856 n1 = n1 % n2;
4857 }
4858 rettv->v_type = VAR_NUMBER;
4859 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862 }
4863
4864 return OK;
4865}
4866
4867/*
4868 * Handle sixth level expression:
4869 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004870 * "string" string constant
4871 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 * &option-name option value
4873 * @r register contents
4874 * identifier variable value
4875 * function() function call
4876 * $VAR environment variable
4877 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004878 * [expr, expr] List
4879 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 *
4881 * Also handle:
4882 * ! in front logical NOT
4883 * - in front unary minus
4884 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004885 * trailing [] subscript in String or List
4886 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 *
4888 * "arg" must point to the first non-white of the expression.
4889 * "arg" is advanced to the next non-white after the recognized expression.
4890 *
4891 * Return OK or FAIL.
4892 */
4893 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004894eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004898 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 long n;
4901 int len;
4902 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 char_u *start_leader, *end_leader;
4904 int ret = OK;
4905 char_u *alias;
4906
4907 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913 /*
4914 * Skip '!' and '-' characters. They are handled later.
4915 */
4916 start_leader = *arg;
4917 while (**arg == '!' || **arg == '-' || **arg == '+')
4918 *arg = skipwhite(*arg + 1);
4919 end_leader = *arg;
4920
4921 switch (**arg)
4922 {
4923 /*
4924 * Number constant.
4925 */
4926 case '0':
4927 case '1':
4928 case '2':
4929 case '3':
4930 case '4':
4931 case '5':
4932 case '6':
4933 case '7':
4934 case '8':
4935 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936 {
4937#ifdef FEAT_FLOAT
4938 char_u *p = skipdigits(*arg + 1);
4939 int get_float = FALSE;
4940
4941 /* We accept a float when the format matches
4942 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004943 * strict to avoid backwards compatibility problems.
4944 * Don't look for a float after the "." operator, so that
4945 * ":let vers = 1.2.3" doesn't fail. */
4946 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 get_float = TRUE;
4949 p = skipdigits(p + 2);
4950 if (*p == 'e' || *p == 'E')
4951 {
4952 ++p;
4953 if (*p == '-' || *p == '+')
4954 ++p;
4955 if (!vim_isdigit(*p))
4956 get_float = FALSE;
4957 else
4958 p = skipdigits(p + 1);
4959 }
4960 if (ASCII_ISALPHA(*p) || *p == '.')
4961 get_float = FALSE;
4962 }
4963 if (get_float)
4964 {
4965 float_T f;
4966
4967 *arg += string2float(*arg, &f);
4968 if (evaluate)
4969 {
4970 rettv->v_type = VAR_FLOAT;
4971 rettv->vval.v_float = f;
4972 }
4973 }
4974 else
4975#endif
4976 {
4977 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4978 *arg += len;
4979 if (evaluate)
4980 {
4981 rettv->v_type = VAR_NUMBER;
4982 rettv->vval.v_number = n;
4983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /*
4989 * String constant: "string".
4990 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 break;
4993
4994 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004998 break;
4999
5000 /*
5001 * List: [expr, expr]
5002 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 break;
5005
5006 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 * Dictionary: {key: val, key: val}
5008 */
5009 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5010 break;
5011
5012 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005013 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005015 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
5017
5018 /*
5019 * Environment variable: $VAR.
5020 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023
5024 /*
5025 * Register contents: @r.
5026 */
5027 case '@': ++*arg;
5028 if (evaluate)
5029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005030 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005031 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 if (**arg != NUL)
5034 ++*arg;
5035 break;
5036
5037 /*
5038 * nested expression: (expression).
5039 */
5040 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (**arg == ')')
5043 ++*arg;
5044 else if (ret == OK)
5045 {
5046 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 ret = FAIL;
5049 }
5050 break;
5051
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055
5056 if (ret == NOTDONE)
5057 {
5058 /*
5059 * Must be a variable or function name.
5060 * Can also be a curly-braces kind of name: {expr}.
5061 */
5062 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005063 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 if (alias != NULL)
5065 s = alias;
5066
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005067 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 ret = FAIL;
5069 else
5070 {
5071 if (**arg == '(') /* recursive! */
5072 {
5073 /* If "s" is the name of a variable of type VAR_FUNC
5074 * use its contents. */
5075 s = deref_func_name(s, &len);
5076
5077 /* Invoke the function. */
5078 ret = get_func_tv(s, len, rettv, arg,
5079 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005080 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 /* Stop the expression evaluation when immediately
5082 * aborting on error, or when an interrupt occurred or
5083 * an exception was thrown but not caught. */
5084 if (aborting())
5085 {
5086 if (ret == OK)
5087 clear_tv(rettv);
5088 ret = FAIL;
5089 }
5090 }
5091 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005092 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005093 else
5094 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 }
5096
5097 if (alias != NULL)
5098 vim_free(alias);
5099 }
5100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 *arg = skipwhite(*arg);
5102
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005103 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5104 * expr(expr). */
5105 if (ret == OK)
5106 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
5109 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5110 */
5111 if (ret == OK && evaluate && end_leader > start_leader)
5112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005113 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114 int val = 0;
5115#ifdef FEAT_FLOAT
5116 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005117
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 if (rettv->v_type == VAR_FLOAT)
5119 f = rettv->vval.v_float;
5120 else
5121#endif
5122 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005123 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005125 clear_tv(rettv);
5126 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128 else
5129 {
5130 while (end_leader > start_leader)
5131 {
5132 --end_leader;
5133 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005134 {
5135#ifdef FEAT_FLOAT
5136 if (rettv->v_type == VAR_FLOAT)
5137 f = !f;
5138 else
5139#endif
5140 val = !val;
5141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005142 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 {
5144#ifdef FEAT_FLOAT
5145 if (rettv->v_type == VAR_FLOAT)
5146 f = -f;
5147 else
5148#endif
5149 val = -val;
5150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005151 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152#ifdef FEAT_FLOAT
5153 if (rettv->v_type == VAR_FLOAT)
5154 {
5155 clear_tv(rettv);
5156 rettv->vval.v_float = f;
5157 }
5158 else
5159#endif
5160 {
5161 clear_tv(rettv);
5162 rettv->v_type = VAR_NUMBER;
5163 rettv->vval.v_number = val;
5164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167
5168 return ret;
5169}
5170
5171/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005172 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5173 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5175 */
5176 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005179 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182{
5183 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005184 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 long len = -1;
5187 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005191 if (rettv->v_type == VAR_FUNC
5192#ifdef FEAT_FLOAT
5193 || rettv->v_type == VAR_FLOAT
5194#endif
5195 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005197 if (verbose)
5198 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 return FAIL;
5200 }
5201
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 /*
5205 * dict.name
5206 */
5207 key = *arg + 1;
5208 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5209 ;
5210 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213 }
5214 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 /*
5217 * something[idx]
5218 *
5219 * Get the (first) variable from inside the [].
5220 */
5221 *arg = skipwhite(*arg + 1);
5222 if (**arg == ':')
5223 empty1 = TRUE;
5224 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5225 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5227 {
5228 /* not a number or string */
5229 clear_tv(&var1);
5230 return FAIL;
5231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 /*
5234 * Get the second variable from inside the [:].
5235 */
5236 if (**arg == ':')
5237 {
5238 range = TRUE;
5239 *arg = skipwhite(*arg + 1);
5240 if (**arg == ']')
5241 empty2 = TRUE;
5242 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 if (!empty1)
5245 clear_tv(&var1);
5246 return FAIL;
5247 }
5248 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5249 {
5250 /* not a number or string */
5251 if (!empty1)
5252 clear_tv(&var1);
5253 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 return FAIL;
5255 }
5256 }
5257
5258 /* Check for the ']'. */
5259 if (**arg != ']')
5260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261 if (verbose)
5262 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 clear_tv(&var1);
5264 if (range)
5265 clear_tv(&var2);
5266 return FAIL;
5267 }
5268 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 }
5270
5271 if (evaluate)
5272 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 n1 = 0;
5274 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005276 n1 = get_tv_number(&var1);
5277 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 }
5279 if (range)
5280 {
5281 if (empty2)
5282 n2 = -1;
5283 else
5284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005285 n2 = get_tv_number(&var2);
5286 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 }
5288 }
5289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 {
5292 case VAR_NUMBER:
5293 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 len = (long)STRLEN(s);
5296 if (range)
5297 {
5298 /* The resulting variable is a substring. If the indexes
5299 * are out of range the result is empty. */
5300 if (n1 < 0)
5301 {
5302 n1 = len + n1;
5303 if (n1 < 0)
5304 n1 = 0;
5305 }
5306 if (n2 < 0)
5307 n2 = len + n2;
5308 else if (n2 >= len)
5309 n2 = len;
5310 if (n1 >= len || n2 < 0 || n1 > n2)
5311 s = NULL;
5312 else
5313 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5314 }
5315 else
5316 {
5317 /* The resulting variable is a string of a single
5318 * character. If the index is too big or negative the
5319 * result is empty. */
5320 if (n1 >= len || n1 < 0)
5321 s = NULL;
5322 else
5323 s = vim_strnsave(s + n1, 1);
5324 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005325 clear_tv(rettv);
5326 rettv->v_type = VAR_STRING;
5327 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 break;
5329
5330 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 if (n1 < 0)
5333 n1 = len + n1;
5334 if (!empty1 && (n1 < 0 || n1 >= len))
5335 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005336 /* For a range we allow invalid values and return an empty
5337 * list. A list index out of range is an error. */
5338 if (!range)
5339 {
5340 if (verbose)
5341 EMSGN(_(e_listidx), n1);
5342 return FAIL;
5343 }
5344 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 }
5346 if (range)
5347 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 list_T *l;
5349 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350
5351 if (n2 < 0)
5352 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005353 else if (n2 >= len)
5354 n2 = len - 1;
5355 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005356 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 l = list_alloc();
5358 if (l == NULL)
5359 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 n1 <= n2; ++n1)
5362 {
5363 if (list_append_tv(l, &item->li_tv) == FAIL)
5364 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005365 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 return FAIL;
5367 }
5368 item = item->li_next;
5369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 clear_tv(rettv);
5371 rettv->v_type = VAR_LIST;
5372 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005373 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 else
5376 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005377 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 clear_tv(rettv);
5379 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 }
5381 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382
5383 case VAR_DICT:
5384 if (range)
5385 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005386 if (verbose)
5387 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 if (len == -1)
5389 clear_tv(&var1);
5390 return FAIL;
5391 }
5392 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394
5395 if (len == -1)
5396 {
5397 key = get_tv_string(&var1);
5398 if (*key == NUL)
5399 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005400 if (verbose)
5401 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 clear_tv(&var1);
5403 return FAIL;
5404 }
5405 }
5406
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005407 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005409 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005410 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 if (len == -1)
5412 clear_tv(&var1);
5413 if (item == NULL)
5414 return FAIL;
5415
5416 copy_tv(&item->di_tv, &var1);
5417 clear_tv(rettv);
5418 *rettv = var1;
5419 }
5420 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 }
5423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 return OK;
5425}
5426
5427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 * Get an option value.
5429 * "arg" points to the '&' or '+' before the option name.
5430 * "arg" is advanced to character after the option name.
5431 * Return OK or FAIL.
5432 */
5433 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 int evaluate;
5438{
5439 char_u *option_end;
5440 long numval;
5441 char_u *stringval;
5442 int opt_type;
5443 int c;
5444 int working = (**arg == '+'); /* has("+option") */
5445 int ret = OK;
5446 int opt_flags;
5447
5448 /*
5449 * Isolate the option name and find its value.
5450 */
5451 option_end = find_option_end(arg, &opt_flags);
5452 if (option_end == NULL)
5453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 EMSG2(_("E112: Option name missing: %s"), *arg);
5456 return FAIL;
5457 }
5458
5459 if (!evaluate)
5460 {
5461 *arg = option_end;
5462 return OK;
5463 }
5464
5465 c = *option_end;
5466 *option_end = NUL;
5467 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470 if (opt_type == -3) /* invalid name */
5471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 EMSG2(_("E113: Unknown option: %s"), *arg);
5474 ret = FAIL;
5475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 if (opt_type == -2) /* hidden string option */
5479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 rettv->v_type = VAR_STRING;
5481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 else if (opt_type == -1) /* hidden number option */
5484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 rettv->v_type = VAR_NUMBER;
5486 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 else if (opt_type == 1) /* number option */
5489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 rettv->v_type = VAR_NUMBER;
5491 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 else /* string option */
5494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 rettv->v_type = VAR_STRING;
5496 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
5498 }
5499 else if (working && (opt_type == -2 || opt_type == -1))
5500 ret = FAIL;
5501
5502 *option_end = c; /* put back for error messages */
5503 *arg = option_end;
5504
5505 return ret;
5506}
5507
5508/*
5509 * Allocate a variable for a string constant.
5510 * Return OK or FAIL.
5511 */
5512 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 int evaluate;
5517{
5518 char_u *p;
5519 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 int extra = 0;
5521
5522 /*
5523 * Find the end of the string, skipping backslashed characters.
5524 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005525 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527 if (*p == '\\' && p[1] != NUL)
5528 {
5529 ++p;
5530 /* A "\<x>" form occupies at least 4 characters, and produces up
5531 * to 6 characters: reserve space for 2 extra */
5532 if (*p == '<')
5533 extra += 2;
5534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 }
5536
5537 if (*p != '"')
5538 {
5539 EMSG2(_("E114: Missing quote: %s"), *arg);
5540 return FAIL;
5541 }
5542
5543 /* If only parsing, set *arg and return here */
5544 if (!evaluate)
5545 {
5546 *arg = p + 1;
5547 return OK;
5548 }
5549
5550 /*
5551 * Copy the string into allocated memory, handling backslashed
5552 * characters.
5553 */
5554 name = alloc((unsigned)(p - *arg + extra));
5555 if (name == NULL)
5556 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557 rettv->v_type = VAR_STRING;
5558 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 if (*p == '\\')
5563 {
5564 switch (*++p)
5565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 case 'b': *name++ = BS; ++p; break;
5567 case 'e': *name++ = ESC; ++p; break;
5568 case 'f': *name++ = FF; ++p; break;
5569 case 'n': *name++ = NL; ++p; break;
5570 case 'r': *name++ = CAR; ++p; break;
5571 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 case 'X': /* hex: "\x1", "\x12" */
5574 case 'x':
5575 case 'u': /* Unicode: "\u0023" */
5576 case 'U':
5577 if (vim_isxdigit(p[1]))
5578 {
5579 int n, nr;
5580 int c = toupper(*p);
5581
5582 if (c == 'X')
5583 n = 2;
5584 else
5585 n = 4;
5586 nr = 0;
5587 while (--n >= 0 && vim_isxdigit(p[1]))
5588 {
5589 ++p;
5590 nr = (nr << 4) + hex2nr(*p);
5591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#ifdef FEAT_MBYTE
5594 /* For "\u" store the number according to
5595 * 'encoding'. */
5596 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 else
5599#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 break;
5603
5604 /* octal: "\1", "\12", "\123" */
5605 case '0':
5606 case '1':
5607 case '2':
5608 case '3':
5609 case '4':
5610 case '5':
5611 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 case '7': *name = *p++ - '0';
5613 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 *name = (*name << 3) + *p++ - '0';
5616 if (*p >= '0' && *p <= '7')
5617 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 break;
5621
5622 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 if (extra != 0)
5625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 break;
5628 }
5629 /* FALLTHROUGH */
5630
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 break;
5633 }
5634 }
5635 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 *arg = p + 1;
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return OK;
5643}
5644
5645/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 * Return OK or FAIL.
5648 */
5649 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 int evaluate;
5654{
5655 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005656 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657 int reduce = 0;
5658
5659 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 */
5662 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005667 break;
5668 ++reduce;
5669 ++p;
5670 }
5671 }
5672
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 return FAIL;
5677 }
5678
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 if (!evaluate)
5681 {
5682 *arg = p + 1;
5683 return OK;
5684 }
5685
5686 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005688 */
5689 str = alloc((unsigned)((p - *arg) - reduce));
5690 if (str == NULL)
5691 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 rettv->v_type = VAR_STRING;
5693 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005694
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700 break;
5701 ++p;
5702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005706 *arg = p + 1;
5707
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005708 return OK;
5709}
5710
5711/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 * Allocate a variable for a List and fill it from "*arg".
5713 * Return OK or FAIL.
5714 */
5715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005716get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005718 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 int evaluate;
5720{
Bram Moolenaar33570922005-01-25 22:26:29 +00005721 list_T *l = NULL;
5722 typval_T tv;
5723 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724
5725 if (evaluate)
5726 {
5727 l = list_alloc();
5728 if (l == NULL)
5729 return FAIL;
5730 }
5731
5732 *arg = skipwhite(*arg + 1);
5733 while (**arg != ']' && **arg != NUL)
5734 {
5735 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5736 goto failret;
5737 if (evaluate)
5738 {
5739 item = listitem_alloc();
5740 if (item != NULL)
5741 {
5742 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005743 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 list_append(l, item);
5745 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005746 else
5747 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 }
5749
5750 if (**arg == ']')
5751 break;
5752 if (**arg != ',')
5753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 goto failret;
5756 }
5757 *arg = skipwhite(*arg + 1);
5758 }
5759
5760 if (**arg != ']')
5761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763failret:
5764 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005765 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 return FAIL;
5767 }
5768
5769 *arg = skipwhite(*arg + 1);
5770 if (evaluate)
5771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005772 rettv->v_type = VAR_LIST;
5773 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 ++l->lv_refcount;
5775 }
5776
5777 return OK;
5778}
5779
5780/*
5781 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005782 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005784 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785list_alloc()
5786{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005787 list_T *l;
5788
5789 l = (list_T *)alloc_clear(sizeof(list_T));
5790 if (l != NULL)
5791 {
5792 /* Prepend the list to the list of lists for garbage collection. */
5793 if (first_list != NULL)
5794 first_list->lv_used_prev = l;
5795 l->lv_used_prev = NULL;
5796 l->lv_used_next = first_list;
5797 first_list = l;
5798 }
5799 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800}
5801
5802/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005803 * Allocate an empty list for a return value.
5804 * Returns OK or FAIL.
5805 */
5806 static int
5807rettv_list_alloc(rettv)
5808 typval_T *rettv;
5809{
5810 list_T *l = list_alloc();
5811
5812 if (l == NULL)
5813 return FAIL;
5814
5815 rettv->vval.v_list = l;
5816 rettv->v_type = VAR_LIST;
5817 ++l->lv_refcount;
5818 return OK;
5819}
5820
5821/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 * Unreference a list: decrement the reference count and free it when it
5823 * becomes zero.
5824 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005825 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005829 if (l != NULL && --l->lv_refcount <= 0)
5830 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831}
5832
5833/*
5834 * Free a list, including all items it points to.
5835 * Ignores the reference count.
5836 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005837 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005838list_free(l, recurse)
5839 list_T *l;
5840 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841{
Bram Moolenaar33570922005-01-25 22:26:29 +00005842 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005844 /* Remove the list from the list of lists for garbage collection. */
5845 if (l->lv_used_prev == NULL)
5846 first_list = l->lv_used_next;
5847 else
5848 l->lv_used_prev->lv_used_next = l->lv_used_next;
5849 if (l->lv_used_next != NULL)
5850 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5851
Bram Moolenaard9fba312005-06-26 22:34:35 +00005852 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005854 /* Remove the item before deleting it. */
5855 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005856 if (recurse || (item->li_tv.v_type != VAR_LIST
5857 && item->li_tv.v_type != VAR_DICT))
5858 clear_tv(&item->li_tv);
5859 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 }
5861 vim_free(l);
5862}
5863
5864/*
5865 * Allocate a list item.
5866 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005867 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868listitem_alloc()
5869{
Bram Moolenaar33570922005-01-25 22:26:29 +00005870 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871}
5872
5873/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005874 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 */
5876 static void
5877listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005880 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 vim_free(item);
5882}
5883
5884/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005885 * Remove a list item from a List and free it. Also clears the value.
5886 */
5887 static void
5888listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *l;
5890 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005891{
5892 list_remove(l, item, item);
5893 listitem_free(item);
5894}
5895
5896/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 * Get the number of items in a list.
5898 */
5899 static long
5900list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 if (l == NULL)
5904 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005905 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906}
5907
5908/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005909 * Return TRUE when two lists have exactly the same values.
5910 */
5911 static int
5912list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 list_T *l1;
5914 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005915 int ic; /* ignore case for strings */
5916{
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005918
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005919 if (l1 == NULL || l2 == NULL)
5920 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005921 if (l1 == l2)
5922 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005923 if (list_len(l1) != list_len(l2))
5924 return FALSE;
5925
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 for (item1 = l1->lv_first, item2 = l2->lv_first;
5927 item1 != NULL && item2 != NULL;
5928 item1 = item1->li_next, item2 = item2->li_next)
5929 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5930 return FALSE;
5931 return item1 == NULL && item2 == NULL;
5932}
5933
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005934#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5935 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005936/*
5937 * Return the dictitem that an entry in a hashtable points to.
5938 */
5939 dictitem_T *
5940dict_lookup(hi)
5941 hashitem_T *hi;
5942{
5943 return HI2DI(hi);
5944}
5945#endif
5946
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005947/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005948 * Return TRUE when two dictionaries have exactly the same key/values.
5949 */
5950 static int
5951dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 dict_T *d1;
5953 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005954 int ic; /* ignore case for strings */
5955{
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 hashitem_T *hi;
5957 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005958 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005959
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005960 if (d1 == NULL || d2 == NULL)
5961 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005962 if (d1 == d2)
5963 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005964 if (dict_len(d1) != dict_len(d2))
5965 return FALSE;
5966
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005967 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005968 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005969 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005970 if (!HASHITEM_EMPTY(hi))
5971 {
5972 item2 = dict_find(d2, hi->hi_key, -1);
5973 if (item2 == NULL)
5974 return FALSE;
5975 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5976 return FALSE;
5977 --todo;
5978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005979 }
5980 return TRUE;
5981}
5982
5983/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005984 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005985 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005986 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987 */
5988 static int
5989tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 typval_T *tv1;
5991 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005992 int ic; /* ignore case */
5993{
5994 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005995 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005996 static int recursive = 0; /* cach recursive loops */
5997 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005999 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006000 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006001 /* Catch lists and dicts that have an endless loop by limiting
6002 * recursiveness to 1000. We guess they are equal then. */
6003 if (recursive >= 1000)
6004 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006005
6006 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006008 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006009 ++recursive;
6010 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
6011 --recursive;
6012 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006013
6014 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006015 ++recursive;
6016 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6017 --recursive;
6018 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006019
6020 case VAR_FUNC:
6021 return (tv1->vval.v_string != NULL
6022 && tv2->vval.v_string != NULL
6023 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6024
6025 case VAR_NUMBER:
6026 return tv1->vval.v_number == tv2->vval.v_number;
6027
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006028#ifdef FEAT_FLOAT
6029 case VAR_FLOAT:
6030 return tv1->vval.v_float == tv2->vval.v_float;
6031#endif
6032
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006033 case VAR_STRING:
6034 s1 = get_tv_string_buf(tv1, buf1);
6035 s2 = get_tv_string_buf(tv2, buf2);
6036 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006037 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006038
6039 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 return TRUE;
6041}
6042
6043/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 * Locate item with index "n" in list "l" and return it.
6045 * A negative index is counted from the end; -1 is the last item.
6046 * Returns NULL when "n" is out of range.
6047 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 long n;
6052{
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 long idx;
6055
6056 if (l == NULL)
6057 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006058
6059 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006061 n = l->lv_len + n;
6062
6063 /* Check for index out of range. */
6064 if (n < 0 || n >= l->lv_len)
6065 return NULL;
6066
6067 /* When there is a cached index may start search from there. */
6068 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006070 if (n < l->lv_idx / 2)
6071 {
6072 /* closest to the start of the list */
6073 item = l->lv_first;
6074 idx = 0;
6075 }
6076 else if (n > (l->lv_idx + l->lv_len) / 2)
6077 {
6078 /* closest to the end of the list */
6079 item = l->lv_last;
6080 idx = l->lv_len - 1;
6081 }
6082 else
6083 {
6084 /* closest to the cached index */
6085 item = l->lv_idx_item;
6086 idx = l->lv_idx;
6087 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 }
6089 else
6090 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006091 if (n < l->lv_len / 2)
6092 {
6093 /* closest to the start of the list */
6094 item = l->lv_first;
6095 idx = 0;
6096 }
6097 else
6098 {
6099 /* closest to the end of the list */
6100 item = l->lv_last;
6101 idx = l->lv_len - 1;
6102 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006104
6105 while (n > idx)
6106 {
6107 /* search forward */
6108 item = item->li_next;
6109 ++idx;
6110 }
6111 while (n < idx)
6112 {
6113 /* search backward */
6114 item = item->li_prev;
6115 --idx;
6116 }
6117
6118 /* cache the used index */
6119 l->lv_idx = idx;
6120 l->lv_idx_item = item;
6121
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 return item;
6123}
6124
6125/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006126 * Get list item "l[idx]" as a number.
6127 */
6128 static long
6129list_find_nr(l, idx, errorp)
6130 list_T *l;
6131 long idx;
6132 int *errorp; /* set to TRUE when something wrong */
6133{
6134 listitem_T *li;
6135
6136 li = list_find(l, idx);
6137 if (li == NULL)
6138 {
6139 if (errorp != NULL)
6140 *errorp = TRUE;
6141 return -1L;
6142 }
6143 return get_tv_number_chk(&li->li_tv, errorp);
6144}
6145
6146/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006147 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6148 */
6149 char_u *
6150list_find_str(l, idx)
6151 list_T *l;
6152 long idx;
6153{
6154 listitem_T *li;
6155
6156 li = list_find(l, idx - 1);
6157 if (li == NULL)
6158 {
6159 EMSGN(_(e_listidx), idx);
6160 return NULL;
6161 }
6162 return get_tv_string(&li->li_tv);
6163}
6164
6165/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006166 * Locate "item" list "l" and return its index.
6167 * Returns -1 when "item" is not in the list.
6168 */
6169 static long
6170list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 list_T *l;
6172 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006173{
6174 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006176
6177 if (l == NULL)
6178 return -1;
6179 idx = 0;
6180 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6181 ++idx;
6182 if (li == NULL)
6183 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006184 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006185}
6186
6187/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 * Append item "item" to the end of list "l".
6189 */
6190 static void
6191list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 list_T *l;
6193 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194{
6195 if (l->lv_last == NULL)
6196 {
6197 /* empty list */
6198 l->lv_first = item;
6199 l->lv_last = item;
6200 item->li_prev = NULL;
6201 }
6202 else
6203 {
6204 l->lv_last->li_next = item;
6205 item->li_prev = l->lv_last;
6206 l->lv_last = item;
6207 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006208 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209 item->li_next = NULL;
6210}
6211
6212/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006214 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006215 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006216 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 list_T *l;
6219 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006221 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222
Bram Moolenaar05159a02005-02-26 23:04:13 +00006223 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006225 copy_tv(tv, &li->li_tv);
6226 list_append(l, li);
6227 return OK;
6228}
6229
6230/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006231 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006232 * Return FAIL when out of memory.
6233 */
6234 int
6235list_append_dict(list, dict)
6236 list_T *list;
6237 dict_T *dict;
6238{
6239 listitem_T *li = listitem_alloc();
6240
6241 if (li == NULL)
6242 return FAIL;
6243 li->li_tv.v_type = VAR_DICT;
6244 li->li_tv.v_lock = 0;
6245 li->li_tv.vval.v_dict = dict;
6246 list_append(list, li);
6247 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 return OK;
6249}
6250
6251/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006252 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006253 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006254 * Returns FAIL when out of memory.
6255 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006256 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006257list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006258 list_T *l;
6259 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006260 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006261{
6262 listitem_T *li = listitem_alloc();
6263
6264 if (li == NULL)
6265 return FAIL;
6266 list_append(l, li);
6267 li->li_tv.v_type = VAR_STRING;
6268 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006269 if (str == NULL)
6270 li->li_tv.vval.v_string = NULL;
6271 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006272 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006273 return FAIL;
6274 return OK;
6275}
6276
6277/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006278 * Append "n" to list "l".
6279 * Returns FAIL when out of memory.
6280 */
6281 static int
6282list_append_number(l, n)
6283 list_T *l;
6284 varnumber_T n;
6285{
6286 listitem_T *li;
6287
6288 li = listitem_alloc();
6289 if (li == NULL)
6290 return FAIL;
6291 li->li_tv.v_type = VAR_NUMBER;
6292 li->li_tv.v_lock = 0;
6293 li->li_tv.vval.v_number = n;
6294 list_append(l, li);
6295 return OK;
6296}
6297
6298/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006300 * If "item" is NULL append at the end.
6301 * Return FAIL when out of memory.
6302 */
6303 static int
6304list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 list_T *l;
6306 typval_T *tv;
6307 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308{
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006310
6311 if (ni == NULL)
6312 return FAIL;
6313 copy_tv(tv, &ni->li_tv);
6314 if (item == NULL)
6315 /* Append new item at end of list. */
6316 list_append(l, ni);
6317 else
6318 {
6319 /* Insert new item before existing item. */
6320 ni->li_prev = item->li_prev;
6321 ni->li_next = item;
6322 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006323 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006324 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006325 ++l->lv_idx;
6326 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006327 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006328 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006329 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330 l->lv_idx_item = NULL;
6331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006333 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334 }
6335 return OK;
6336}
6337
6338/*
6339 * Extend "l1" with "l2".
6340 * If "bef" is NULL append at the end, otherwise insert before this item.
6341 * Returns FAIL when out of memory.
6342 */
6343 static int
6344list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 list_T *l1;
6346 list_T *l2;
6347 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348{
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006350 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006352 /* We also quit the loop when we have inserted the original item count of
6353 * the list, avoid a hang when we extend a list with itself. */
6354 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6356 return FAIL;
6357 return OK;
6358}
6359
6360/*
6361 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6362 * Return FAIL when out of memory.
6363 */
6364 static int
6365list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 list_T *l1;
6367 list_T *l2;
6368 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006369{
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006372 if (l1 == NULL || l2 == NULL)
6373 return FAIL;
6374
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377 if (l == NULL)
6378 return FAIL;
6379 tv->v_type = VAR_LIST;
6380 tv->vval.v_list = l;
6381
6382 /* append all items from the second list */
6383 return list_extend(l, l2, NULL);
6384}
6385
6386/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006389 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 * Returns NULL when out of memory.
6391 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006393list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006396 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397{
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *copy;
6399 listitem_T *item;
6400 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401
6402 if (orig == NULL)
6403 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404
6405 copy = list_alloc();
6406 if (copy != NULL)
6407 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006408 if (copyID != 0)
6409 {
6410 /* Do this before adding the items, because one of the items may
6411 * refer back to this list. */
6412 orig->lv_copyID = copyID;
6413 orig->lv_copylist = copy;
6414 }
6415 for (item = orig->lv_first; item != NULL && !got_int;
6416 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 {
6418 ni = listitem_alloc();
6419 if (ni == NULL)
6420 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006421 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006422 {
6423 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6424 {
6425 vim_free(ni);
6426 break;
6427 }
6428 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006430 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 list_append(copy, ni);
6432 }
6433 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 if (item != NULL)
6435 {
6436 list_unref(copy);
6437 copy = NULL;
6438 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 }
6440
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 return copy;
6442}
6443
6444/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006446 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006449list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 list_T *l;
6451 listitem_T *item;
6452 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453{
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 /* notify watchers */
6457 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 list_fix_watch(l, ip);
6461 if (ip == item2)
6462 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464
6465 if (item2->li_next == NULL)
6466 l->lv_last = item->li_prev;
6467 else
6468 item2->li_next->li_prev = item->li_prev;
6469 if (item->li_prev == NULL)
6470 l->lv_first = item2->li_next;
6471 else
6472 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006473 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474}
6475
6476/*
6477 * Return an allocated string with the string representation of a list.
6478 * May return NULL.
6479 */
6480 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006481list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006483 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006484{
6485 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486
6487 if (tv->vval.v_list == NULL)
6488 return NULL;
6489 ga_init2(&ga, (int)sizeof(char), 80);
6490 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006491 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492 {
6493 vim_free(ga.ga_data);
6494 return NULL;
6495 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496 ga_append(&ga, ']');
6497 ga_append(&ga, NUL);
6498 return (char_u *)ga.ga_data;
6499}
6500
6501/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006502 * Join list "l" into a string in "*gap", using separator "sep".
6503 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006505 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006506 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006507list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006508 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006510 char_u *sep;
6511 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006512 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513{
6514 int first = TRUE;
6515 char_u *tofree;
6516 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006518 char_u *s;
6519
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006520 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006521 {
6522 if (first)
6523 first = FALSE;
6524 else
6525 ga_concat(gap, sep);
6526
6527 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006528 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006529 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006530 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006531 if (s != NULL)
6532 ga_concat(gap, s);
6533 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 if (s == NULL)
6535 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006536 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006537 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006539}
6540
6541/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006542 * Garbage collection for lists and dictionaries.
6543 *
6544 * We use reference counts to be able to free most items right away when they
6545 * are no longer used. But for composite items it's possible that it becomes
6546 * unused while the reference count is > 0: When there is a recursive
6547 * reference. Example:
6548 * :let l = [1, 2, 3]
6549 * :let d = {9: l}
6550 * :let l[1] = d
6551 *
6552 * Since this is quite unusual we handle this with garbage collection: every
6553 * once in a while find out which lists and dicts are not referenced from any
6554 * variable.
6555 *
6556 * Here is a good reference text about garbage collection (refers to Python
6557 * but it applies to all reference-counting mechanisms):
6558 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006559 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006560
6561/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006562 * Do garbage collection for lists and dicts.
6563 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006564 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006565 int
6566garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006567{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006568 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006569 buf_T *buf;
6570 win_T *wp;
6571 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006572 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006573 int did_free;
6574 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006575#ifdef FEAT_WINDOWS
6576 tabpage_T *tp;
6577#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006578
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006579 /* Only do this once. */
6580 want_garbage_collect = FALSE;
6581 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006582 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006583
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006584 /* We advance by two because we add one for items referenced through
6585 * previous_funccal. */
6586 current_copyID += COPYID_INC;
6587 copyID = current_copyID;
6588
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006589 /*
6590 * 1. Go through all accessible variables and mark all lists and dicts
6591 * with copyID.
6592 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006593
6594 /* Don't free variables in the previous_funccal list unless they are only
6595 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006596 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006597 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6598 {
6599 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6600 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6601 }
6602
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006603 /* script-local variables */
6604 for (i = 1; i <= ga_scripts.ga_len; ++i)
6605 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6606
6607 /* buffer-local variables */
6608 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6609 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6610
6611 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006612 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006613 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6614
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006615#ifdef FEAT_WINDOWS
6616 /* tabpage-local variables */
6617 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6618 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6619#endif
6620
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006621 /* global variables */
6622 set_ref_in_ht(&globvarht, copyID);
6623
6624 /* function-local variables */
6625 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6626 {
6627 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6628 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6629 }
6630
Bram Moolenaard812df62008-11-09 12:46:09 +00006631 /* v: vars */
6632 set_ref_in_ht(&vimvarht, copyID);
6633
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006634 /*
6635 * 2. Free lists and dictionaries that are not referenced.
6636 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006637 did_free = free_unref_items(copyID);
6638
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006639 /*
6640 * 3. Check if any funccal can be freed now.
6641 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006642 for (pfc = &previous_funccal; *pfc != NULL; )
6643 {
6644 if (can_free_funccal(*pfc, copyID))
6645 {
6646 fc = *pfc;
6647 *pfc = fc->caller;
6648 free_funccal(fc, TRUE);
6649 did_free = TRUE;
6650 did_free_funccal = TRUE;
6651 }
6652 else
6653 pfc = &(*pfc)->caller;
6654 }
6655 if (did_free_funccal)
6656 /* When a funccal was freed some more items might be garbage
6657 * collected, so run again. */
6658 (void)garbage_collect();
6659
6660 return did_free;
6661}
6662
6663/*
6664 * Free lists and dictionaries that are no longer referenced.
6665 */
6666 static int
6667free_unref_items(copyID)
6668 int copyID;
6669{
6670 dict_T *dd;
6671 list_T *ll;
6672 int did_free = FALSE;
6673
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006674 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006675 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 */
6677 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006678 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006679 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006680 /* Free the Dictionary and ordinary items it contains, but don't
6681 * recurse into Lists and Dictionaries, they will be in the list
6682 * of dicts or list of lists. */
6683 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 did_free = TRUE;
6685
6686 /* restart, next dict may also have been freed */
6687 dd = first_dict;
6688 }
6689 else
6690 dd = dd->dv_used_next;
6691
6692 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006693 * Go through the list of lists and free items without the copyID.
6694 * But don't free a list that has a watcher (used in a for loop), these
6695 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006696 */
6697 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006698 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6699 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006701 /* Free the List and ordinary items it contains, but don't recurse
6702 * into Lists and Dictionaries, they will be in the list of dicts
6703 * or list of lists. */
6704 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006705 did_free = TRUE;
6706
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006707 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006708 ll = first_list;
6709 }
6710 else
6711 ll = ll->lv_used_next;
6712
6713 return did_free;
6714}
6715
6716/*
6717 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6718 */
6719 static void
6720set_ref_in_ht(ht, copyID)
6721 hashtab_T *ht;
6722 int copyID;
6723{
6724 int todo;
6725 hashitem_T *hi;
6726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006727 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 for (hi = ht->ht_array; todo > 0; ++hi)
6729 if (!HASHITEM_EMPTY(hi))
6730 {
6731 --todo;
6732 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6733 }
6734}
6735
6736/*
6737 * Mark all lists and dicts referenced through list "l" with "copyID".
6738 */
6739 static void
6740set_ref_in_list(l, copyID)
6741 list_T *l;
6742 int copyID;
6743{
6744 listitem_T *li;
6745
6746 for (li = l->lv_first; li != NULL; li = li->li_next)
6747 set_ref_in_item(&li->li_tv, copyID);
6748}
6749
6750/*
6751 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6752 */
6753 static void
6754set_ref_in_item(tv, copyID)
6755 typval_T *tv;
6756 int copyID;
6757{
6758 dict_T *dd;
6759 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760
6761 switch (tv->v_type)
6762 {
6763 case VAR_DICT:
6764 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006765 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 /* Didn't see this dict yet. */
6768 dd->dv_copyID = copyID;
6769 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006770 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772
6773 case VAR_LIST:
6774 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006775 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006776 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 /* Didn't see this list yet. */
6778 ll->lv_copyID = copyID;
6779 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006780 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006782 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006784}
6785
6786/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 * Allocate an empty header for a dictionary.
6788 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006789 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790dict_alloc()
6791{
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006793
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006795 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006796 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006797 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 if (first_dict != NULL)
6799 first_dict->dv_used_prev = d;
6800 d->dv_used_next = first_dict;
6801 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006802 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006803
Bram Moolenaar33570922005-01-25 22:26:29 +00006804 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006805 d->dv_lock = 0;
6806 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006807 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006808 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006809 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810}
6811
6812/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006813 * Allocate an empty dict for a return value.
6814 * Returns OK or FAIL.
6815 */
6816 static int
6817rettv_dict_alloc(rettv)
6818 typval_T *rettv;
6819{
6820 dict_T *d = dict_alloc();
6821
6822 if (d == NULL)
6823 return FAIL;
6824
6825 rettv->vval.v_dict = d;
6826 rettv->v_type = VAR_DICT;
6827 ++d->dv_refcount;
6828 return OK;
6829}
6830
6831
6832/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006833 * Unreference a Dictionary: decrement the reference count and free it when it
6834 * becomes zero.
6835 */
6836 static void
6837dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006840 if (d != NULL && --d->dv_refcount <= 0)
6841 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006842}
6843
6844/*
6845 * Free a Dictionary, including all items it contains.
6846 * Ignores the reference count.
6847 */
6848 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006849dict_free(d, recurse)
6850 dict_T *d;
6851 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006852{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006853 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 /* Remove the dict from the list of dicts for garbage collection. */
6858 if (d->dv_used_prev == NULL)
6859 first_dict = d->dv_used_next;
6860 else
6861 d->dv_used_prev->dv_used_next = d->dv_used_next;
6862 if (d->dv_used_next != NULL)
6863 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6864
6865 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006866 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006867 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006870 if (!HASHITEM_EMPTY(hi))
6871 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006872 /* Remove the item before deleting it, just in case there is
6873 * something recursive causing trouble. */
6874 di = HI2DI(hi);
6875 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006876 if (recurse || (di->di_tv.v_type != VAR_LIST
6877 && di->di_tv.v_type != VAR_DICT))
6878 clear_tv(&di->di_tv);
6879 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880 --todo;
6881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006883 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884 vim_free(d);
6885}
6886
6887/*
6888 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006889 * The "key" is copied to the new item.
6890 * Note that the value of the item "di_tv" still needs to be initialized!
6891 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006893 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006894dictitem_alloc(key)
6895 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006896{
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006899 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006900 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006901 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006902 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006903 di->di_flags = 0;
6904 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906}
6907
6908/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006909 * Make a copy of a Dictionary item.
6910 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006912dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914{
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006917 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6918 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006919 if (di != NULL)
6920 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006921 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006922 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 copy_tv(&org->di_tv, &di->di_tv);
6924 }
6925 return di;
6926}
6927
6928/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006929 * Remove item "item" from Dictionary "dict" and free it.
6930 */
6931 static void
6932dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006933 dict_T *dict;
6934 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006935{
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006937
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939 if (HASHITEM_EMPTY(hi))
6940 EMSG2(_(e_intern2), "dictitem_remove()");
6941 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 dictitem_free(item);
6944}
6945
6946/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947 * Free a dict item. Also clears the value.
6948 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006949 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006950dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006952{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006953 clear_tv(&item->di_tv);
6954 vim_free(item);
6955}
6956
6957/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006958 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6959 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006960 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961 * Returns NULL when out of memory.
6962 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006963 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968{
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 dict_T *copy;
6970 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973
6974 if (orig == NULL)
6975 return NULL;
6976
6977 copy = dict_alloc();
6978 if (copy != NULL)
6979 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006980 if (copyID != 0)
6981 {
6982 orig->dv_copyID = copyID;
6983 orig->dv_copydict = copy;
6984 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006985 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006986 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006990 --todo;
6991
6992 di = dictitem_alloc(hi->hi_key);
6993 if (di == NULL)
6994 break;
6995 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006996 {
6997 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6998 copyID) == FAIL)
6999 {
7000 vim_free(di);
7001 break;
7002 }
7003 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004 else
7005 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7006 if (dict_add(copy, di) == FAIL)
7007 {
7008 dictitem_free(di);
7009 break;
7010 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007011 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007013
Bram Moolenaare9a41262005-01-15 22:18:47 +00007014 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007015 if (todo > 0)
7016 {
7017 dict_unref(copy);
7018 copy = NULL;
7019 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020 }
7021
7022 return copy;
7023}
7024
7025/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007027 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007029 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dict_T *d;
7032 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033{
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035}
7036
Bram Moolenaar8c711452005-01-14 21:53:12 +00007037/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007038 * Add a number or string entry to dictionary "d".
7039 * When "str" is NULL use number "nr", otherwise use "str".
7040 * Returns FAIL when out of memory and when key already exists.
7041 */
7042 int
7043dict_add_nr_str(d, key, nr, str)
7044 dict_T *d;
7045 char *key;
7046 long nr;
7047 char_u *str;
7048{
7049 dictitem_T *item;
7050
7051 item = dictitem_alloc((char_u *)key);
7052 if (item == NULL)
7053 return FAIL;
7054 item->di_tv.v_lock = 0;
7055 if (str == NULL)
7056 {
7057 item->di_tv.v_type = VAR_NUMBER;
7058 item->di_tv.vval.v_number = nr;
7059 }
7060 else
7061 {
7062 item->di_tv.v_type = VAR_STRING;
7063 item->di_tv.vval.v_string = vim_strsave(str);
7064 }
7065 if (dict_add(d, item) == FAIL)
7066 {
7067 dictitem_free(item);
7068 return FAIL;
7069 }
7070 return OK;
7071}
7072
7073/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007074 * Add a list entry to dictionary "d".
7075 * Returns FAIL when out of memory and when key already exists.
7076 */
7077 int
7078dict_add_list(d, key, list)
7079 dict_T *d;
7080 char *key;
7081 list_T *list;
7082{
7083 dictitem_T *item;
7084
7085 item = dictitem_alloc((char_u *)key);
7086 if (item == NULL)
7087 return FAIL;
7088 item->di_tv.v_lock = 0;
7089 item->di_tv.v_type = VAR_LIST;
7090 item->di_tv.vval.v_list = list;
7091 if (dict_add(d, item) == FAIL)
7092 {
7093 dictitem_free(item);
7094 return FAIL;
7095 }
7096 return OK;
7097}
7098
7099/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 * Get the number of items in a Dictionary.
7101 */
7102 static long
7103dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106 if (d == NULL)
7107 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007108 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007109}
7110
7111/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 * Find item "key[len]" in Dictionary "d".
7113 * If "len" is negative use strlen(key).
7114 * Returns NULL when not found.
7115 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007116 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 char_u *key;
7120 int len;
7121{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122#define AKEYLEN 200
7123 char_u buf[AKEYLEN];
7124 char_u *akey;
7125 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 if (len < 0)
7129 akey = key;
7130 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 tofree = akey = vim_strnsave(key, len);
7133 if (akey == NULL)
7134 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007135 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 else
7137 {
7138 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007139 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007140 akey = buf;
7141 }
7142
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007144 vim_free(tofree);
7145 if (HASHITEM_EMPTY(hi))
7146 return NULL;
7147 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148}
7149
7150/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007151 * Get a string item from a dictionary.
7152 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007153 * Returns NULL if the entry doesn't exist or out of memory.
7154 */
7155 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007156get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007157 dict_T *d;
7158 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007159 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007160{
7161 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007162 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007163
7164 di = dict_find(d, key, -1);
7165 if (di == NULL)
7166 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007167 s = get_tv_string(&di->di_tv);
7168 if (save && s != NULL)
7169 s = vim_strsave(s);
7170 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007171}
7172
7173/*
7174 * Get a number item from a dictionary.
7175 * Returns 0 if the entry doesn't exist or out of memory.
7176 */
7177 long
7178get_dict_number(d, key)
7179 dict_T *d;
7180 char_u *key;
7181{
7182 dictitem_T *di;
7183
7184 di = dict_find(d, key, -1);
7185 if (di == NULL)
7186 return 0;
7187 return get_tv_number(&di->di_tv);
7188}
7189
7190/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191 * Return an allocated string with the string representation of a Dictionary.
7192 * May return NULL.
7193 */
7194 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007195dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007197 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198{
7199 garray_T ga;
7200 int first = TRUE;
7201 char_u *tofree;
7202 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 return NULL;
7210 ga_init2(&ga, (int)sizeof(char), 80);
7211 ga_append(&ga, '{');
7212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007213 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 --todo;
7219
7220 if (first)
7221 first = FALSE;
7222 else
7223 ga_concat(&ga, (char_u *)", ");
7224
7225 tofree = string_quote(hi->hi_key, FALSE);
7226 if (tofree != NULL)
7227 {
7228 ga_concat(&ga, tofree);
7229 vim_free(tofree);
7230 }
7231 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007232 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007233 if (s != NULL)
7234 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007236 if (s == NULL)
7237 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007240 if (todo > 0)
7241 {
7242 vim_free(ga.ga_data);
7243 return NULL;
7244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245
7246 ga_append(&ga, '}');
7247 ga_append(&ga, NUL);
7248 return (char_u *)ga.ga_data;
7249}
7250
7251/*
7252 * Allocate a variable for a Dictionary and fill it from "*arg".
7253 * Return OK or FAIL. Returns NOTDONE for {expr}.
7254 */
7255 static int
7256get_dict_tv(arg, rettv, evaluate)
7257 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 int evaluate;
7260{
Bram Moolenaar33570922005-01-25 22:26:29 +00007261 dict_T *d = NULL;
7262 typval_T tvkey;
7263 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007264 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268
7269 /*
7270 * First check if it's not a curly-braces thing: {expr}.
7271 * Must do this without evaluating, otherwise a function may be called
7272 * twice. Unfortunately this means we need to call eval1() twice for the
7273 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 if (*start != '}')
7277 {
7278 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7279 return FAIL;
7280 if (*start == '}')
7281 return NOTDONE;
7282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283
7284 if (evaluate)
7285 {
7286 d = dict_alloc();
7287 if (d == NULL)
7288 return FAIL;
7289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 tvkey.v_type = VAR_UNKNOWN;
7291 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292
7293 *arg = skipwhite(*arg + 1);
7294 while (**arg != '}' && **arg != NUL)
7295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 goto failret;
7298 if (**arg != ':')
7299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007300 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 goto failret;
7303 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007304 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007306 key = get_tv_string_buf_chk(&tvkey, buf);
7307 if (key == NULL || *key == NUL)
7308 {
7309 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7310 if (key != NULL)
7311 EMSG(_(e_emptykey));
7312 clear_tv(&tvkey);
7313 goto failret;
7314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316
7317 *arg = skipwhite(*arg + 1);
7318 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7319 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007320 if (evaluate)
7321 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 goto failret;
7323 }
7324 if (evaluate)
7325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 if (item != NULL)
7328 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007329 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 clear_tv(&tv);
7332 goto failret;
7333 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 item = dictitem_alloc(key);
7335 clear_tv(&tvkey);
7336 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007339 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 if (dict_add(d, item) == FAIL)
7341 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 }
7343 }
7344
7345 if (**arg == '}')
7346 break;
7347 if (**arg != ',')
7348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007349 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350 goto failret;
7351 }
7352 *arg = skipwhite(*arg + 1);
7353 }
7354
7355 if (**arg != '}')
7356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007357 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358failret:
7359 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007360 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 return FAIL;
7362 }
7363
7364 *arg = skipwhite(*arg + 1);
7365 if (evaluate)
7366 {
7367 rettv->v_type = VAR_DICT;
7368 rettv->vval.v_dict = d;
7369 ++d->dv_refcount;
7370 }
7371
7372 return OK;
7373}
7374
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007376 * Return a string with the string representation of a variable.
7377 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007378 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007379 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007381 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 */
7383 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007384echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007387 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007388 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 static int recurse = 0;
7391 char_u *r = NULL;
7392
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007395 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 *tofree = NULL;
7397 return NULL;
7398 }
7399 ++recurse;
7400
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 switch (tv->v_type)
7402 {
7403 case VAR_FUNC:
7404 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 r = tv->vval.v_string;
7406 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007408 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007409 if (tv->vval.v_list == NULL)
7410 {
7411 *tofree = NULL;
7412 r = NULL;
7413 }
7414 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7415 {
7416 *tofree = NULL;
7417 r = (char_u *)"[...]";
7418 }
7419 else
7420 {
7421 tv->vval.v_list->lv_copyID = copyID;
7422 *tofree = list2string(tv, copyID);
7423 r = *tofree;
7424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007426
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007428 if (tv->vval.v_dict == NULL)
7429 {
7430 *tofree = NULL;
7431 r = NULL;
7432 }
7433 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7434 {
7435 *tofree = NULL;
7436 r = (char_u *)"{...}";
7437 }
7438 else
7439 {
7440 tv->vval.v_dict->dv_copyID = copyID;
7441 *tofree = dict2string(tv, copyID);
7442 r = *tofree;
7443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007444 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007445
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 case VAR_STRING:
7447 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 *tofree = NULL;
7449 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007452#ifdef FEAT_FLOAT
7453 case VAR_FLOAT:
7454 *tofree = NULL;
7455 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7456 r = numbuf;
7457 break;
7458#endif
7459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007461 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007462 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007463 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464
7465 --recurse;
7466 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007467}
7468
7469/*
7470 * Return a string with the string representation of a variable.
7471 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7472 * "numbuf" is used for a number.
7473 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007474 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007475 */
7476 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007477tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007479 char_u **tofree;
7480 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007481 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007482{
7483 switch (tv->v_type)
7484 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007485 case VAR_FUNC:
7486 *tofree = string_quote(tv->vval.v_string, TRUE);
7487 return *tofree;
7488 case VAR_STRING:
7489 *tofree = string_quote(tv->vval.v_string, FALSE);
7490 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007491#ifdef FEAT_FLOAT
7492 case VAR_FLOAT:
7493 *tofree = NULL;
7494 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7495 return numbuf;
7496#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007498 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007501 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007504 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007505}
7506
7507/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 * Return string "str" in ' quotes, doubling ' characters.
7509 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007511 */
7512 static char_u *
7513string_quote(str, function)
7514 char_u *str;
7515 int function;
7516{
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007518 char_u *p, *r, *s;
7519
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 len = (function ? 13 : 3);
7521 if (str != NULL)
7522 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007523 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 for (p = str; *p != NUL; mb_ptr_adv(p))
7525 if (*p == '\'')
7526 ++len;
7527 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007528 s = r = alloc(len);
7529 if (r != NULL)
7530 {
7531 if (function)
7532 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534 r += 10;
7535 }
7536 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 if (str != NULL)
7539 for (p = str; *p != NUL; )
7540 {
7541 if (*p == '\'')
7542 *r++ = '\'';
7543 MB_COPY_CHAR(p, r);
7544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007546 if (function)
7547 *r++ = ')';
7548 *r++ = NUL;
7549 }
7550 return s;
7551}
7552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007553#ifdef FEAT_FLOAT
7554/*
7555 * Convert the string "text" to a floating point number.
7556 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7557 * this always uses a decimal point.
7558 * Returns the length of the text that was consumed.
7559 */
7560 static int
7561string2float(text, value)
7562 char_u *text;
7563 float_T *value; /* result stored here */
7564{
7565 char *s = (char *)text;
7566 float_T f;
7567
7568 f = strtod(s, &s);
7569 *value = f;
7570 return (int)((char_u *)s - text);
7571}
7572#endif
7573
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 * Get the value of an environment variable.
7576 * "arg" is pointing to the '$'. It is advanced to after the name.
7577 * If the environment variable was not set, silently assume it is empty.
7578 * Always return OK.
7579 */
7580 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 int evaluate;
7585{
7586 char_u *string = NULL;
7587 int len;
7588 int cc;
7589 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007590 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
7592 ++*arg;
7593 name = *arg;
7594 len = get_env_len(arg);
7595 if (evaluate)
7596 {
7597 if (len != 0)
7598 {
7599 cc = name[len];
7600 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007601 /* first try vim_getenv(), fast for normal environment vars */
7602 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007604 {
7605 if (!mustfree)
7606 string = vim_strsave(string);
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 else
7609 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007610 if (mustfree)
7611 vim_free(string);
7612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 /* next try expanding things like $VIM and ${HOME} */
7614 string = expand_env_save(name - 1);
7615 if (string != NULL && *string == '$')
7616 {
7617 vim_free(string);
7618 string = NULL;
7619 }
7620 }
7621 name[len] = cc;
7622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 rettv->v_type = VAR_STRING;
7624 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626
7627 return OK;
7628}
7629
7630/*
7631 * Array with names and number of arguments of all internal functions
7632 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7633 */
7634static struct fst
7635{
7636 char *f_name; /* function name */
7637 char f_min_argc; /* minimal number of arguments */
7638 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007640 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641} functions[] =
7642{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007643#ifdef FEAT_FLOAT
7644 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007645 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007646#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007647 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"append", 2, 2, f_append},
7649 {"argc", 0, 0, f_argc},
7650 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007651 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007652#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007653 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007654 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007655 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007658 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {"bufexists", 1, 1, f_bufexists},
7660 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7661 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7662 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7663 {"buflisted", 1, 1, f_buflisted},
7664 {"bufloaded", 1, 1, f_bufloaded},
7665 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007666 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"bufwinnr", 1, 1, f_bufwinnr},
7668 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007669 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671#ifdef FEAT_FLOAT
7672 {"ceil", 1, 1, f_ceil},
7673#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007674 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"char2nr", 1, 1, f_char2nr},
7676 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007677 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007679#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007680 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007681 {"complete_add", 1, 1, f_complete_add},
7682 {"complete_check", 0, 0, f_complete_check},
7683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007685 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007686#ifdef FEAT_FLOAT
7687 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007688 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007689#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007692 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007693 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"delete", 1, 1, f_delete},
7695 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007696 {"diff_filler", 1, 1, f_diff_filler},
7697 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007698 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {"eventhandler", 0, 0, f_eventhandler},
7702 {"executable", 1, 1, f_executable},
7703 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007704#ifdef FEAT_FLOAT
7705 {"exp", 1, 1, f_exp},
7706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007708 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007709 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7711 {"filereadable", 1, 1, f_filereadable},
7712 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007714 {"finddir", 1, 3, f_finddir},
7715 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#ifdef FEAT_FLOAT
7717 {"float2nr", 1, 1, f_float2nr},
7718 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007719 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007720#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007721 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"fnamemodify", 2, 2, f_fnamemodify},
7723 {"foldclosed", 1, 1, f_foldclosed},
7724 {"foldclosedend", 1, 1, f_foldclosedend},
7725 {"foldlevel", 1, 1, f_foldlevel},
7726 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007727 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007730 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007731 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007732 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {"getbufvar", 2, 2, f_getbufvar},
7734 {"getchar", 0, 1, f_getchar},
7735 {"getcharmod", 0, 0, f_getcharmod},
7736 {"getcmdline", 0, 0, f_getcmdline},
7737 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007738 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007740 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007741 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {"getfsize", 1, 1, f_getfsize},
7743 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007744 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007745 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007746 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007747 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007748 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007749 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007750 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007751 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007753 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007754 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"getwinposx", 0, 0, f_getwinposx},
7756 {"getwinposy", 0, 0, f_getwinposy},
7757 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007758 {"glob", 1, 2, f_glob},
7759 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007762 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007763 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7765 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7766 {"histadd", 2, 2, f_histadd},
7767 {"histdel", 1, 2, f_histdel},
7768 {"histget", 1, 2, f_histget},
7769 {"histnr", 1, 1, f_histnr},
7770 {"hlID", 1, 1, f_hlID},
7771 {"hlexists", 1, 1, f_hlexists},
7772 {"hostname", 0, 0, f_hostname},
7773 {"iconv", 3, 3, f_iconv},
7774 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007775 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007776 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007778 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {"inputrestore", 0, 0, f_inputrestore},
7780 {"inputsave", 0, 0, f_inputsave},
7781 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007784 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007786 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 {"libcall", 3, 3, f_libcall},
7791 {"libcallnr", 3, 3, f_libcallnr},
7792 {"line", 1, 1, f_line},
7793 {"line2byte", 1, 1, f_line2byte},
7794 {"lispindent", 1, 1, f_lispindent},
7795 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007796#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007797 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007798 {"log10", 1, 1, f_log10},
7799#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007800 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007801 {"maparg", 1, 3, f_maparg},
7802 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007803 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007804 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007805 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007806 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007807 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007808 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007809 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007810 {"max", 1, 1, f_max},
7811 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007812#ifdef vim_mkdir
7813 {"mkdir", 1, 3, f_mkdir},
7814#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007816#ifdef FEAT_MZSCHEME
7817 {"mzeval", 1, 1, f_mzeval},
7818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"nextnonblank", 1, 1, f_nextnonblank},
7820 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007821 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007822#ifdef FEAT_FLOAT
7823 {"pow", 2, 2, f_pow},
7824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007826 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007827 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007829 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007830 {"reltime", 0, 2, f_reltime},
7831 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"remote_expr", 2, 3, f_remote_expr},
7833 {"remote_foreground", 1, 1, f_remote_foreground},
7834 {"remote_peek", 1, 2, f_remote_peek},
7835 {"remote_read", 1, 1, f_remote_read},
7836 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007837 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007839 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007841 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#ifdef FEAT_FLOAT
7843 {"round", 1, 1, f_round},
7844#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007845 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007846 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007847 {"searchpair", 3, 7, f_searchpair},
7848 {"searchpairpos", 3, 7, f_searchpairpos},
7849 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"server2client", 2, 2, f_server2client},
7851 {"serverlist", 0, 0, f_serverlist},
7852 {"setbufvar", 3, 3, f_setbufvar},
7853 {"setcmdpos", 1, 1, f_setcmdpos},
7854 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007855 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007857 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007858 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007860 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007861 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007863 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#ifdef FEAT_FLOAT
7866 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007869 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007870 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007871 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007872 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007873 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#ifdef FEAT_FLOAT
7875 {"sqrt", 1, 1, f_sqrt},
7876 {"str2float", 1, 1, f_str2float},
7877#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007878 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007879 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007880 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881#ifdef HAVE_STRFTIME
7882 {"strftime", 1, 2, f_strftime},
7883#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"strlen", 1, 1, f_strlen},
7887 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007888 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007890 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"submatch", 1, 1, f_submatch},
7892 {"substitute", 4, 4, f_substitute},
7893 {"synID", 3, 3, f_synID},
7894 {"synIDattr", 2, 3, f_synIDattr},
7895 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007896 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007897 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007898 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007899 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007900 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007901 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007902 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007903 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007904#ifdef FEAT_FLOAT
7905 {"tan", 1, 1, f_tan},
7906 {"tanh", 1, 1, f_tanh},
7907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007909 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"tolower", 1, 1, f_tolower},
7911 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007912 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#ifdef FEAT_FLOAT
7914 {"trunc", 1, 1, f_trunc},
7915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007917 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007918 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007919 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"virtcol", 1, 1, f_virtcol},
7921 {"visualmode", 0, 1, f_visualmode},
7922 {"winbufnr", 1, 1, f_winbufnr},
7923 {"wincol", 0, 0, f_wincol},
7924 {"winheight", 1, 1, f_winheight},
7925 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007926 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007928 {"winrestview", 1, 1, f_winrestview},
7929 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007931 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932};
7933
7934#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7935
7936/*
7937 * Function given to ExpandGeneric() to obtain the list of internal
7938 * or user defined function names.
7939 */
7940 char_u *
7941get_function_name(xp, idx)
7942 expand_T *xp;
7943 int idx;
7944{
7945 static int intidx = -1;
7946 char_u *name;
7947
7948 if (idx == 0)
7949 intidx = -1;
7950 if (intidx < 0)
7951 {
7952 name = get_user_func_name(xp, idx);
7953 if (name != NULL)
7954 return name;
7955 }
7956 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7957 {
7958 STRCPY(IObuff, functions[intidx].f_name);
7959 STRCAT(IObuff, "(");
7960 if (functions[intidx].f_max_argc == 0)
7961 STRCAT(IObuff, ")");
7962 return IObuff;
7963 }
7964
7965 return NULL;
7966}
7967
7968/*
7969 * Function given to ExpandGeneric() to obtain the list of internal or
7970 * user defined variable or function names.
7971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 char_u *
7973get_expr_name(xp, idx)
7974 expand_T *xp;
7975 int idx;
7976{
7977 static int intidx = -1;
7978 char_u *name;
7979
7980 if (idx == 0)
7981 intidx = -1;
7982 if (intidx < 0)
7983 {
7984 name = get_function_name(xp, idx);
7985 if (name != NULL)
7986 return name;
7987 }
7988 return get_user_var_name(xp, ++intidx);
7989}
7990
7991#endif /* FEAT_CMDL_COMPL */
7992
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007993#if defined(EBCDIC) || defined(PROTO)
7994/*
7995 * Compare struct fst by function name.
7996 */
7997 static int
7998compare_func_name(s1, s2)
7999 const void *s1;
8000 const void *s2;
8001{
8002 struct fst *p1 = (struct fst *)s1;
8003 struct fst *p2 = (struct fst *)s2;
8004
8005 return STRCMP(p1->f_name, p2->f_name);
8006}
8007
8008/*
8009 * Sort the function table by function name.
8010 * The sorting of the table above is ASCII dependant.
8011 * On machines using EBCDIC we have to sort it.
8012 */
8013 static void
8014sortFunctions()
8015{
8016 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8017
8018 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8019}
8020#endif
8021
8022
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023/*
8024 * Find internal function in table above.
8025 * Return index, or -1 if not found
8026 */
8027 static int
8028find_internal_func(name)
8029 char_u *name; /* name of the function */
8030{
8031 int first = 0;
8032 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8033 int cmp;
8034 int x;
8035
8036 /*
8037 * Find the function name in the table. Binary search.
8038 */
8039 while (first <= last)
8040 {
8041 x = first + ((unsigned)(last - first) >> 1);
8042 cmp = STRCMP(name, functions[x].f_name);
8043 if (cmp < 0)
8044 last = x - 1;
8045 else if (cmp > 0)
8046 first = x + 1;
8047 else
8048 return x;
8049 }
8050 return -1;
8051}
8052
8053/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8055 * name it contains, otherwise return "name".
8056 */
8057 static char_u *
8058deref_func_name(name, lenp)
8059 char_u *name;
8060 int *lenp;
8061{
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008063 int cc;
8064
8065 cc = name[*lenp];
8066 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008067 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008069 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008070 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008072 {
8073 *lenp = 0;
8074 return (char_u *)""; /* just in case */
8075 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008076 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008078 }
8079
8080 return name;
8081}
8082
8083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 * Allocate a variable for the result of a function.
8085 * Return OK or FAIL.
8086 */
8087 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8089 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 char_u *name; /* name of the function */
8091 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 char_u **arg; /* argument, pointing to the '(' */
8094 linenr_T firstline; /* first line of range */
8095 linenr_T lastline; /* last line of range */
8096 int *doesrange; /* return: function handled range */
8097 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 char_u *argp;
8101 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008102 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 int argcount = 0; /* number of arguments found */
8104
8105 /*
8106 * Get the arguments.
8107 */
8108 argp = *arg;
8109 while (argcount < MAX_FUNC_ARGS)
8110 {
8111 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8112 if (*argp == ')' || *argp == ',' || *argp == NUL)
8113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8115 {
8116 ret = FAIL;
8117 break;
8118 }
8119 ++argcount;
8120 if (*argp != ',')
8121 break;
8122 }
8123 if (*argp == ')')
8124 ++argp;
8125 else
8126 ret = FAIL;
8127
8128 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008129 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008130 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 {
8133 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008134 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008136 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138
8139 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008140 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141
8142 *arg = skipwhite(argp);
8143 return ret;
8144}
8145
8146
8147/*
8148 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008149 * Return OK when the function can't be called, FAIL otherwise.
8150 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 */
8152 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008153call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008155 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008159 typval_T *argvars; /* vars for arguments, must have "argcount"
8160 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 linenr_T firstline; /* first line of range */
8162 linenr_T lastline; /* last line of range */
8163 int *doesrange; /* return: function handled range */
8164 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008165 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168#define ERROR_UNKNOWN 0
8169#define ERROR_TOOMANY 1
8170#define ERROR_TOOFEW 2
8171#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008172#define ERROR_DICT 4
8173#define ERROR_NONE 5
8174#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 int error = ERROR_NONE;
8176 int i;
8177 int llen;
8178 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179#define FLEN_FIXED 40
8180 char_u fname_buf[FLEN_FIXED + 1];
8181 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008182 char_u *name;
8183
8184 /* Make a copy of the name, if it comes from a funcref variable it could
8185 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008186 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008187 if (name == NULL)
8188 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189
8190 /*
8191 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8192 * Change <SNR>123_name() to K_SNR 123_name().
8193 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 llen = eval_fname_script(name);
8196 if (llen > 0)
8197 {
8198 fname_buf[0] = K_SPECIAL;
8199 fname_buf[1] = KS_EXTRA;
8200 fname_buf[2] = (int)KE_SNR;
8201 i = 3;
8202 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8203 {
8204 if (current_SID <= 0)
8205 error = ERROR_SCRIPT;
8206 else
8207 {
8208 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8209 i = (int)STRLEN(fname_buf);
8210 }
8211 }
8212 if (i + STRLEN(name + llen) < FLEN_FIXED)
8213 {
8214 STRCPY(fname_buf + i, name + llen);
8215 fname = fname_buf;
8216 }
8217 else
8218 {
8219 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8220 if (fname == NULL)
8221 error = ERROR_OTHER;
8222 else
8223 {
8224 mch_memmove(fname, fname_buf, (size_t)i);
8225 STRCPY(fname + i, name + llen);
8226 }
8227 }
8228 }
8229 else
8230 fname = name;
8231
8232 *doesrange = FALSE;
8233
8234
8235 /* execute the function if no errors detected and executing */
8236 if (evaluate && error == ERROR_NONE)
8237 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008238 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8239 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 error = ERROR_UNKNOWN;
8241
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008242 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 /*
8245 * User defined function.
8246 */
8247 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008250 /* Trigger FuncUndefined event, may load the function. */
8251 if (fp == NULL
8252 && apply_autocmds(EVENT_FUNCUNDEFINED,
8253 fname, fname, TRUE, NULL)
8254 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008256 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 fp = find_func(fname);
8258 }
8259#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008260 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008261 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008262 {
8263 /* loaded a package, search for the function again */
8264 fp = find_func(fname);
8265 }
8266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (fp != NULL)
8268 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008269 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008271 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008273 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008275 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008276 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 else
8278 {
8279 /*
8280 * Call the user function.
8281 * Save and restore search patterns, script variables and
8282 * redo buffer.
8283 */
8284 save_search_patterns();
8285 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008286 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008288 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008289 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8290 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8291 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008292 /* Function was unreferenced while being used, free it
8293 * now. */
8294 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 restoreRedobuff();
8296 restore_search_patterns();
8297 error = ERROR_NONE;
8298 }
8299 }
8300 }
8301 else
8302 {
8303 /*
8304 * Find the function name in the table, call its implementation.
8305 */
8306 i = find_internal_func(fname);
8307 if (i >= 0)
8308 {
8309 if (argcount < functions[i].f_min_argc)
8310 error = ERROR_TOOFEW;
8311 else if (argcount > functions[i].f_max_argc)
8312 error = ERROR_TOOMANY;
8313 else
8314 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 error = ERROR_NONE;
8318 }
8319 }
8320 }
8321 /*
8322 * The function call (or "FuncUndefined" autocommand sequence) might
8323 * have been aborted by an error, an interrupt, or an explicitly thrown
8324 * exception that has not been caught so far. This situation can be
8325 * tested for by calling aborting(). For an error in an internal
8326 * function or for the "E132" error in call_user_func(), however, the
8327 * throw point at which the "force_abort" flag (temporarily reset by
8328 * emsg()) is normally updated has not been reached yet. We need to
8329 * update that flag first to make aborting() reliable.
8330 */
8331 update_force_abort();
8332 }
8333 if (error == ERROR_NONE)
8334 ret = OK;
8335
8336 /*
8337 * Report an error unless the argument evaluation or function call has been
8338 * cancelled due to an aborting error, an interrupt, or an exception.
8339 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008340 if (!aborting())
8341 {
8342 switch (error)
8343 {
8344 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008345 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 break;
8347 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008348 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008349 break;
8350 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008351 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008352 name);
8353 break;
8354 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008355 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008356 name);
8357 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008358 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008359 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008360 name);
8361 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008362 }
8363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 if (fname != name && fname != fname_buf)
8366 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008367 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
8369 return ret;
8370}
8371
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008372/*
8373 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008374 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008375 */
8376 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008377emsg_funcname(ermsg, name)
8378 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008379 char_u *name;
8380{
8381 char_u *p;
8382
8383 if (*name == K_SPECIAL)
8384 p = concat_str((char_u *)"<SNR>", name + 3);
8385 else
8386 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008387 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008388 if (p != name)
8389 vim_free(p);
8390}
8391
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008392/*
8393 * Return TRUE for a non-zero Number and a non-empty String.
8394 */
8395 static int
8396non_zero_arg(argvars)
8397 typval_T *argvars;
8398{
8399 return ((argvars[0].v_type == VAR_NUMBER
8400 && argvars[0].vval.v_number != 0)
8401 || (argvars[0].v_type == VAR_STRING
8402 && argvars[0].vval.v_string != NULL
8403 && *argvars[0].vval.v_string != NUL));
8404}
8405
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406/*********************************************
8407 * Implementation of the built-in functions
8408 */
8409
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008410#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008411static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8412
8413/*
8414 * Get the float value of "argvars[0]" into "f".
8415 * Returns FAIL when the argument is not a Number or Float.
8416 */
8417 static int
8418get_float_arg(argvars, f)
8419 typval_T *argvars;
8420 float_T *f;
8421{
8422 if (argvars[0].v_type == VAR_FLOAT)
8423 {
8424 *f = argvars[0].vval.v_float;
8425 return OK;
8426 }
8427 if (argvars[0].v_type == VAR_NUMBER)
8428 {
8429 *f = (float_T)argvars[0].vval.v_number;
8430 return OK;
8431 }
8432 EMSG(_("E808: Number or Float required"));
8433 return FAIL;
8434}
8435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008436/*
8437 * "abs(expr)" function
8438 */
8439 static void
8440f_abs(argvars, rettv)
8441 typval_T *argvars;
8442 typval_T *rettv;
8443{
8444 if (argvars[0].v_type == VAR_FLOAT)
8445 {
8446 rettv->v_type = VAR_FLOAT;
8447 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8448 }
8449 else
8450 {
8451 varnumber_T n;
8452 int error = FALSE;
8453
8454 n = get_tv_number_chk(&argvars[0], &error);
8455 if (error)
8456 rettv->vval.v_number = -1;
8457 else if (n > 0)
8458 rettv->vval.v_number = n;
8459 else
8460 rettv->vval.v_number = -n;
8461 }
8462}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008463
8464/*
8465 * "acos()" function
8466 */
8467 static void
8468f_acos(argvars, rettv)
8469 typval_T *argvars;
8470 typval_T *rettv;
8471{
8472 float_T f;
8473
8474 rettv->v_type = VAR_FLOAT;
8475 if (get_float_arg(argvars, &f) == OK)
8476 rettv->vval.v_float = acos(f);
8477 else
8478 rettv->vval.v_float = 0.0;
8479}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008480#endif
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008483 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 */
8485 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008487 typval_T *argvars;
8488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008493 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008495 if ((l = argvars[0].vval.v_list) != NULL
8496 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8497 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 }
8500 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008501 EMSG(_(e_listreq));
8502}
8503
8504/*
8505 * "append(lnum, string/list)" function
8506 */
8507 static void
8508f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008509 typval_T *argvars;
8510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008511{
8512 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 list_T *l = NULL;
8515 listitem_T *li = NULL;
8516 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008517 long added = 0;
8518
Bram Moolenaar0d660222005-01-07 21:51:51 +00008519 lnum = get_tv_lnum(argvars);
8520 if (lnum >= 0
8521 && lnum <= curbuf->b_ml.ml_line_count
8522 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008523 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008524 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008525 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008526 l = argvars[1].vval.v_list;
8527 if (l == NULL)
8528 return;
8529 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008531 for (;;)
8532 {
8533 if (l == NULL)
8534 tv = &argvars[1]; /* append a string */
8535 else if (li == NULL)
8536 break; /* end of list */
8537 else
8538 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 line = get_tv_string_chk(tv);
8540 if (line == NULL) /* type error */
8541 {
8542 rettv->vval.v_number = 1; /* Failed */
8543 break;
8544 }
8545 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008546 ++added;
8547 if (l == NULL)
8548 break;
8549 li = li->li_next;
8550 }
8551
8552 appended_lines_mark(lnum, added);
8553 if (curwin->w_cursor.lnum > lnum)
8554 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008556 else
8557 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
8561 * "argc()" function
8562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008565 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569}
8570
8571/*
8572 * "argidx()" function
8573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008576 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580}
8581
8582/*
8583 * "argv(nr)" function
8584 */
8585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008587 typval_T *argvars;
8588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 int idx;
8591
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008592 if (argvars[0].v_type != VAR_UNKNOWN)
8593 {
8594 idx = get_tv_number_chk(&argvars[0], NULL);
8595 if (idx >= 0 && idx < ARGCOUNT)
8596 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8597 else
8598 rettv->vval.v_string = NULL;
8599 rettv->v_type = VAR_STRING;
8600 }
8601 else if (rettv_list_alloc(rettv) == OK)
8602 for (idx = 0; idx < ARGCOUNT; ++idx)
8603 list_append_string(rettv->vval.v_list,
8604 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605}
8606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008608/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008609 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008610 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008611 static void
8612f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008613 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008615{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008616 float_T f;
8617
8618 rettv->v_type = VAR_FLOAT;
8619 if (get_float_arg(argvars, &f) == OK)
8620 rettv->vval.v_float = asin(f);
8621 else
8622 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008623}
8624
8625/*
8626 * "atan()" function
8627 */
8628 static void
8629f_atan(argvars, rettv)
8630 typval_T *argvars;
8631 typval_T *rettv;
8632{
8633 float_T f;
8634
8635 rettv->v_type = VAR_FLOAT;
8636 if (get_float_arg(argvars, &f) == OK)
8637 rettv->vval.v_float = atan(f);
8638 else
8639 rettv->vval.v_float = 0.0;
8640}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008641
8642/*
8643 * "atan2()" function
8644 */
8645 static void
8646f_atan2(argvars, rettv)
8647 typval_T *argvars;
8648 typval_T *rettv;
8649{
8650 float_T fx, fy;
8651
8652 rettv->v_type = VAR_FLOAT;
8653 if (get_float_arg(argvars, &fx) == OK
8654 && get_float_arg(&argvars[1], &fy) == OK)
8655 rettv->vval.v_float = atan2(fx, fy);
8656 else
8657 rettv->vval.v_float = 0.0;
8658}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008659#endif
8660
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661/*
8662 * "browse(save, title, initdir, default)" function
8663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008666 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669#ifdef FEAT_BROWSE
8670 int save;
8671 char_u *title;
8672 char_u *initdir;
8673 char_u *defname;
8674 char_u buf[NUMBUFLEN];
8675 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008676 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008678 save = get_tv_number_chk(&argvars[0], &error);
8679 title = get_tv_string_chk(&argvars[1]);
8680 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8681 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008683 if (error || title == NULL || initdir == NULL || defname == NULL)
8684 rettv->vval.v_string = NULL;
8685 else
8686 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008687 do_browse(save ? BROWSE_SAVE : 0,
8688 title, defname, NULL, initdir, NULL, curbuf);
8689#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008691#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008693}
8694
8695/*
8696 * "browsedir(title, initdir)" function
8697 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008702{
8703#ifdef FEAT_BROWSE
8704 char_u *title;
8705 char_u *initdir;
8706 char_u buf[NUMBUFLEN];
8707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008708 title = get_tv_string_chk(&argvars[0]);
8709 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008711 if (title == NULL || initdir == NULL)
8712 rettv->vval.v_string = NULL;
8713 else
8714 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008715 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720}
8721
Bram Moolenaar33570922005-01-25 22:26:29 +00008722static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724/*
8725 * Find a buffer by number or exact name.
8726 */
8727 static buf_T *
8728find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008733 if (avar->v_type == VAR_NUMBER)
8734 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008735 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008737 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008738 if (buf == NULL)
8739 {
8740 /* No full path name match, try a match with a URL or a "nofile"
8741 * buffer, these don't use the full path. */
8742 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8743 if (buf->b_fname != NULL
8744 && (path_with_url(buf->b_fname)
8745#ifdef FEAT_QUICKFIX
8746 || bt_nofile(buf)
8747#endif
8748 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008750 break;
8751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 }
8753 return buf;
8754}
8755
8756/*
8757 * "bufexists(expr)" function
8758 */
8759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008761 typval_T *argvars;
8762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765}
8766
8767/*
8768 * "buflisted(expr)" function
8769 */
8770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 typval_T *argvars;
8773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 buf_T *buf;
8776
8777 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
8781/*
8782 * "bufloaded(expr)" function
8783 */
8784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *argvars;
8787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 buf_T *buf;
8790
8791 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
Bram Moolenaar33570922005-01-25 22:26:29 +00008795static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797/*
8798 * Get buffer by number or pattern.
8799 */
8800 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008804 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 int save_magic;
8806 char_u *save_cpo;
8807 buf_T *buf;
8808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809 if (tv->v_type == VAR_NUMBER)
8810 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008811 if (tv->v_type != VAR_STRING)
8812 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 if (name == NULL || *name == NUL)
8814 return curbuf;
8815 if (name[0] == '$' && name[1] == NUL)
8816 return lastbuf;
8817
8818 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8819 save_magic = p_magic;
8820 p_magic = TRUE;
8821 save_cpo = p_cpo;
8822 p_cpo = (char_u *)"";
8823
8824 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8825 TRUE, FALSE));
8826
8827 p_magic = save_magic;
8828 p_cpo = save_cpo;
8829
8830 /* If not found, try expanding the name, like done for bufexists(). */
8831 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833
8834 return buf;
8835}
8836
8837/*
8838 * "bufname(expr)" function
8839 */
8840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *argvars;
8843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845 buf_T *buf;
8846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 buf = get_buf_tv(&argvars[0]);
8850 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 --emsg_off;
8856}
8857
8858/*
8859 * "bufnr(expr)" function
8860 */
8861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 typval_T *argvars;
8864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008867 int error = FALSE;
8868 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008873 --emsg_off;
8874
8875 /* If the buffer isn't found and the second argument is not zero create a
8876 * new buffer. */
8877 if (buf == NULL
8878 && argvars[1].v_type != VAR_UNKNOWN
8879 && get_tv_number_chk(&argvars[1], &error) != 0
8880 && !error
8881 && (name = get_tv_string_chk(&argvars[0])) != NULL
8882 && !error)
8883 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889}
8890
8891/*
8892 * "bufwinnr(nr)" function
8893 */
8894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 typval_T *argvars;
8897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899#ifdef FEAT_WINDOWS
8900 win_T *wp;
8901 int winnr = 0;
8902#endif
8903 buf_T *buf;
8904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908#ifdef FEAT_WINDOWS
8909 for (wp = firstwin; wp; wp = wp->w_next)
8910 {
8911 ++winnr;
8912 if (wp->w_buffer == buf)
8913 break;
8914 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918#endif
8919 --emsg_off;
8920}
8921
8922/*
8923 * "byte2line(byte)" function
8924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932#else
8933 long boff = 0;
8934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 (linenr_T)0, &boff);
8941#endif
8942}
8943
8944/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008945 * "byteidx()" function
8946 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T *argvars;
8950 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008951{
8952#ifdef FEAT_MBYTE
8953 char_u *t;
8954#endif
8955 char_u *str;
8956 long idx;
8957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 str = get_tv_string_chk(&argvars[0]);
8959 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008961 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008962 return;
8963
8964#ifdef FEAT_MBYTE
8965 t = str;
8966 for ( ; idx > 0; idx--)
8967 {
8968 if (*t == NUL) /* EOL reached */
8969 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008970 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008971 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008972 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008973#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008974 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008976#endif
8977}
8978
8979/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008980 * "call(func, arglist)" function
8981 */
8982 static void
8983f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 typval_T *argvars;
8985 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008986{
8987 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008988 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008989 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008991 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008993
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008994 if (argvars[1].v_type != VAR_LIST)
8995 {
8996 EMSG(_(e_listreq));
8997 return;
8998 }
8999 if (argvars[1].vval.v_list == NULL)
9000 return;
9001
9002 if (argvars[0].v_type == VAR_FUNC)
9003 func = argvars[0].vval.v_string;
9004 else
9005 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 if (*func == NUL)
9007 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009008
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 if (argvars[2].v_type != VAR_UNKNOWN)
9010 {
9011 if (argvars[2].v_type != VAR_DICT)
9012 {
9013 EMSG(_(e_dictreq));
9014 return;
9015 }
9016 selfdict = argvars[2].vval.v_dict;
9017 }
9018
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009019 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9020 item = item->li_next)
9021 {
9022 if (argc == MAX_FUNC_ARGS)
9023 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009024 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009025 break;
9026 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009027 /* Make a copy of each argument. This is needed to be able to set
9028 * v_lock to VAR_FIXED in the copy without changing the original list.
9029 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009030 copy_tv(&item->li_tv, &argv[argc++]);
9031 }
9032
9033 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009034 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9036 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009037
9038 /* Free the arguments. */
9039 while (argc > 0)
9040 clear_tv(&argv[--argc]);
9041}
9042
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009043#ifdef FEAT_FLOAT
9044/*
9045 * "ceil({float})" function
9046 */
9047 static void
9048f_ceil(argvars, rettv)
9049 typval_T *argvars;
9050 typval_T *rettv;
9051{
9052 float_T f;
9053
9054 rettv->v_type = VAR_FLOAT;
9055 if (get_float_arg(argvars, &f) == OK)
9056 rettv->vval.v_float = ceil(f);
9057 else
9058 rettv->vval.v_float = 0.0;
9059}
9060#endif
9061
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009062/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009063 * "changenr()" function
9064 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009065 static void
9066f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009067 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009068 typval_T *rettv;
9069{
9070 rettv->vval.v_number = curbuf->b_u_seq_cur;
9071}
9072
9073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 * "char2nr(string)" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081#ifdef FEAT_MBYTE
9082 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 else
9085#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
9090 * "cindent(lnum)" function
9091 */
9092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 typval_T *argvars;
9095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097#ifdef FEAT_CINDENT
9098 pos_T pos;
9099 linenr_T lnum;
9100
9101 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9104 {
9105 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 curwin->w_cursor = pos;
9108 }
9109 else
9110#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112}
9113
9114/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009115 * "clearmatches()" function
9116 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009117 static void
9118f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009119 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009120 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009121{
9122#ifdef FEAT_SEARCH_EXTRA
9123 clear_matches(curwin);
9124#endif
9125}
9126
9127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 * "col(string)" function
9129 */
9130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 colnr_T col = 0;
9136 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009137 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009139 fp = var2fpos(&argvars[0], FALSE, &fnum);
9140 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
9142 if (fp->col == MAXCOL)
9143 {
9144 /* '> can be MAXCOL, get the length of the line then */
9145 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009146 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 else
9148 col = MAXCOL;
9149 }
9150 else
9151 {
9152 col = fp->col + 1;
9153#ifdef FEAT_VIRTUALEDIT
9154 /* col(".") when the cursor is on the NUL at the end of the line
9155 * because of "coladd" can be seen as an extra column. */
9156 if (virtual_active() && fp == &curwin->w_cursor)
9157 {
9158 char_u *p = ml_get_cursor();
9159
9160 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9161 curwin->w_virtcol - curwin->w_cursor.coladd))
9162 {
9163# ifdef FEAT_MBYTE
9164 int l;
9165
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009166 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 col += l;
9168# else
9169 if (*p != NUL && p[1] == NUL)
9170 ++col;
9171# endif
9172 }
9173 }
9174#endif
9175 }
9176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178}
9179
Bram Moolenaar572cb562005-08-05 21:35:02 +00009180#if defined(FEAT_INS_EXPAND)
9181/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009182 * "complete()" function
9183 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009184 static void
9185f_complete(argvars, rettv)
9186 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009187 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009188{
9189 int startcol;
9190
9191 if ((State & INSERT) == 0)
9192 {
9193 EMSG(_("E785: complete() can only be used in Insert mode"));
9194 return;
9195 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009196
9197 /* Check for undo allowed here, because if something was already inserted
9198 * the line was already saved for undo and this check isn't done. */
9199 if (!undo_allowed())
9200 return;
9201
Bram Moolenaarade00832006-03-10 21:46:58 +00009202 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9203 {
9204 EMSG(_(e_invarg));
9205 return;
9206 }
9207
9208 startcol = get_tv_number_chk(&argvars[0], NULL);
9209 if (startcol <= 0)
9210 return;
9211
9212 set_completion(startcol - 1, argvars[1].vval.v_list);
9213}
9214
9215/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009216 * "complete_add()" function
9217 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009218 static void
9219f_complete_add(argvars, rettv)
9220 typval_T *argvars;
9221 typval_T *rettv;
9222{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009223 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009224}
9225
9226/*
9227 * "complete_check()" function
9228 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009229 static void
9230f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009231 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009232 typval_T *rettv;
9233{
9234 int saved = RedrawingDisabled;
9235
9236 RedrawingDisabled = 0;
9237 ins_compl_check_keys(0);
9238 rettv->vval.v_number = compl_interrupted;
9239 RedrawingDisabled = saved;
9240}
9241#endif
9242
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243/*
9244 * "confirm(message, buttons[, default [, type]])" function
9245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009248 typval_T *argvars UNUSED;
9249 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9252 char_u *message;
9253 char_u *buttons = NULL;
9254 char_u buf[NUMBUFLEN];
9255 char_u buf2[NUMBUFLEN];
9256 int def = 1;
9257 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 char_u *typestr;
9259 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 message = get_tv_string_chk(&argvars[0]);
9262 if (message == NULL)
9263 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009264 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9267 if (buttons == NULL)
9268 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009269 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009272 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009274 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9275 if (typestr == NULL)
9276 error = TRUE;
9277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 switch (TOUPPER_ASC(*typestr))
9280 {
9281 case 'E': type = VIM_ERROR; break;
9282 case 'Q': type = VIM_QUESTION; break;
9283 case 'I': type = VIM_INFO; break;
9284 case 'W': type = VIM_WARNING; break;
9285 case 'G': type = VIM_GENERIC; break;
9286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 }
9288 }
9289 }
9290 }
9291
9292 if (buttons == NULL || *buttons == NUL)
9293 buttons = (char_u *)_("&Ok");
9294
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009295 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298#endif
9299}
9300
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009301/*
9302 * "copy()" function
9303 */
9304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 typval_T *argvars;
9307 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009308{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009309 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009312#ifdef FEAT_FLOAT
9313/*
9314 * "cos()" function
9315 */
9316 static void
9317f_cos(argvars, rettv)
9318 typval_T *argvars;
9319 typval_T *rettv;
9320{
9321 float_T f;
9322
9323 rettv->v_type = VAR_FLOAT;
9324 if (get_float_arg(argvars, &f) == OK)
9325 rettv->vval.v_float = cos(f);
9326 else
9327 rettv->vval.v_float = 0.0;
9328}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009329
9330/*
9331 * "cosh()" function
9332 */
9333 static void
9334f_cosh(argvars, rettv)
9335 typval_T *argvars;
9336 typval_T *rettv;
9337{
9338 float_T f;
9339
9340 rettv->v_type = VAR_FLOAT;
9341 if (get_float_arg(argvars, &f) == OK)
9342 rettv->vval.v_float = cosh(f);
9343 else
9344 rettv->vval.v_float = 0.0;
9345}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009346#endif
9347
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009349 * "count()" function
9350 */
9351 static void
9352f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009353 typval_T *argvars;
9354 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009355{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009356 long n = 0;
9357 int ic = FALSE;
9358
Bram Moolenaare9a41262005-01-15 22:18:47 +00009359 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009360 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009361 listitem_T *li;
9362 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009363 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009364
Bram Moolenaare9a41262005-01-15 22:18:47 +00009365 if ((l = argvars[0].vval.v_list) != NULL)
9366 {
9367 li = l->lv_first;
9368 if (argvars[2].v_type != VAR_UNKNOWN)
9369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009370 int error = FALSE;
9371
9372 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009373 if (argvars[3].v_type != VAR_UNKNOWN)
9374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009375 idx = get_tv_number_chk(&argvars[3], &error);
9376 if (!error)
9377 {
9378 li = list_find(l, idx);
9379 if (li == NULL)
9380 EMSGN(_(e_listidx), idx);
9381 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009382 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009383 if (error)
9384 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385 }
9386
9387 for ( ; li != NULL; li = li->li_next)
9388 if (tv_equal(&li->li_tv, &argvars[1], ic))
9389 ++n;
9390 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 else if (argvars[0].v_type == VAR_DICT)
9393 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 int todo;
9395 dict_T *d;
9396 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009398 if ((d = argvars[0].vval.v_dict) != NULL)
9399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009400 int error = FALSE;
9401
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 if (argvars[2].v_type != VAR_UNKNOWN)
9403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 if (argvars[3].v_type != VAR_UNKNOWN)
9406 EMSG(_(e_invarg));
9407 }
9408
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009409 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009411 {
9412 if (!HASHITEM_EMPTY(hi))
9413 {
9414 --todo;
9415 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9416 ++n;
9417 }
9418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009419 }
9420 }
9421 else
9422 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009423 rettv->vval.v_number = n;
9424}
9425
9426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9428 *
9429 * Checks the existence of a cscope connection.
9430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009433 typval_T *argvars UNUSED;
9434 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436#ifdef FEAT_CSCOPE
9437 int num = 0;
9438 char_u *dbpath = NULL;
9439 char_u *prepend = NULL;
9440 char_u buf[NUMBUFLEN];
9441
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009442 if (argvars[0].v_type != VAR_UNKNOWN
9443 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 num = (int)get_tv_number(&argvars[0]);
9446 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 }
9450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452#endif
9453}
9454
9455/*
9456 * "cursor(lnum, col)" function
9457 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009458 * Moves the cursor to the specified line and column.
9459 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 typval_T *argvars;
9464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009467#ifdef FEAT_VIRTUALEDIT
9468 long coladd = 0;
9469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009471 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009472 if (argvars[1].v_type == VAR_UNKNOWN)
9473 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009474 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009475
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009476 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009477 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009478 line = pos.lnum;
9479 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009480#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009481 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009482#endif
9483 }
9484 else
9485 {
9486 line = get_tv_lnum(argvars);
9487 col = get_tv_number_chk(&argvars[1], NULL);
9488#ifdef FEAT_VIRTUALEDIT
9489 if (argvars[2].v_type != VAR_UNKNOWN)
9490 coladd = get_tv_number_chk(&argvars[2], NULL);
9491#endif
9492 }
9493 if (line < 0 || col < 0
9494#ifdef FEAT_VIRTUALEDIT
9495 || coladd < 0
9496#endif
9497 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (line > 0)
9500 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 if (col > 0)
9502 curwin->w_cursor.col = col - 1;
9503#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009504 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#endif
9506
9507 /* Make sure the cursor is in a valid position. */
9508 check_cursor();
9509#ifdef FEAT_MBYTE
9510 /* Correct cursor for multi-byte character. */
9511 if (has_mbyte)
9512 mb_adjust_cursor();
9513#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009514
9515 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009516 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517}
9518
9519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 */
9522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009524 typval_T *argvars;
9525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009527 int noref = 0;
9528
9529 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009531 if (noref < 0 || noref > 1)
9532 EMSG(_(e_invarg));
9533 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009534 {
9535 current_copyID += COPYID_INC;
9536 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
9540/*
9541 * "delete()" function
9542 */
9543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009545 typval_T *argvars;
9546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552}
9553
9554/*
9555 * "did_filetype()" function
9556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009559 typval_T *argvars UNUSED;
9560 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#endif
9565}
9566
9567/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009568 * "diff_filler()" function
9569 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009572 typval_T *argvars UNUSED;
9573 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009574{
9575#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009577#endif
9578}
9579
9580/*
9581 * "diff_hlID()" function
9582 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009585 typval_T *argvars UNUSED;
9586 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009587{
9588#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009590 static linenr_T prev_lnum = 0;
9591 static int changedtick = 0;
9592 static int fnum = 0;
9593 static int change_start = 0;
9594 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009595 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009596 int filler_lines;
9597 int col;
9598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 if (lnum < 0) /* ignore type error in {lnum} arg */
9600 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009601 if (lnum != prev_lnum
9602 || changedtick != curbuf->b_changedtick
9603 || fnum != curbuf->b_fnum)
9604 {
9605 /* New line, buffer, change: need to get the values. */
9606 filler_lines = diff_check(curwin, lnum);
9607 if (filler_lines < 0)
9608 {
9609 if (filler_lines == -1)
9610 {
9611 change_start = MAXCOL;
9612 change_end = -1;
9613 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9614 hlID = HLF_ADD; /* added line */
9615 else
9616 hlID = HLF_CHD; /* changed line */
9617 }
9618 else
9619 hlID = HLF_ADD; /* added line */
9620 }
9621 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009622 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009623 prev_lnum = lnum;
9624 changedtick = curbuf->b_changedtick;
9625 fnum = curbuf->b_fnum;
9626 }
9627
9628 if (hlID == HLF_CHD || hlID == HLF_TXD)
9629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009630 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009631 if (col >= change_start && col <= change_end)
9632 hlID = HLF_TXD; /* changed text */
9633 else
9634 hlID = HLF_CHD; /* changed line */
9635 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009636 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009637#endif
9638}
9639
9640/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009641 * "empty({expr})" function
9642 */
9643 static void
9644f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009645 typval_T *argvars;
9646 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009647{
9648 int n;
9649
9650 switch (argvars[0].v_type)
9651 {
9652 case VAR_STRING:
9653 case VAR_FUNC:
9654 n = argvars[0].vval.v_string == NULL
9655 || *argvars[0].vval.v_string == NUL;
9656 break;
9657 case VAR_NUMBER:
9658 n = argvars[0].vval.v_number == 0;
9659 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009660#ifdef FEAT_FLOAT
9661 case VAR_FLOAT:
9662 n = argvars[0].vval.v_float == 0.0;
9663 break;
9664#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009665 case VAR_LIST:
9666 n = argvars[0].vval.v_list == NULL
9667 || argvars[0].vval.v_list->lv_first == NULL;
9668 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009669 case VAR_DICT:
9670 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009673 default:
9674 EMSG2(_(e_intern2), "f_empty()");
9675 n = 0;
9676 }
9677
9678 rettv->vval.v_number = n;
9679}
9680
9681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 * "escape({string}, {chars})" function
9683 */
9684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 typval_T *argvars;
9687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
9689 char_u buf[NUMBUFLEN];
9690
Bram Moolenaar758711c2005-02-02 23:11:38 +00009691 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9692 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694}
9695
9696/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009697 * "eval()" function
9698 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009699 static void
9700f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009701 typval_T *argvars;
9702 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009703{
9704 char_u *s;
9705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009706 s = get_tv_string_chk(&argvars[0]);
9707 if (s != NULL)
9708 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9711 {
9712 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009713 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009714 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009715 else if (*s != NUL)
9716 EMSG(_(e_trailing));
9717}
9718
9719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 * "eventhandler()" function
9721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728}
9729
9730/*
9731 * "executable()" function
9732 */
9733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 typval_T *argvars;
9736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739}
9740
9741/*
9742 * "exists()" function
9743 */
9744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009746 typval_T *argvars;
9747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749 char_u *p;
9750 char_u *name;
9751 int n = FALSE;
9752 int len = 0;
9753
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009754 no_autoload = TRUE;
9755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (*p == '$') /* environment variable */
9758 {
9759 /* first try "normal" environment variables (fast) */
9760 if (mch_getenv(p + 1) != NULL)
9761 n = TRUE;
9762 else
9763 {
9764 /* try expanding things like $VIM and ${HOME} */
9765 p = expand_env_save(p);
9766 if (p != NULL && *p != '$')
9767 n = TRUE;
9768 vim_free(p);
9769 }
9770 }
9771 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009774 if (*skipwhite(p) != NUL)
9775 n = FALSE; /* trailing garbage */
9776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 else if (*p == '*') /* internal or user defined function */
9778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009779 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 }
9781 else if (*p == ':')
9782 {
9783 n = cmd_exists(p + 1);
9784 }
9785 else if (*p == '#')
9786 {
9787#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009788 if (p[1] == '#')
9789 n = autocmd_supported(p + 2);
9790 else
9791 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792#endif
9793 }
9794 else /* internal variable */
9795 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009796 char_u *tofree;
9797 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009799 /* get_name_len() takes care of expanding curly braces */
9800 name = p;
9801 len = get_name_len(&p, &tofree, TRUE, FALSE);
9802 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009804 if (tofree != NULL)
9805 name = tofree;
9806 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9807 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009809 /* handle d.key, l[idx], f(expr) */
9810 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9811 if (n)
9812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009815 if (*p != NUL)
9816 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009818 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 }
9820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009822
9823 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824}
9825
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009826#ifdef FEAT_FLOAT
9827/*
9828 * "exp()" function
9829 */
9830 static void
9831f_exp(argvars, rettv)
9832 typval_T *argvars;
9833 typval_T *rettv;
9834{
9835 float_T f;
9836
9837 rettv->v_type = VAR_FLOAT;
9838 if (get_float_arg(argvars, &f) == OK)
9839 rettv->vval.v_float = exp(f);
9840 else
9841 rettv->vval.v_float = 0.0;
9842}
9843#endif
9844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845/*
9846 * "expand()" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 typval_T *argvars;
9851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 char_u *s;
9854 int len;
9855 char_u *errormsg;
9856 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9857 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860 rettv->v_type = VAR_STRING;
9861 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 if (*s == '%' || *s == '#' || *s == '<')
9863 {
9864 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009865 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 --emsg_off;
9867 }
9868 else
9869 {
9870 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009871 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 if (argvars[1].v_type != VAR_UNKNOWN
9873 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 if (!error)
9876 {
9877 ExpandInit(&xpc);
9878 xpc.xp_context = EXPAND_FILES;
9879 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 }
9881 else
9882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 }
9884}
9885
9886/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009888 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009889 */
9890 static void
9891f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *argvars;
9893 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009896 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 list_T *l1, *l2;
9898 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 l1 = argvars[0].vval.v_list;
9903 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009904 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9905 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009906 {
9907 if (argvars[2].v_type != VAR_UNKNOWN)
9908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 before = get_tv_number_chk(&argvars[2], &error);
9910 if (error)
9911 return; /* type error; errmsg already given */
9912
Bram Moolenaar758711c2005-02-02 23:11:38 +00009913 if (before == l1->lv_len)
9914 item = NULL;
9915 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009917 item = list_find(l1, before);
9918 if (item == NULL)
9919 {
9920 EMSGN(_(e_listidx), before);
9921 return;
9922 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 }
9924 }
9925 else
9926 item = NULL;
9927 list_extend(l1, l2, item);
9928
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 copy_tv(&argvars[0], rettv);
9930 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009934 dict_T *d1, *d2;
9935 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936 char_u *action;
9937 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009939 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009940
9941 d1 = argvars[0].vval.v_dict;
9942 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009943 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9944 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945 {
9946 /* Check the third argument. */
9947 if (argvars[2].v_type != VAR_UNKNOWN)
9948 {
9949 static char *(av[]) = {"keep", "force", "error"};
9950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 action = get_tv_string_chk(&argvars[2]);
9952 if (action == NULL)
9953 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 for (i = 0; i < 3; ++i)
9955 if (STRCMP(action, av[i]) == 0)
9956 break;
9957 if (i == 3)
9958 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009959 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 return;
9961 }
9962 }
9963 else
9964 action = (char_u *)"force";
9965
9966 /* Go over all entries in the second dict and add them to the
9967 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009968 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009971 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009973 --todo;
9974 di1 = dict_find(d1, hi2->hi_key, -1);
9975 if (di1 == NULL)
9976 {
9977 di1 = dictitem_copy(HI2DI(hi2));
9978 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9979 dictitem_free(di1);
9980 }
9981 else if (*action == 'e')
9982 {
9983 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9984 break;
9985 }
9986 else if (*action == 'f')
9987 {
9988 clear_tv(&di1->di_tv);
9989 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 }
9992 }
9993
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994 copy_tv(&argvars[0], rettv);
9995 }
9996 }
9997 else
9998 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009999}
10000
10001/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010002 * "feedkeys()" function
10003 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010004 static void
10005f_feedkeys(argvars, rettv)
10006 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010007 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010008{
10009 int remap = TRUE;
10010 char_u *keys, *flags;
10011 char_u nbuf[NUMBUFLEN];
10012 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010013 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010014
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010015 /* This is not allowed in the sandbox. If the commands would still be
10016 * executed in the sandbox it would be OK, but it probably happens later,
10017 * when "sandbox" is no longer set. */
10018 if (check_secure())
10019 return;
10020
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010021 keys = get_tv_string(&argvars[0]);
10022 if (*keys != NUL)
10023 {
10024 if (argvars[1].v_type != VAR_UNKNOWN)
10025 {
10026 flags = get_tv_string_buf(&argvars[1], nbuf);
10027 for ( ; *flags != NUL; ++flags)
10028 {
10029 switch (*flags)
10030 {
10031 case 'n': remap = FALSE; break;
10032 case 'm': remap = TRUE; break;
10033 case 't': typed = TRUE; break;
10034 }
10035 }
10036 }
10037
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010038 /* Need to escape K_SPECIAL and CSI before putting the string in the
10039 * typeahead buffer. */
10040 keys_esc = vim_strsave_escape_csi(keys);
10041 if (keys_esc != NULL)
10042 {
10043 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010044 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010045 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010046 if (vgetc_busy)
10047 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010048 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010049 }
10050}
10051
10052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 * "filereadable()" function
10054 */
10055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010057 typval_T *argvars;
10058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010060 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 char_u *p;
10062 int n;
10063
Bram Moolenaarc236c162008-07-13 17:41:49 +000010064#ifndef O_NONBLOCK
10065# define O_NONBLOCK 0
10066#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010068 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10069 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
10071 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010072 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 }
10074 else
10075 n = FALSE;
10076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010081 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 * rights to write into.
10083 */
10084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010086 typval_T *argvars;
10087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010089 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090}
10091
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010092static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010093
10094 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010095findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010098 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010099{
10100#ifdef FEAT_SEARCHPATH
10101 char_u *fname;
10102 char_u *fresult = NULL;
10103 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10104 char_u *p;
10105 char_u pathbuf[NUMBUFLEN];
10106 int count = 1;
10107 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010108 int error = FALSE;
10109#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010110
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010111 rettv->vval.v_string = NULL;
10112 rettv->v_type = VAR_STRING;
10113
10114#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010116
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10120 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010121 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 else
10123 {
10124 if (*p != NUL)
10125 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010128 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010130 }
10131
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010132 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10133 error = TRUE;
10134
10135 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 do
10138 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010139 if (rettv->v_type == VAR_STRING)
10140 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 fresult = find_file_in_path_option(first ? fname : NULL,
10142 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010143 0, first, path,
10144 find_what,
10145 curbuf->b_ffname,
10146 find_what == FINDFILE_DIR
10147 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010148 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010149
10150 if (fresult != NULL && rettv->v_type == VAR_LIST)
10151 list_append_string(rettv->vval.v_list, fresult, -1);
10152
10153 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010155
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010156 if (rettv->v_type == VAR_STRING)
10157 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010158#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010159}
10160
Bram Moolenaar33570922005-01-25 22:26:29 +000010161static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10162static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010163
10164/*
10165 * Implementation of map() and filter().
10166 */
10167 static void
10168filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010169 typval_T *argvars;
10170 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010171 int map;
10172{
10173 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010174 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 listitem_T *li, *nli;
10176 list_T *l = NULL;
10177 dictitem_T *di;
10178 hashtab_T *ht;
10179 hashitem_T *hi;
10180 dict_T *d = NULL;
10181 typval_T save_val;
10182 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010184 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010185 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010186 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010187 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010188
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010190 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010191 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010192 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010193 return;
10194 }
10195 else if (argvars[0].v_type == VAR_DICT)
10196 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010197 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010198 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 return;
10200 }
10201 else
10202 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010203 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010204 return;
10205 }
10206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 expr = get_tv_string_buf_chk(&argvars[1], buf);
10208 /* On type errors, the preceding call has already displayed an error
10209 * message. Avoid a misleading error message for an empty string that
10210 * was not passed as argument. */
10211 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 prepare_vimvar(VV_VAL, &save_val);
10214 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010215
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010216 /* We reset "did_emsg" to be able to detect whether an error
10217 * occurred during evaluation of the expression. */
10218 save_did_emsg = did_emsg;
10219 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010220
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010221 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 vimvars[VV_KEY].vv_type = VAR_STRING;
10225
10226 ht = &d->dv_hashtab;
10227 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010228 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 if (!HASHITEM_EMPTY(hi))
10232 {
10233 --todo;
10234 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010235 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 break;
10237 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010238 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010239 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 break;
10241 if (!map && rem)
10242 dictitem_remove(d, di);
10243 clear_tv(&vimvars[VV_KEY].vv_tv);
10244 }
10245 }
10246 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 }
10248 else
10249 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010250 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252 for (li = l->lv_first; li != NULL; li = nli)
10253 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010254 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010255 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010257 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010258 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010259 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010260 break;
10261 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010263 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010264 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010266
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010267 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010269
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010270 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272
10273 copy_tv(&argvars[0], rettv);
10274}
10275
10276 static int
10277filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010278 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 char_u *expr;
10280 int map;
10281 int *remp;
10282{
Bram Moolenaar33570922005-01-25 22:26:29 +000010283 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010285 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 s = expr;
10289 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010290 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 if (*s != NUL) /* check for trailing chars after expr */
10292 {
10293 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010294 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 }
10296 if (map)
10297 {
10298 /* map(): replace the list item value */
10299 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010300 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301 *tv = rettv;
10302 }
10303 else
10304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 int error = FALSE;
10306
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 /* On type error, nothing has been removed; return FAIL to stop the
10311 * loop. The error message was given by get_tv_number_chk(). */
10312 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010313 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010315 retval = OK;
10316theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010317 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010318 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010319}
10320
10321/*
10322 * "filter()" function
10323 */
10324 static void
10325f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010326 typval_T *argvars;
10327 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010328{
10329 filter_map(argvars, rettv, FALSE);
10330}
10331
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010333 * "finddir({fname}[, {path}[, {count}]])" function
10334 */
10335 static void
10336f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010339{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010340 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010341}
10342
10343/*
10344 * "findfile({fname}[, {path}[, {count}]])" function
10345 */
10346 static void
10347f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 typval_T *argvars;
10349 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010350{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010351 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010352}
10353
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010354#ifdef FEAT_FLOAT
10355/*
10356 * "float2nr({float})" function
10357 */
10358 static void
10359f_float2nr(argvars, rettv)
10360 typval_T *argvars;
10361 typval_T *rettv;
10362{
10363 float_T f;
10364
10365 if (get_float_arg(argvars, &f) == OK)
10366 {
10367 if (f < -0x7fffffff)
10368 rettv->vval.v_number = -0x7fffffff;
10369 else if (f > 0x7fffffff)
10370 rettv->vval.v_number = 0x7fffffff;
10371 else
10372 rettv->vval.v_number = (varnumber_T)f;
10373 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010374}
10375
10376/*
10377 * "floor({float})" function
10378 */
10379 static void
10380f_floor(argvars, rettv)
10381 typval_T *argvars;
10382 typval_T *rettv;
10383{
10384 float_T f;
10385
10386 rettv->v_type = VAR_FLOAT;
10387 if (get_float_arg(argvars, &f) == OK)
10388 rettv->vval.v_float = floor(f);
10389 else
10390 rettv->vval.v_float = 0.0;
10391}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010392
10393/*
10394 * "fmod()" function
10395 */
10396 static void
10397f_fmod(argvars, rettv)
10398 typval_T *argvars;
10399 typval_T *rettv;
10400{
10401 float_T fx, fy;
10402
10403 rettv->v_type = VAR_FLOAT;
10404 if (get_float_arg(argvars, &fx) == OK
10405 && get_float_arg(&argvars[1], &fy) == OK)
10406 rettv->vval.v_float = fmod(fx, fy);
10407 else
10408 rettv->vval.v_float = 0.0;
10409}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010410#endif
10411
Bram Moolenaar0d660222005-01-07 21:51:51 +000010412/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010413 * "fnameescape({string})" function
10414 */
10415 static void
10416f_fnameescape(argvars, rettv)
10417 typval_T *argvars;
10418 typval_T *rettv;
10419{
10420 rettv->vval.v_string = vim_strsave_fnameescape(
10421 get_tv_string(&argvars[0]), FALSE);
10422 rettv->v_type = VAR_STRING;
10423}
10424
10425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 * "fnamemodify({fname}, {mods})" function
10427 */
10428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 char_u *fname;
10434 char_u *mods;
10435 int usedlen = 0;
10436 int len;
10437 char_u *fbuf = NULL;
10438 char_u buf[NUMBUFLEN];
10439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 fname = get_tv_string_chk(&argvars[0]);
10441 mods = get_tv_string_buf_chk(&argvars[1], buf);
10442 if (fname == NULL || mods == NULL)
10443 fname = NULL;
10444 else
10445 {
10446 len = (int)STRLEN(fname);
10447 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 vim_free(fbuf);
10456}
10457
Bram Moolenaar33570922005-01-25 22:26:29 +000010458static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
10460/*
10461 * "foldclosed()" function
10462 */
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *argvars;
10466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 int end;
10468{
10469#ifdef FEAT_FOLDING
10470 linenr_T lnum;
10471 linenr_T first, last;
10472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10475 {
10476 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10477 {
10478 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 return;
10483 }
10484 }
10485#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487}
10488
10489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010490 * "foldclosed()" function
10491 */
10492 static void
10493f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 typval_T *argvars;
10495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010496{
10497 foldclosed_both(argvars, rettv, FALSE);
10498}
10499
10500/*
10501 * "foldclosedend()" function
10502 */
10503 static void
10504f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010505 typval_T *argvars;
10506 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010507{
10508 foldclosed_both(argvars, rettv, TRUE);
10509}
10510
10511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 * "foldlevel()" function
10513 */
10514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010516 typval_T *argvars;
10517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518{
10519#ifdef FEAT_FOLDING
10520 linenr_T lnum;
10521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526}
10527
10528/*
10529 * "foldtext()" function
10530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
10536#ifdef FEAT_FOLDING
10537 linenr_T lnum;
10538 char_u *s;
10539 char_u *r;
10540 int len;
10541 char *txt;
10542#endif
10543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->v_type = VAR_STRING;
10545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10548 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10549 <= curbuf->b_ml.ml_line_count
10550 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 {
10552 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10554 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 {
10556 if (!linewhite(lnum))
10557 break;
10558 ++lnum;
10559 }
10560
10561 /* Find interesting text in this line. */
10562 s = skipwhite(ml_get(lnum));
10563 /* skip C comment-start */
10564 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010567 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010569 {
10570 s = skipwhite(ml_get(lnum + 1));
10571 if (*s == '*')
10572 s = skipwhite(s + 1);
10573 }
10574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 txt = _("+-%s%3ld lines: ");
10576 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 + 20 /* for %3ld */
10579 + STRLEN(s))); /* concatenated */
10580 if (r != NULL)
10581 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10583 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10584 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 len = (int)STRLEN(r);
10586 STRCAT(r, s);
10587 /* remove 'foldmarker' and 'commentstring' */
10588 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 }
10591 }
10592#endif
10593}
10594
10595/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010596 * "foldtextresult(lnum)" function
10597 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010600 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602{
10603#ifdef FEAT_FOLDING
10604 linenr_T lnum;
10605 char_u *text;
10606 char_u buf[51];
10607 foldinfo_T foldinfo;
10608 int fold_count;
10609#endif
10610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611 rettv->v_type = VAR_STRING;
10612 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010613#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010615 /* treat illegal types and illegal string values for {lnum} the same */
10616 if (lnum < 0)
10617 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010618 fold_count = foldedCount(curwin, lnum, &foldinfo);
10619 if (fold_count > 0)
10620 {
10621 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10622 &foldinfo, buf);
10623 if (text == buf)
10624 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010625 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010626 }
10627#endif
10628}
10629
10630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 * "foreground()" function
10632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010635 typval_T *argvars UNUSED;
10636 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638#ifdef FEAT_GUI
10639 if (gui.in_use)
10640 gui_mch_set_foreground();
10641#else
10642# ifdef WIN32
10643 win32_set_foreground();
10644# endif
10645#endif
10646}
10647
10648/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010649 * "function()" function
10650 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 typval_T *argvars;
10654 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010655{
10656 char_u *s;
10657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010659 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010660 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010661 /* Don't check an autoload name for existence here. */
10662 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010663 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010664 else
10665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 rettv->vval.v_string = vim_strsave(s);
10667 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010668 }
10669}
10670
10671/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010672 * "garbagecollect()" function
10673 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010674 static void
10675f_garbagecollect(argvars, rettv)
10676 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010677 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010678{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010679 /* This is postponed until we are back at the toplevel, because we may be
10680 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10681 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010682
10683 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10684 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010685}
10686
10687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010688 * "get()" function
10689 */
10690 static void
10691f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010692 typval_T *argvars;
10693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010694{
Bram Moolenaar33570922005-01-25 22:26:29 +000010695 listitem_T *li;
10696 list_T *l;
10697 dictitem_T *di;
10698 dict_T *d;
10699 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010700
Bram Moolenaare9a41262005-01-15 22:18:47 +000010701 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010702 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 int error = FALSE;
10706
10707 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10708 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010710 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010711 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010712 else if (argvars[0].v_type == VAR_DICT)
10713 {
10714 if ((d = argvars[0].vval.v_dict) != NULL)
10715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010716 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010717 if (di != NULL)
10718 tv = &di->di_tv;
10719 }
10720 }
10721 else
10722 EMSG2(_(e_listdictarg), "get()");
10723
10724 if (tv == NULL)
10725 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010726 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010727 copy_tv(&argvars[2], rettv);
10728 }
10729 else
10730 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010731}
10732
Bram Moolenaar342337a2005-07-21 21:11:17 +000010733static 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 +000010734
10735/*
10736 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010737 * Return a range (from start to end) of lines in rettv from the specified
10738 * buffer.
10739 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010740 */
10741 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010742get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010743 buf_T *buf;
10744 linenr_T start;
10745 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010746 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010747 typval_T *rettv;
10748{
10749 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010750
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010751 if (retlist && rettv_list_alloc(rettv) == FAIL)
10752 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010753
10754 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10755 return;
10756
10757 if (!retlist)
10758 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010759 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10760 p = ml_get_buf(buf, start, FALSE);
10761 else
10762 p = (char_u *)"";
10763
10764 rettv->v_type = VAR_STRING;
10765 rettv->vval.v_string = vim_strsave(p);
10766 }
10767 else
10768 {
10769 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010770 return;
10771
10772 if (start < 1)
10773 start = 1;
10774 if (end > buf->b_ml.ml_line_count)
10775 end = buf->b_ml.ml_line_count;
10776 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010777 if (list_append_string(rettv->vval.v_list,
10778 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010779 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010780 }
10781}
10782
10783/*
10784 * "getbufline()" function
10785 */
10786 static void
10787f_getbufline(argvars, rettv)
10788 typval_T *argvars;
10789 typval_T *rettv;
10790{
10791 linenr_T lnum;
10792 linenr_T end;
10793 buf_T *buf;
10794
10795 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10796 ++emsg_off;
10797 buf = get_buf_tv(&argvars[0]);
10798 --emsg_off;
10799
Bram Moolenaar661b1822005-07-28 22:36:45 +000010800 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010801 if (argvars[2].v_type == VAR_UNKNOWN)
10802 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010803 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010804 end = get_tv_lnum_buf(&argvars[2], buf);
10805
Bram Moolenaar342337a2005-07-21 21:11:17 +000010806 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010807}
10808
Bram Moolenaar0d660222005-01-07 21:51:51 +000010809/*
10810 * "getbufvar()" function
10811 */
10812 static void
10813f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 typval_T *argvars;
10815 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010816{
10817 buf_T *buf;
10818 buf_T *save_curbuf;
10819 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10823 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010824 ++emsg_off;
10825 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010826
10827 rettv->v_type = VAR_STRING;
10828 rettv->vval.v_string = NULL;
10829
10830 if (buf != NULL && varname != NULL)
10831 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010832 /* set curbuf to be our buf, temporarily */
10833 save_curbuf = curbuf;
10834 curbuf = buf;
10835
Bram Moolenaar0d660222005-01-07 21:51:51 +000010836 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010837 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010838 else
10839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 if (*varname == NUL)
10841 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10842 * scope prefix before the NUL byte is required by
10843 * find_var_in_ht(). */
10844 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010845 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010846 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010847 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010848 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010849 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010850
10851 /* restore previous notion of curbuf */
10852 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010853 }
10854
10855 --emsg_off;
10856}
10857
10858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 * "getchar()" function
10860 */
10861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010863 typval_T *argvars;
10864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865{
10866 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010867 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010869 /* Position the cursor. Needed after a message that ends in a space. */
10870 windgoto(msg_row, msg_col);
10871
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 ++no_mapping;
10873 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010874 for (;;)
10875 {
10876 if (argvars[0].v_type == VAR_UNKNOWN)
10877 /* getchar(): blocking wait. */
10878 n = safe_vgetc();
10879 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10880 /* getchar(1): only check if char avail */
10881 n = vpeekc();
10882 else if (error || vpeekc() == NUL)
10883 /* illegal argument or getchar(0) and no char avail: return zero */
10884 n = 0;
10885 else
10886 /* getchar(0) and char avail: return char */
10887 n = safe_vgetc();
10888 if (n == K_IGNORE)
10889 continue;
10890 break;
10891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 --no_mapping;
10893 --allow_keys;
10894
Bram Moolenaar219b8702006-11-01 14:32:36 +000010895 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10896 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10897 vimvars[VV_MOUSE_COL].vv_nr = 0;
10898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 if (IS_SPECIAL(n) || mod_mask != 0)
10901 {
10902 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10903 int i = 0;
10904
10905 /* Turn a special key into three bytes, plus modifier. */
10906 if (mod_mask != 0)
10907 {
10908 temp[i++] = K_SPECIAL;
10909 temp[i++] = KS_MODIFIER;
10910 temp[i++] = mod_mask;
10911 }
10912 if (IS_SPECIAL(n))
10913 {
10914 temp[i++] = K_SPECIAL;
10915 temp[i++] = K_SECOND(n);
10916 temp[i++] = K_THIRD(n);
10917 }
10918#ifdef FEAT_MBYTE
10919 else if (has_mbyte)
10920 i += (*mb_char2bytes)(n, temp + i);
10921#endif
10922 else
10923 temp[i++] = n;
10924 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 rettv->v_type = VAR_STRING;
10926 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010927
10928#ifdef FEAT_MOUSE
10929 if (n == K_LEFTMOUSE
10930 || n == K_LEFTMOUSE_NM
10931 || n == K_LEFTDRAG
10932 || n == K_LEFTRELEASE
10933 || n == K_LEFTRELEASE_NM
10934 || n == K_MIDDLEMOUSE
10935 || n == K_MIDDLEDRAG
10936 || n == K_MIDDLERELEASE
10937 || n == K_RIGHTMOUSE
10938 || n == K_RIGHTDRAG
10939 || n == K_RIGHTRELEASE
10940 || n == K_X1MOUSE
10941 || n == K_X1DRAG
10942 || n == K_X1RELEASE
10943 || n == K_X2MOUSE
10944 || n == K_X2DRAG
10945 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010946 || n == K_MOUSELEFT
10947 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010948 || n == K_MOUSEDOWN
10949 || n == K_MOUSEUP)
10950 {
10951 int row = mouse_row;
10952 int col = mouse_col;
10953 win_T *win;
10954 linenr_T lnum;
10955# ifdef FEAT_WINDOWS
10956 win_T *wp;
10957# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010958 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010959
10960 if (row >= 0 && col >= 0)
10961 {
10962 /* Find the window at the mouse coordinates and compute the
10963 * text position. */
10964 win = mouse_find_win(&row, &col);
10965 (void)mouse_comp_pos(win, &row, &col, &lnum);
10966# ifdef FEAT_WINDOWS
10967 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010968 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010969# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010970 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010971 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10972 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10973 }
10974 }
10975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
10977}
10978
10979/*
10980 * "getcharmod()" function
10981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
10991 * "getcmdline()" function
10992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010995 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998 rettv->v_type = VAR_STRING;
10999 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000}
11001
11002/*
11003 * "getcmdpos()" function
11004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011007 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011}
11012
11013/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011014 * "getcmdtype()" function
11015 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011016 static void
11017f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011018 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011019 typval_T *rettv;
11020{
11021 rettv->v_type = VAR_STRING;
11022 rettv->vval.v_string = alloc(2);
11023 if (rettv->vval.v_string != NULL)
11024 {
11025 rettv->vval.v_string[0] = get_cmdline_type();
11026 rettv->vval.v_string[1] = NUL;
11027 }
11028}
11029
11030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 * "getcwd()" function
11032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011035 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038 char_u cwd[MAXPATHL];
11039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 else
11044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011047 if (rettv->vval.v_string != NULL)
11048 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049#endif
11050 }
11051}
11052
11053/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011054 * "getfontname()" function
11055 */
11056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011058 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011059 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->v_type = VAR_STRING;
11062 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011063#ifdef FEAT_GUI
11064 if (gui.in_use)
11065 {
11066 GuiFont font;
11067 char_u *name = NULL;
11068
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011069 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011070 {
11071 /* Get the "Normal" font. Either the name saved by
11072 * hl_set_font_name() or from the font ID. */
11073 font = gui.norm_font;
11074 name = hl_get_font_name();
11075 }
11076 else
11077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011079 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11080 return;
11081 font = gui_mch_get_font(name, FALSE);
11082 if (font == NOFONT)
11083 return; /* Invalid font name, return empty string. */
11084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011087 gui_mch_free_font(font);
11088 }
11089#endif
11090}
11091
11092/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011093 * "getfperm({fname})" function
11094 */
11095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 typval_T *argvars;
11098 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011099{
11100 char_u *fname;
11101 struct stat st;
11102 char_u *perm = NULL;
11103 char_u flags[] = "rwx";
11104 int i;
11105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011109 if (mch_stat((char *)fname, &st) >= 0)
11110 {
11111 perm = vim_strsave((char_u *)"---------");
11112 if (perm != NULL)
11113 {
11114 for (i = 0; i < 9; i++)
11115 {
11116 if (st.st_mode & (1 << (8 - i)))
11117 perm[i] = flags[i % 3];
11118 }
11119 }
11120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011122}
11123
11124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 * "getfsize({fname})" function
11126 */
11127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
11132 char_u *fname;
11133 struct stat st;
11134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138
11139 if (mch_stat((char *)fname, &st) >= 0)
11140 {
11141 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011146
11147 /* non-perfect check for overflow */
11148 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11149 rettv->vval.v_number = -2;
11150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 }
11152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154}
11155
11156/*
11157 * "getftime({fname})" function
11158 */
11159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011161 typval_T *argvars;
11162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163{
11164 char_u *fname;
11165 struct stat st;
11166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168
11169 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173}
11174
11175/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011176 * "getftype({fname})" function
11177 */
11178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011180 typval_T *argvars;
11181 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011182{
11183 char_u *fname;
11184 struct stat st;
11185 char_u *type = NULL;
11186 char *t;
11187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011191 if (mch_lstat((char *)fname, &st) >= 0)
11192 {
11193#ifdef S_ISREG
11194 if (S_ISREG(st.st_mode))
11195 t = "file";
11196 else if (S_ISDIR(st.st_mode))
11197 t = "dir";
11198# ifdef S_ISLNK
11199 else if (S_ISLNK(st.st_mode))
11200 t = "link";
11201# endif
11202# ifdef S_ISBLK
11203 else if (S_ISBLK(st.st_mode))
11204 t = "bdev";
11205# endif
11206# ifdef S_ISCHR
11207 else if (S_ISCHR(st.st_mode))
11208 t = "cdev";
11209# endif
11210# ifdef S_ISFIFO
11211 else if (S_ISFIFO(st.st_mode))
11212 t = "fifo";
11213# endif
11214# ifdef S_ISSOCK
11215 else if (S_ISSOCK(st.st_mode))
11216 t = "fifo";
11217# endif
11218 else
11219 t = "other";
11220#else
11221# ifdef S_IFMT
11222 switch (st.st_mode & S_IFMT)
11223 {
11224 case S_IFREG: t = "file"; break;
11225 case S_IFDIR: t = "dir"; break;
11226# ifdef S_IFLNK
11227 case S_IFLNK: t = "link"; break;
11228# endif
11229# ifdef S_IFBLK
11230 case S_IFBLK: t = "bdev"; break;
11231# endif
11232# ifdef S_IFCHR
11233 case S_IFCHR: t = "cdev"; break;
11234# endif
11235# ifdef S_IFIFO
11236 case S_IFIFO: t = "fifo"; break;
11237# endif
11238# ifdef S_IFSOCK
11239 case S_IFSOCK: t = "socket"; break;
11240# endif
11241 default: t = "other";
11242 }
11243# else
11244 if (mch_isdir(fname))
11245 t = "dir";
11246 else
11247 t = "file";
11248# endif
11249#endif
11250 type = vim_strsave((char_u *)t);
11251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011253}
11254
11255/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011256 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011257 */
11258 static void
11259f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011262{
11263 linenr_T lnum;
11264 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011265 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266
11267 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011268 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011269 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011270 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011271 retlist = FALSE;
11272 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011273 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011274 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011275 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011276 retlist = TRUE;
11277 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011278
Bram Moolenaar342337a2005-07-21 21:11:17 +000011279 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280}
11281
11282/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011283 * "getmatches()" function
11284 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011285 static void
11286f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011287 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011288 typval_T *rettv;
11289{
11290#ifdef FEAT_SEARCH_EXTRA
11291 dict_T *dict;
11292 matchitem_T *cur = curwin->w_match_head;
11293
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011294 if (rettv_list_alloc(rettv) == OK)
11295 {
11296 while (cur != NULL)
11297 {
11298 dict = dict_alloc();
11299 if (dict == NULL)
11300 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011301 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11302 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11303 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11304 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11305 list_append_dict(rettv->vval.v_list, dict);
11306 cur = cur->next;
11307 }
11308 }
11309#endif
11310}
11311
11312/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011313 * "getpid()" function
11314 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011315 static void
11316f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011317 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011318 typval_T *rettv;
11319{
11320 rettv->vval.v_number = mch_get_pid();
11321}
11322
11323/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011324 * "getpos(string)" function
11325 */
11326 static void
11327f_getpos(argvars, rettv)
11328 typval_T *argvars;
11329 typval_T *rettv;
11330{
11331 pos_T *fp;
11332 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011333 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011334
11335 if (rettv_list_alloc(rettv) == OK)
11336 {
11337 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011338 fp = var2fpos(&argvars[0], TRUE, &fnum);
11339 if (fnum != -1)
11340 list_append_number(l, (varnumber_T)fnum);
11341 else
11342 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011343 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11344 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011345 list_append_number(l, (fp != NULL)
11346 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011347 : (varnumber_T)0);
11348 list_append_number(l,
11349#ifdef FEAT_VIRTUALEDIT
11350 (fp != NULL) ? (varnumber_T)fp->coladd :
11351#endif
11352 (varnumber_T)0);
11353 }
11354 else
11355 rettv->vval.v_number = FALSE;
11356}
11357
11358/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011359 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011360 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011361 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011362f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011363 typval_T *argvars UNUSED;
11364 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011365{
11366#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011367 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011368#endif
11369
Bram Moolenaar2641f772005-03-25 21:58:17 +000011370#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011371 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011372 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011373 wp = NULL;
11374 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11375 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011376 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011377 if (wp == NULL)
11378 return;
11379 }
11380
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011381 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011382 }
11383#endif
11384}
11385
11386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 * "getreg()" function
11388 */
11389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *argvars;
11392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393{
11394 char_u *strregname;
11395 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011396 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011399 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 strregname = get_tv_string_chk(&argvars[0]);
11402 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011403 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011407 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 regname = (strregname == NULL ? '"' : *strregname);
11409 if (regname == 0)
11410 regname = '"';
11411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 rettv->vval.v_string = error ? NULL :
11414 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
11417/*
11418 * "getregtype()" function
11419 */
11420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424{
11425 char_u *strregname;
11426 int regname;
11427 char_u buf[NUMBUFLEN + 2];
11428 long reglen = 0;
11429
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011430 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 {
11432 strregname = get_tv_string_chk(&argvars[0]);
11433 if (strregname == NULL) /* type error; errmsg already given */
11434 {
11435 rettv->v_type = VAR_STRING;
11436 rettv->vval.v_string = NULL;
11437 return;
11438 }
11439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 else
11441 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443
11444 regname = (strregname == NULL ? '"' : *strregname);
11445 if (regname == 0)
11446 regname = '"';
11447
11448 buf[0] = NUL;
11449 buf[1] = NUL;
11450 switch (get_reg_type(regname, &reglen))
11451 {
11452 case MLINE: buf[0] = 'V'; break;
11453 case MCHAR: buf[0] = 'v'; break;
11454#ifdef FEAT_VISUAL
11455 case MBLOCK:
11456 buf[0] = Ctrl_V;
11457 sprintf((char *)buf + 1, "%ld", reglen + 1);
11458 break;
11459#endif
11460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->v_type = VAR_STRING;
11462 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
11465/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011466 * "gettabvar()" function
11467 */
11468 static void
11469f_gettabvar(argvars, rettv)
11470 typval_T *argvars;
11471 typval_T *rettv;
11472{
11473 tabpage_T *tp;
11474 dictitem_T *v;
11475 char_u *varname;
11476
11477 rettv->v_type = VAR_STRING;
11478 rettv->vval.v_string = NULL;
11479
11480 varname = get_tv_string_chk(&argvars[1]);
11481 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11482 if (tp != NULL && varname != NULL)
11483 {
11484 /* look up the variable */
11485 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11486 if (v != NULL)
11487 copy_tv(&v->di_tv, rettv);
11488 }
11489}
11490
11491/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011492 * "gettabwinvar()" function
11493 */
11494 static void
11495f_gettabwinvar(argvars, rettv)
11496 typval_T *argvars;
11497 typval_T *rettv;
11498{
11499 getwinvar(argvars, rettv, 1);
11500}
11501
11502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 * "getwinposx()" function
11504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506f_getwinposx(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 = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519#endif
11520}
11521
11522/*
11523 * "getwinposy()" function
11524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011527 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531#ifdef FEAT_GUI
11532 if (gui.in_use)
11533 {
11534 int x, y;
11535
11536 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 }
11539#endif
11540}
11541
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011542/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011543 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011544 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011545 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011546find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011547 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011548 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011549{
11550#ifdef FEAT_WINDOWS
11551 win_T *wp;
11552#endif
11553 int nr;
11554
11555 nr = get_tv_number_chk(vp, NULL);
11556
11557#ifdef FEAT_WINDOWS
11558 if (nr < 0)
11559 return NULL;
11560 if (nr == 0)
11561 return curwin;
11562
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011563 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11564 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011565 if (--nr <= 0)
11566 break;
11567 return wp;
11568#else
11569 if (nr == 0 || nr == 1)
11570 return curwin;
11571 return NULL;
11572#endif
11573}
11574
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575/*
11576 * "getwinvar()" function
11577 */
11578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 typval_T *argvars;
11581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011583 getwinvar(argvars, rettv, 0);
11584}
11585
11586/*
11587 * getwinvar() and gettabwinvar()
11588 */
11589 static void
11590getwinvar(argvars, rettv, off)
11591 typval_T *argvars;
11592 typval_T *rettv;
11593 int off; /* 1 for gettabwinvar() */
11594{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 win_T *win, *oldcurwin;
11596 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011598 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011600#ifdef FEAT_WINDOWS
11601 if (off == 1)
11602 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11603 else
11604 tp = curtab;
11605#endif
11606 win = find_win_by_nr(&argvars[off], tp);
11607 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->v_type = VAR_STRING;
11611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612
11613 if (win != NULL && varname != NULL)
11614 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011615 /* Set curwin to be our win, temporarily. Also set curbuf, so
11616 * that we can get buffer-local options. */
11617 oldcurwin = curwin;
11618 curwin = win;
11619 curbuf = win->w_buffer;
11620
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 else
11624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 if (*varname == NUL)
11626 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11627 * scope prefix before the NUL byte is required by
11628 * find_var_in_ht(). */
11629 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011631 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011635
11636 /* restore previous notion of curwin */
11637 curwin = oldcurwin;
11638 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 }
11640
11641 --emsg_off;
11642}
11643
11644/*
11645 * "glob()" function
11646 */
11647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011648f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011649 typval_T *argvars;
11650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011652 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011654 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011656 /* When the optional second argument is non-zero, don't remove matches
11657 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11658 if (argvars[1].v_type != VAR_UNKNOWN
11659 && get_tv_number_chk(&argvars[1], &error))
11660 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011662 if (!error)
11663 {
11664 ExpandInit(&xpc);
11665 xpc.xp_context = EXPAND_FILES;
11666 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11667 NULL, flags, WILD_ALL);
11668 }
11669 else
11670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671}
11672
11673/*
11674 * "globpath()" function
11675 */
11676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *argvars;
11679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011681 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011684 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011686 /* When the optional second argument is non-zero, don't remove matches
11687 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11688 if (argvars[2].v_type != VAR_UNKNOWN
11689 && get_tv_number_chk(&argvars[2], &error))
11690 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011692 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 rettv->vval.v_string = NULL;
11694 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011695 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11696 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697}
11698
11699/*
11700 * "has()" function
11701 */
11702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011704 typval_T *argvars;
11705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706{
11707 int i;
11708 char_u *name;
11709 int n = FALSE;
11710 static char *(has_list[]) =
11711 {
11712#ifdef AMIGA
11713 "amiga",
11714# ifdef FEAT_ARP
11715 "arp",
11716# endif
11717#endif
11718#ifdef __BEOS__
11719 "beos",
11720#endif
11721#ifdef MSDOS
11722# ifdef DJGPP
11723 "dos32",
11724# else
11725 "dos16",
11726# endif
11727#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011728#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 "mac",
11730#endif
11731#if defined(MACOS_X_UNIX)
11732 "macunix",
11733#endif
11734#ifdef OS2
11735 "os2",
11736#endif
11737#ifdef __QNX__
11738 "qnx",
11739#endif
11740#ifdef RISCOS
11741 "riscos",
11742#endif
11743#ifdef UNIX
11744 "unix",
11745#endif
11746#ifdef VMS
11747 "vms",
11748#endif
11749#ifdef WIN16
11750 "win16",
11751#endif
11752#ifdef WIN32
11753 "win32",
11754#endif
11755#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11756 "win32unix",
11757#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011758#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 "win64",
11760#endif
11761#ifdef EBCDIC
11762 "ebcdic",
11763#endif
11764#ifndef CASE_INSENSITIVE_FILENAME
11765 "fname_case",
11766#endif
11767#ifdef FEAT_ARABIC
11768 "arabic",
11769#endif
11770#ifdef FEAT_AUTOCMD
11771 "autocmd",
11772#endif
11773#ifdef FEAT_BEVAL
11774 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011775# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11776 "balloon_multiline",
11777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778#endif
11779#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11780 "builtin_terms",
11781# ifdef ALL_BUILTIN_TCAPS
11782 "all_builtin_terms",
11783# endif
11784#endif
11785#ifdef FEAT_BYTEOFF
11786 "byte_offset",
11787#endif
11788#ifdef FEAT_CINDENT
11789 "cindent",
11790#endif
11791#ifdef FEAT_CLIENTSERVER
11792 "clientserver",
11793#endif
11794#ifdef FEAT_CLIPBOARD
11795 "clipboard",
11796#endif
11797#ifdef FEAT_CMDL_COMPL
11798 "cmdline_compl",
11799#endif
11800#ifdef FEAT_CMDHIST
11801 "cmdline_hist",
11802#endif
11803#ifdef FEAT_COMMENTS
11804 "comments",
11805#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011806#ifdef FEAT_CONCEAL
11807 "conceal",
11808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809#ifdef FEAT_CRYPT
11810 "cryptv",
11811#endif
11812#ifdef FEAT_CSCOPE
11813 "cscope",
11814#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011815#ifdef FEAT_CURSORBIND
11816 "cursorbind",
11817#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011818#ifdef CURSOR_SHAPE
11819 "cursorshape",
11820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821#ifdef DEBUG
11822 "debug",
11823#endif
11824#ifdef FEAT_CON_DIALOG
11825 "dialog_con",
11826#endif
11827#ifdef FEAT_GUI_DIALOG
11828 "dialog_gui",
11829#endif
11830#ifdef FEAT_DIFF
11831 "diff",
11832#endif
11833#ifdef FEAT_DIGRAPHS
11834 "digraphs",
11835#endif
11836#ifdef FEAT_DND
11837 "dnd",
11838#endif
11839#ifdef FEAT_EMACS_TAGS
11840 "emacs_tags",
11841#endif
11842 "eval", /* always present, of course! */
11843#ifdef FEAT_EX_EXTRA
11844 "ex_extra",
11845#endif
11846#ifdef FEAT_SEARCH_EXTRA
11847 "extra_search",
11848#endif
11849#ifdef FEAT_FKMAP
11850 "farsi",
11851#endif
11852#ifdef FEAT_SEARCHPATH
11853 "file_in_path",
11854#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011855#if defined(UNIX) && !defined(USE_SYSTEM)
11856 "filterpipe",
11857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858#ifdef FEAT_FIND_ID
11859 "find_in_path",
11860#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011861#ifdef FEAT_FLOAT
11862 "float",
11863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864#ifdef FEAT_FOLDING
11865 "folding",
11866#endif
11867#ifdef FEAT_FOOTER
11868 "footer",
11869#endif
11870#if !defined(USE_SYSTEM) && defined(UNIX)
11871 "fork",
11872#endif
11873#ifdef FEAT_GETTEXT
11874 "gettext",
11875#endif
11876#ifdef FEAT_GUI
11877 "gui",
11878#endif
11879#ifdef FEAT_GUI_ATHENA
11880# ifdef FEAT_GUI_NEXTAW
11881 "gui_neXtaw",
11882# else
11883 "gui_athena",
11884# endif
11885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886#ifdef FEAT_GUI_GTK
11887 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011890#ifdef FEAT_GUI_GNOME
11891 "gui_gnome",
11892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893#ifdef FEAT_GUI_MAC
11894 "gui_mac",
11895#endif
11896#ifdef FEAT_GUI_MOTIF
11897 "gui_motif",
11898#endif
11899#ifdef FEAT_GUI_PHOTON
11900 "gui_photon",
11901#endif
11902#ifdef FEAT_GUI_W16
11903 "gui_win16",
11904#endif
11905#ifdef FEAT_GUI_W32
11906 "gui_win32",
11907#endif
11908#ifdef FEAT_HANGULIN
11909 "hangul_input",
11910#endif
11911#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11912 "iconv",
11913#endif
11914#ifdef FEAT_INS_EXPAND
11915 "insert_expand",
11916#endif
11917#ifdef FEAT_JUMPLIST
11918 "jumplist",
11919#endif
11920#ifdef FEAT_KEYMAP
11921 "keymap",
11922#endif
11923#ifdef FEAT_LANGMAP
11924 "langmap",
11925#endif
11926#ifdef FEAT_LIBCALL
11927 "libcall",
11928#endif
11929#ifdef FEAT_LINEBREAK
11930 "linebreak",
11931#endif
11932#ifdef FEAT_LISP
11933 "lispindent",
11934#endif
11935#ifdef FEAT_LISTCMDS
11936 "listcmds",
11937#endif
11938#ifdef FEAT_LOCALMAP
11939 "localmap",
11940#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011941#ifdef FEAT_LUA
11942# ifndef DYNAMIC_LUA
11943 "lua",
11944# endif
11945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946#ifdef FEAT_MENU
11947 "menu",
11948#endif
11949#ifdef FEAT_SESSION
11950 "mksession",
11951#endif
11952#ifdef FEAT_MODIFY_FNAME
11953 "modify_fname",
11954#endif
11955#ifdef FEAT_MOUSE
11956 "mouse",
11957#endif
11958#ifdef FEAT_MOUSESHAPE
11959 "mouseshape",
11960#endif
11961#if defined(UNIX) || defined(VMS)
11962# ifdef FEAT_MOUSE_DEC
11963 "mouse_dec",
11964# endif
11965# ifdef FEAT_MOUSE_GPM
11966 "mouse_gpm",
11967# endif
11968# ifdef FEAT_MOUSE_JSB
11969 "mouse_jsbterm",
11970# endif
11971# ifdef FEAT_MOUSE_NET
11972 "mouse_netterm",
11973# endif
11974# ifdef FEAT_MOUSE_PTERM
11975 "mouse_pterm",
11976# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011977# ifdef FEAT_SYSMOUSE
11978 "mouse_sysmouse",
11979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980# ifdef FEAT_MOUSE_XTERM
11981 "mouse_xterm",
11982# endif
11983#endif
11984#ifdef FEAT_MBYTE
11985 "multi_byte",
11986#endif
11987#ifdef FEAT_MBYTE_IME
11988 "multi_byte_ime",
11989#endif
11990#ifdef FEAT_MULTI_LANG
11991 "multi_lang",
11992#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011993#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011994#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011995 "mzscheme",
11996#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998#ifdef FEAT_OLE
11999 "ole",
12000#endif
12001#ifdef FEAT_OSFILETYPE
12002 "osfiletype",
12003#endif
12004#ifdef FEAT_PATH_EXTRA
12005 "path_extra",
12006#endif
12007#ifdef FEAT_PERL
12008#ifndef DYNAMIC_PERL
12009 "perl",
12010#endif
12011#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012012#ifdef FEAT_PERSISTENT_UNDO
12013 "persistent_undo",
12014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015#ifdef FEAT_PYTHON
12016#ifndef DYNAMIC_PYTHON
12017 "python",
12018#endif
12019#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012020#ifdef FEAT_PYTHON3
12021#ifndef DYNAMIC_PYTHON3
12022 "python3",
12023#endif
12024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025#ifdef FEAT_POSTSCRIPT
12026 "postscript",
12027#endif
12028#ifdef FEAT_PRINTER
12029 "printer",
12030#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012031#ifdef FEAT_PROFILE
12032 "profile",
12033#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012034#ifdef FEAT_RELTIME
12035 "reltime",
12036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037#ifdef FEAT_QUICKFIX
12038 "quickfix",
12039#endif
12040#ifdef FEAT_RIGHTLEFT
12041 "rightleft",
12042#endif
12043#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12044 "ruby",
12045#endif
12046#ifdef FEAT_SCROLLBIND
12047 "scrollbind",
12048#endif
12049#ifdef FEAT_CMDL_INFO
12050 "showcmd",
12051 "cmdline_info",
12052#endif
12053#ifdef FEAT_SIGNS
12054 "signs",
12055#endif
12056#ifdef FEAT_SMARTINDENT
12057 "smartindent",
12058#endif
12059#ifdef FEAT_SNIFF
12060 "sniff",
12061#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012062#ifdef STARTUPTIME
12063 "startuptime",
12064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065#ifdef FEAT_STL_OPT
12066 "statusline",
12067#endif
12068#ifdef FEAT_SUN_WORKSHOP
12069 "sun_workshop",
12070#endif
12071#ifdef FEAT_NETBEANS_INTG
12072 "netbeans_intg",
12073#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012074#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012075 "spell",
12076#endif
12077#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 "syntax",
12079#endif
12080#if defined(USE_SYSTEM) || !defined(UNIX)
12081 "system",
12082#endif
12083#ifdef FEAT_TAG_BINS
12084 "tag_binary",
12085#endif
12086#ifdef FEAT_TAG_OLDSTATIC
12087 "tag_old_static",
12088#endif
12089#ifdef FEAT_TAG_ANYWHITE
12090 "tag_any_white",
12091#endif
12092#ifdef FEAT_TCL
12093# ifndef DYNAMIC_TCL
12094 "tcl",
12095# endif
12096#endif
12097#ifdef TERMINFO
12098 "terminfo",
12099#endif
12100#ifdef FEAT_TERMRESPONSE
12101 "termresponse",
12102#endif
12103#ifdef FEAT_TEXTOBJ
12104 "textobjects",
12105#endif
12106#ifdef HAVE_TGETENT
12107 "tgetent",
12108#endif
12109#ifdef FEAT_TITLE
12110 "title",
12111#endif
12112#ifdef FEAT_TOOLBAR
12113 "toolbar",
12114#endif
12115#ifdef FEAT_USR_CMDS
12116 "user-commands", /* was accidentally included in 5.4 */
12117 "user_commands",
12118#endif
12119#ifdef FEAT_VIMINFO
12120 "viminfo",
12121#endif
12122#ifdef FEAT_VERTSPLIT
12123 "vertsplit",
12124#endif
12125#ifdef FEAT_VIRTUALEDIT
12126 "virtualedit",
12127#endif
12128#ifdef FEAT_VISUAL
12129 "visual",
12130#endif
12131#ifdef FEAT_VISUALEXTRA
12132 "visualextra",
12133#endif
12134#ifdef FEAT_VREPLACE
12135 "vreplace",
12136#endif
12137#ifdef FEAT_WILDIGN
12138 "wildignore",
12139#endif
12140#ifdef FEAT_WILDMENU
12141 "wildmenu",
12142#endif
12143#ifdef FEAT_WINDOWS
12144 "windows",
12145#endif
12146#ifdef FEAT_WAK
12147 "winaltkeys",
12148#endif
12149#ifdef FEAT_WRITEBACKUP
12150 "writebackup",
12151#endif
12152#ifdef FEAT_XIM
12153 "xim",
12154#endif
12155#ifdef FEAT_XFONTSET
12156 "xfontset",
12157#endif
12158#ifdef USE_XSMP
12159 "xsmp",
12160#endif
12161#ifdef USE_XSMP_INTERACT
12162 "xsmp_interact",
12163#endif
12164#ifdef FEAT_XCLIPBOARD
12165 "xterm_clipboard",
12166#endif
12167#ifdef FEAT_XTERM_SAVE
12168 "xterm_save",
12169#endif
12170#if defined(UNIX) && defined(FEAT_X11)
12171 "X11",
12172#endif
12173 NULL
12174 };
12175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 for (i = 0; has_list[i] != NULL; ++i)
12178 if (STRICMP(name, has_list[i]) == 0)
12179 {
12180 n = TRUE;
12181 break;
12182 }
12183
12184 if (n == FALSE)
12185 {
12186 if (STRNICMP(name, "patch", 5) == 0)
12187 n = has_patch(atoi((char *)name + 5));
12188 else if (STRICMP(name, "vim_starting") == 0)
12189 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012190#ifdef FEAT_MBYTE
12191 else if (STRICMP(name, "multi_byte_encoding") == 0)
12192 n = has_mbyte;
12193#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012194#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12195 else if (STRICMP(name, "balloon_multiline") == 0)
12196 n = multiline_balloon_available();
12197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198#ifdef DYNAMIC_TCL
12199 else if (STRICMP(name, "tcl") == 0)
12200 n = tcl_enabled(FALSE);
12201#endif
12202#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12203 else if (STRICMP(name, "iconv") == 0)
12204 n = iconv_enabled(FALSE);
12205#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012206#ifdef DYNAMIC_LUA
12207 else if (STRICMP(name, "lua") == 0)
12208 n = lua_enabled(FALSE);
12209#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012210#ifdef DYNAMIC_MZSCHEME
12211 else if (STRICMP(name, "mzscheme") == 0)
12212 n = mzscheme_enabled(FALSE);
12213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214#ifdef DYNAMIC_RUBY
12215 else if (STRICMP(name, "ruby") == 0)
12216 n = ruby_enabled(FALSE);
12217#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012218#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219#ifdef DYNAMIC_PYTHON
12220 else if (STRICMP(name, "python") == 0)
12221 n = python_enabled(FALSE);
12222#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012223#endif
12224#ifdef FEAT_PYTHON3
12225#ifdef DYNAMIC_PYTHON3
12226 else if (STRICMP(name, "python3") == 0)
12227 n = python3_enabled(FALSE);
12228#endif
12229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230#ifdef DYNAMIC_PERL
12231 else if (STRICMP(name, "perl") == 0)
12232 n = perl_enabled(FALSE);
12233#endif
12234#ifdef FEAT_GUI
12235 else if (STRICMP(name, "gui_running") == 0)
12236 n = (gui.in_use || gui.starting);
12237# ifdef FEAT_GUI_W32
12238 else if (STRICMP(name, "gui_win32s") == 0)
12239 n = gui_is_win32s();
12240# endif
12241# ifdef FEAT_BROWSE
12242 else if (STRICMP(name, "browse") == 0)
12243 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12244# endif
12245#endif
12246#ifdef FEAT_SYN_HL
12247 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012248 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249#endif
12250#if defined(WIN3264)
12251 else if (STRICMP(name, "win95") == 0)
12252 n = mch_windows95();
12253#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012254#ifdef FEAT_NETBEANS_INTG
12255 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012256 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 }
12259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261}
12262
12263/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012264 * "has_key()" function
12265 */
12266 static void
12267f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 typval_T *argvars;
12269 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012270{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012271 if (argvars[0].v_type != VAR_DICT)
12272 {
12273 EMSG(_(e_dictreq));
12274 return;
12275 }
12276 if (argvars[0].vval.v_dict == NULL)
12277 return;
12278
12279 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012280 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012281}
12282
12283/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012284 * "haslocaldir()" function
12285 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012286 static void
12287f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012288 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012289 typval_T *rettv;
12290{
12291 rettv->vval.v_number = (curwin->w_localdir != NULL);
12292}
12293
12294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 * "hasmapto()" function
12296 */
12297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012299 typval_T *argvars;
12300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301{
12302 char_u *name;
12303 char_u *mode;
12304 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012305 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012308 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 mode = (char_u *)"nvo";
12310 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012313 if (argvars[2].v_type != VAR_UNKNOWN)
12314 abbr = get_tv_number(&argvars[2]);
12315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316
Bram Moolenaar2c932302006-03-18 21:42:09 +000012317 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321}
12322
12323/*
12324 * "histadd()" function
12325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012328 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331#ifdef FEAT_CMDHIST
12332 int histype;
12333 char_u *str;
12334 char_u buf[NUMBUFLEN];
12335#endif
12336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 if (check_restricted() || check_secure())
12339 return;
12340#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12342 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 if (histype >= 0)
12344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 if (*str != NUL)
12347 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012348 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 return;
12352 }
12353 }
12354#endif
12355}
12356
12357/*
12358 * "histdel()" function
12359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012362 typval_T *argvars UNUSED;
12363 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365#ifdef FEAT_CMDHIST
12366 int n;
12367 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012370 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12371 if (str == NULL)
12372 n = 0;
12373 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012375 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012376 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 else
12381 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 get_tv_string_buf(&argvars[1], buf));
12384 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385#endif
12386}
12387
12388/*
12389 * "histget()" function
12390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012393 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395{
12396#ifdef FEAT_CMDHIST
12397 int type;
12398 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012399 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12402 if (str == NULL)
12403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 {
12406 type = get_histtype(str);
12407 if (argvars[1].v_type == VAR_UNKNOWN)
12408 idx = get_history_idx(type);
12409 else
12410 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12411 /* -1 on type error */
12412 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418}
12419
12420/*
12421 * "histnr()" function
12422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012425 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427{
12428 int i;
12429
12430#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 char_u *history = get_tv_string_chk(&argvars[0]);
12432
12433 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 if (i >= HIST_CMD && i < HIST_COUNT)
12435 i = get_history_idx(i);
12436 else
12437#endif
12438 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
12441
12442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443 * "highlightID(name)" function
12444 */
12445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012447 typval_T *argvars;
12448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451}
12452
12453/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012454 * "highlight_exists()" function
12455 */
12456 static void
12457f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012458 typval_T *argvars;
12459 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012460{
12461 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12462}
12463
12464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 * "hostname()" function
12466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012469 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471{
12472 char_u hostname[256];
12473
12474 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->v_type = VAR_STRING;
12476 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477}
12478
12479/*
12480 * iconv() function
12481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486{
12487#ifdef FEAT_MBYTE
12488 char_u buf1[NUMBUFLEN];
12489 char_u buf2[NUMBUFLEN];
12490 char_u *from, *to, *str;
12491 vimconv_T vimconv;
12492#endif
12493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->v_type = VAR_STRING;
12495 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496
12497#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 str = get_tv_string(&argvars[0]);
12499 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12500 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 vimconv.vc_type = CONV_NONE;
12502 convert_setup(&vimconv, from, to);
12503
12504 /* If the encodings are equal, no conversion needed. */
12505 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509
12510 convert_setup(&vimconv, NULL, NULL);
12511 vim_free(from);
12512 vim_free(to);
12513#endif
12514}
12515
12516/*
12517 * "indent()" function
12518 */
12519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012520f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 typval_T *argvars;
12522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523{
12524 linenr_T lnum;
12525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531}
12532
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012533/*
12534 * "index()" function
12535 */
12536 static void
12537f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012538 typval_T *argvars;
12539 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012540{
Bram Moolenaar33570922005-01-25 22:26:29 +000012541 list_T *l;
12542 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012543 long idx = 0;
12544 int ic = FALSE;
12545
12546 rettv->vval.v_number = -1;
12547 if (argvars[0].v_type != VAR_LIST)
12548 {
12549 EMSG(_(e_listreq));
12550 return;
12551 }
12552 l = argvars[0].vval.v_list;
12553 if (l != NULL)
12554 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012555 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 int error = FALSE;
12559
Bram Moolenaar758711c2005-02-02 23:11:38 +000012560 /* Start at specified item. Use the cached index that list_find()
12561 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012562 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012563 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012564 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012565 ic = get_tv_number_chk(&argvars[3], &error);
12566 if (error)
12567 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012568 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012569
Bram Moolenaar758711c2005-02-02 23:11:38 +000012570 for ( ; item != NULL; item = item->li_next, ++idx)
12571 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012572 {
12573 rettv->vval.v_number = idx;
12574 break;
12575 }
12576 }
12577}
12578
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579static int inputsecret_flag = 0;
12580
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012581static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12582
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012584 * This function is used by f_input() and f_inputdialog() functions. The third
12585 * argument to f_input() specifies the type of completion to use at the
12586 * prompt. The third argument to f_inputdialog() specifies the value to return
12587 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 */
12589 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012590get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012591 typval_T *argvars;
12592 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012593 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012595 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 char_u *p = NULL;
12597 int c;
12598 char_u buf[NUMBUFLEN];
12599 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012600 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012601 int xp_type = EXPAND_NOTHING;
12602 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012605 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606
12607#ifdef NO_CONSOLE_INPUT
12608 /* While starting up, there is no place to enter text. */
12609 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611#endif
12612
12613 cmd_silent = FALSE; /* Want to see the prompt. */
12614 if (prompt != NULL)
12615 {
12616 /* Only the part of the message after the last NL is considered as
12617 * prompt for the command line */
12618 p = vim_strrchr(prompt, '\n');
12619 if (p == NULL)
12620 p = prompt;
12621 else
12622 {
12623 ++p;
12624 c = *p;
12625 *p = NUL;
12626 msg_start();
12627 msg_clr_eos();
12628 msg_puts_attr(prompt, echo_attr);
12629 msg_didout = FALSE;
12630 msg_starthere();
12631 *p = c;
12632 }
12633 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 if (argvars[1].v_type != VAR_UNKNOWN)
12636 {
12637 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12638 if (defstr != NULL)
12639 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012641 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012642 {
12643 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012644 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012645 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012646
Bram Moolenaar4463f292005-09-25 22:20:24 +000012647 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012648
Bram Moolenaar4463f292005-09-25 22:20:24 +000012649 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12650 if (xp_name == NULL)
12651 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012652
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012653 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012654
Bram Moolenaar4463f292005-09-25 22:20:24 +000012655 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12656 &xp_arg) == FAIL)
12657 return;
12658 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012659 }
12660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661 if (defstr != NULL)
12662 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012663 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12664 xp_type, xp_arg);
12665
12666 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668 /* since the user typed this, no need to wait for return */
12669 need_wait_return = FALSE;
12670 msg_didout = FALSE;
12671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 cmd_silent = cmd_silent_save;
12673}
12674
12675/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012676 * "input()" function
12677 * Also handles inputsecret() when inputsecret is set.
12678 */
12679 static void
12680f_input(argvars, rettv)
12681 typval_T *argvars;
12682 typval_T *rettv;
12683{
12684 get_user_input(argvars, rettv, FALSE);
12685}
12686
12687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 * "inputdialog()" function
12689 */
12690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012692 typval_T *argvars;
12693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694{
12695#if defined(FEAT_GUI_TEXTDIALOG)
12696 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12697 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12698 {
12699 char_u *message;
12700 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 message = get_tv_string_chk(&argvars[0]);
12704 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012705 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012706 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 else
12708 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 if (message != NULL && defstr != NULL
12710 && do_dialog(VIM_QUESTION, NULL, message,
12711 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 else
12714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012715 if (message != NULL && defstr != NULL
12716 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012717 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 rettv->vval.v_string = vim_strsave(
12719 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 }
12725 else
12726#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012727 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728}
12729
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012730/*
12731 * "inputlist()" function
12732 */
12733 static void
12734f_inputlist(argvars, rettv)
12735 typval_T *argvars;
12736 typval_T *rettv;
12737{
12738 listitem_T *li;
12739 int selected;
12740 int mouse_used;
12741
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012742#ifdef NO_CONSOLE_INPUT
12743 /* While starting up, there is no place to enter text. */
12744 if (no_console_input())
12745 return;
12746#endif
12747 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12748 {
12749 EMSG2(_(e_listarg), "inputlist()");
12750 return;
12751 }
12752
12753 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012754 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012755 lines_left = Rows; /* avoid more prompt */
12756 msg_scroll = TRUE;
12757 msg_clr_eos();
12758
12759 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12760 {
12761 msg_puts(get_tv_string(&li->li_tv));
12762 msg_putchar('\n');
12763 }
12764
12765 /* Ask for choice. */
12766 selected = prompt_for_number(&mouse_used);
12767 if (mouse_used)
12768 selected -= lines_left;
12769
12770 rettv->vval.v_number = selected;
12771}
12772
12773
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12775
12776/*
12777 * "inputrestore()" function
12778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783{
12784 if (ga_userinput.ga_len > 0)
12785 {
12786 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12788 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012789 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 }
12791 else if (p_verbose > 1)
12792 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012793 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 }
12796}
12797
12798/*
12799 * "inputsave()" function
12800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012806 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 if (ga_grow(&ga_userinput, 1) == OK)
12808 {
12809 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12810 + ga_userinput.ga_len);
12811 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012812 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 }
12814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816}
12817
12818/*
12819 * "inputsecret()" function
12820 */
12821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *argvars;
12824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
12826 ++cmdline_star;
12827 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 --cmdline_star;
12830 --inputsecret_flag;
12831}
12832
12833/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012834 * "insert()" function
12835 */
12836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012840{
12841 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 listitem_T *item;
12843 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012844 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012845
12846 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012848 else if ((l = argvars[0].vval.v_list) != NULL
12849 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012850 {
12851 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012852 before = get_tv_number_chk(&argvars[2], &error);
12853 if (error)
12854 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012855
Bram Moolenaar758711c2005-02-02 23:11:38 +000012856 if (before == l->lv_len)
12857 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012858 else
12859 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012860 item = list_find(l, before);
12861 if (item == NULL)
12862 {
12863 EMSGN(_(e_listidx), before);
12864 l = NULL;
12865 }
12866 }
12867 if (l != NULL)
12868 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012869 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012870 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012871 }
12872 }
12873}
12874
12875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 * "isdirectory()" function
12877 */
12878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884}
12885
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012886/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012887 * "islocked()" function
12888 */
12889 static void
12890f_islocked(argvars, rettv)
12891 typval_T *argvars;
12892 typval_T *rettv;
12893{
12894 lval_T lv;
12895 char_u *end;
12896 dictitem_T *di;
12897
12898 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012899 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12900 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012901 if (end != NULL && lv.ll_name != NULL)
12902 {
12903 if (*end != NUL)
12904 EMSG(_(e_trailing));
12905 else
12906 {
12907 if (lv.ll_tv == NULL)
12908 {
12909 if (check_changedtick(lv.ll_name))
12910 rettv->vval.v_number = 1; /* always locked */
12911 else
12912 {
12913 di = find_var(lv.ll_name, NULL);
12914 if (di != NULL)
12915 {
12916 /* Consider a variable locked when:
12917 * 1. the variable itself is locked
12918 * 2. the value of the variable is locked.
12919 * 3. the List or Dict value is locked.
12920 */
12921 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12922 || tv_islocked(&di->di_tv));
12923 }
12924 }
12925 }
12926 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012927 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012928 else if (lv.ll_newkey != NULL)
12929 EMSG2(_(e_dictkey), lv.ll_newkey);
12930 else if (lv.ll_list != NULL)
12931 /* List item. */
12932 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12933 else
12934 /* Dictionary item. */
12935 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12936 }
12937 }
12938
12939 clear_lval(&lv);
12940}
12941
Bram Moolenaar33570922005-01-25 22:26:29 +000012942static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012943
12944/*
12945 * Turn a dict into a list:
12946 * "what" == 0: list of keys
12947 * "what" == 1: list of values
12948 * "what" == 2: list of items
12949 */
12950 static void
12951dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012952 typval_T *argvars;
12953 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012954 int what;
12955{
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 list_T *l2;
12957 dictitem_T *di;
12958 hashitem_T *hi;
12959 listitem_T *li;
12960 listitem_T *li2;
12961 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012962 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012963
Bram Moolenaar8c711452005-01-14 21:53:12 +000012964 if (argvars[0].v_type != VAR_DICT)
12965 {
12966 EMSG(_(e_dictreq));
12967 return;
12968 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012969 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012970 return;
12971
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012972 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012973 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012974
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012975 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012976 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012977 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012978 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012979 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012980 --todo;
12981 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012982
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012983 li = listitem_alloc();
12984 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012985 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012986 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012987
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012988 if (what == 0)
12989 {
12990 /* keys() */
12991 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012992 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012993 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12994 }
12995 else if (what == 1)
12996 {
12997 /* values() */
12998 copy_tv(&di->di_tv, &li->li_tv);
12999 }
13000 else
13001 {
13002 /* items() */
13003 l2 = list_alloc();
13004 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013005 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013006 li->li_tv.vval.v_list = l2;
13007 if (l2 == NULL)
13008 break;
13009 ++l2->lv_refcount;
13010
13011 li2 = listitem_alloc();
13012 if (li2 == NULL)
13013 break;
13014 list_append(l2, li2);
13015 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013016 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013017 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13018
13019 li2 = listitem_alloc();
13020 if (li2 == NULL)
13021 break;
13022 list_append(l2, li2);
13023 copy_tv(&di->di_tv, &li2->li_tv);
13024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013025 }
13026 }
13027}
13028
13029/*
13030 * "items(dict)" function
13031 */
13032 static void
13033f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *argvars;
13035 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013036{
13037 dict_list(argvars, rettv, 2);
13038}
13039
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013041 * "join()" function
13042 */
13043 static void
13044f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013047{
13048 garray_T ga;
13049 char_u *sep;
13050
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013051 if (argvars[0].v_type != VAR_LIST)
13052 {
13053 EMSG(_(e_listreq));
13054 return;
13055 }
13056 if (argvars[0].vval.v_list == NULL)
13057 return;
13058 if (argvars[1].v_type == VAR_UNKNOWN)
13059 sep = (char_u *)" ";
13060 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013062
13063 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064
13065 if (sep != NULL)
13066 {
13067 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013068 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013069 ga_append(&ga, NUL);
13070 rettv->vval.v_string = (char_u *)ga.ga_data;
13071 }
13072 else
13073 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013074}
13075
13076/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013077 * "keys()" function
13078 */
13079 static void
13080f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013081 typval_T *argvars;
13082 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013083{
13084 dict_list(argvars, rettv, 0);
13085}
13086
13087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 * "last_buffer_nr()" function.
13089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013092 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094{
13095 int n = 0;
13096 buf_T *buf;
13097
13098 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13099 if (n < buf->b_fnum)
13100 n = buf->b_fnum;
13101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013103}
13104
13105/*
13106 * "len()" function
13107 */
13108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013109f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *argvars;
13111 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013112{
13113 switch (argvars[0].v_type)
13114 {
13115 case VAR_STRING:
13116 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117 rettv->vval.v_number = (varnumber_T)STRLEN(
13118 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013119 break;
13120 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013122 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013123 case VAR_DICT:
13124 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13125 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013127 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013128 break;
13129 }
13130}
13131
Bram Moolenaar33570922005-01-25 22:26:29 +000013132static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013133
13134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013136 typval_T *argvars;
13137 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013138 int type;
13139{
13140#ifdef FEAT_LIBCALL
13141 char_u *string_in;
13142 char_u **string_result;
13143 int nr_result;
13144#endif
13145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013147 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013149
13150 if (check_restricted() || check_secure())
13151 return;
13152
13153#ifdef FEAT_LIBCALL
13154 /* The first two args must be strings, otherwise its meaningless */
13155 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13156 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013157 string_in = NULL;
13158 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013159 string_in = argvars[2].vval.v_string;
13160 if (type == VAR_NUMBER)
13161 string_result = NULL;
13162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164 if (mch_libcall(argvars[0].vval.v_string,
13165 argvars[1].vval.v_string,
13166 string_in,
13167 argvars[2].vval.v_number,
13168 string_result,
13169 &nr_result) == OK
13170 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013172 }
13173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174}
13175
13176/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013177 * "libcall()" function
13178 */
13179 static void
13180f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013181 typval_T *argvars;
13182 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013183{
13184 libcall_common(argvars, rettv, VAR_STRING);
13185}
13186
13187/*
13188 * "libcallnr()" function
13189 */
13190 static void
13191f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013192 typval_T *argvars;
13193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013194{
13195 libcall_common(argvars, rettv, VAR_NUMBER);
13196}
13197
13198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 * "line(string)" function
13200 */
13201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206 linenr_T lnum = 0;
13207 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013208 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013210 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 if (fp != NULL)
13212 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214}
13215
13216/*
13217 * "line2byte(lnum)" function
13218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223{
13224#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226#else
13227 linenr_T lnum;
13228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13234 if (rettv->vval.v_number >= 0)
13235 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236#endif
13237}
13238
13239/*
13240 * "lispindent(lnum)" function
13241 */
13242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013244 typval_T *argvars;
13245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246{
13247#ifdef FEAT_LISP
13248 pos_T pos;
13249 linenr_T lnum;
13250
13251 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13254 {
13255 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257 curwin->w_cursor = pos;
13258 }
13259 else
13260#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262}
13263
13264/*
13265 * "localtime()" function
13266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273}
13274
Bram Moolenaar33570922005-01-25 22:26:29 +000013275static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276
13277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013279 typval_T *argvars;
13280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 int exact;
13282{
13283 char_u *keys;
13284 char_u *which;
13285 char_u buf[NUMBUFLEN];
13286 char_u *keys_buf = NULL;
13287 char_u *rhs;
13288 int mode;
13289 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013290 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291
13292 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 rettv->v_type = VAR_STRING;
13294 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 if (*keys == NUL)
13298 return;
13299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013300 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013303 if (argvars[2].v_type != VAR_UNKNOWN)
13304 abbr = get_tv_number(&argvars[2]);
13305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 else
13307 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013308 if (which == NULL)
13309 return;
13310
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 mode = get_map_mode(&which, 0);
13312
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013313 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013314 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 vim_free(keys_buf);
13316 if (rhs != NULL)
13317 {
13318 ga_init(&ga);
13319 ga.ga_itemsize = 1;
13320 ga.ga_growsize = 40;
13321
13322 while (*rhs != NUL)
13323 ga_concat(&ga, str2special(&rhs, FALSE));
13324
13325 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 }
13328}
13329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013330#ifdef FEAT_FLOAT
13331/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013332 * "log()" function
13333 */
13334 static void
13335f_log(argvars, rettv)
13336 typval_T *argvars;
13337 typval_T *rettv;
13338{
13339 float_T f;
13340
13341 rettv->v_type = VAR_FLOAT;
13342 if (get_float_arg(argvars, &f) == OK)
13343 rettv->vval.v_float = log(f);
13344 else
13345 rettv->vval.v_float = 0.0;
13346}
13347
13348/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013349 * "log10()" function
13350 */
13351 static void
13352f_log10(argvars, rettv)
13353 typval_T *argvars;
13354 typval_T *rettv;
13355{
13356 float_T f;
13357
13358 rettv->v_type = VAR_FLOAT;
13359 if (get_float_arg(argvars, &f) == OK)
13360 rettv->vval.v_float = log10(f);
13361 else
13362 rettv->vval.v_float = 0.0;
13363}
13364#endif
13365
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013367 * "map()" function
13368 */
13369 static void
13370f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013371 typval_T *argvars;
13372 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013373{
13374 filter_map(argvars, rettv, TRUE);
13375}
13376
13377/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 */
13380 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013381f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
13383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386}
13387
13388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013389 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 */
13391 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 typval_T *argvars;
13394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013396 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397}
13398
Bram Moolenaar33570922005-01-25 22:26:29 +000013399static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400
13401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013402find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 typval_T *argvars;
13404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 int type;
13406{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013407 char_u *str = NULL;
13408 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 char_u *pat;
13410 regmatch_T regmatch;
13411 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013412 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 char_u *save_cpo;
13414 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013415 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013416 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013417 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013418 list_T *l = NULL;
13419 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013420 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013421 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422
13423 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13424 save_cpo = p_cpo;
13425 p_cpo = (char_u *)"";
13426
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013427 rettv->vval.v_number = -1;
13428 if (type == 3)
13429 {
13430 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013431 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013432 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013433 }
13434 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436 rettv->v_type = VAR_STRING;
13437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013440 if (argvars[0].v_type == VAR_LIST)
13441 {
13442 if ((l = argvars[0].vval.v_list) == NULL)
13443 goto theend;
13444 li = l->lv_first;
13445 }
13446 else
13447 expr = str = get_tv_string(&argvars[0]);
13448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13450 if (pat == NULL)
13451 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 int error = FALSE;
13456
13457 start = get_tv_number_chk(&argvars[2], &error);
13458 if (error)
13459 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013460 if (l != NULL)
13461 {
13462 li = list_find(l, start);
13463 if (li == NULL)
13464 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013465 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013466 }
13467 else
13468 {
13469 if (start < 0)
13470 start = 0;
13471 if (start > (long)STRLEN(str))
13472 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013473 /* When "count" argument is there ignore matches before "start",
13474 * otherwise skip part of the string. Differs when pattern is "^"
13475 * or "\<". */
13476 if (argvars[3].v_type != VAR_UNKNOWN)
13477 startcol = start;
13478 else
13479 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013480 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013481
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 nth = get_tv_number_chk(&argvars[3], &error);
13484 if (error)
13485 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 }
13487
13488 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13489 if (regmatch.regprog != NULL)
13490 {
13491 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013492
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013493 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013494 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013495 if (l != NULL)
13496 {
13497 if (li == NULL)
13498 {
13499 match = FALSE;
13500 break;
13501 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013502 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013503 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013504 if (str == NULL)
13505 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013506 }
13507
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013508 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013509
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013510 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013511 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 if (l == NULL && !match)
13513 break;
13514
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013515 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013516 if (l != NULL)
13517 {
13518 li = li->li_next;
13519 ++idx;
13520 }
13521 else
13522 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013523#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013524 startcol = (colnr_T)(regmatch.startp[0]
13525 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013526#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013527 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013528#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013529 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013530 }
13531
13532 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013534 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013535 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013536 int i;
13537
13538 /* return list with matched string and submatches */
13539 for (i = 0; i < NSUBEXP; ++i)
13540 {
13541 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013542 {
13543 if (list_append_string(rettv->vval.v_list,
13544 (char_u *)"", 0) == FAIL)
13545 break;
13546 }
13547 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013548 regmatch.startp[i],
13549 (int)(regmatch.endp[i] - regmatch.startp[i]))
13550 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013551 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013552 }
13553 }
13554 else if (type == 2)
13555 {
13556 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013557 if (l != NULL)
13558 copy_tv(&li->li_tv, rettv);
13559 else
13560 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013562 }
13563 else if (l != NULL)
13564 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 else
13566 {
13567 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 (varnumber_T)(regmatch.startp[0] - str);
13570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013573 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 }
13575 }
13576 vim_free(regmatch.regprog);
13577 }
13578
13579theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013580 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 p_cpo = save_cpo;
13582}
13583
13584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013585 * "match()" function
13586 */
13587 static void
13588f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013589 typval_T *argvars;
13590 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013591{
13592 find_some_match(argvars, rettv, 1);
13593}
13594
13595/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013596 * "matchadd()" function
13597 */
13598 static void
13599f_matchadd(argvars, rettv)
13600 typval_T *argvars;
13601 typval_T *rettv;
13602{
13603#ifdef FEAT_SEARCH_EXTRA
13604 char_u buf[NUMBUFLEN];
13605 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13606 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13607 int prio = 10; /* default priority */
13608 int id = -1;
13609 int error = FALSE;
13610
13611 rettv->vval.v_number = -1;
13612
13613 if (grp == NULL || pat == NULL)
13614 return;
13615 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013616 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013617 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013618 if (argvars[3].v_type != VAR_UNKNOWN)
13619 id = get_tv_number_chk(&argvars[3], &error);
13620 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013621 if (error == TRUE)
13622 return;
13623 if (id >= 1 && id <= 3)
13624 {
13625 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13626 return;
13627 }
13628
13629 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13630#endif
13631}
13632
13633/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013634 * "matcharg()" function
13635 */
13636 static void
13637f_matcharg(argvars, rettv)
13638 typval_T *argvars;
13639 typval_T *rettv;
13640{
13641 if (rettv_list_alloc(rettv) == OK)
13642 {
13643#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013644 int id = get_tv_number(&argvars[0]);
13645 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013646
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013647 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013648 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013649 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13650 {
13651 list_append_string(rettv->vval.v_list,
13652 syn_id2name(m->hlg_id), -1);
13653 list_append_string(rettv->vval.v_list, m->pattern, -1);
13654 }
13655 else
13656 {
13657 list_append_string(rettv->vval.v_list, NUL, -1);
13658 list_append_string(rettv->vval.v_list, NUL, -1);
13659 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013660 }
13661#endif
13662 }
13663}
13664
13665/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013666 * "matchdelete()" function
13667 */
13668 static void
13669f_matchdelete(argvars, rettv)
13670 typval_T *argvars;
13671 typval_T *rettv;
13672{
13673#ifdef FEAT_SEARCH_EXTRA
13674 rettv->vval.v_number = match_delete(curwin,
13675 (int)get_tv_number(&argvars[0]), TRUE);
13676#endif
13677}
13678
13679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013680 * "matchend()" function
13681 */
13682 static void
13683f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 typval_T *argvars;
13685 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686{
13687 find_some_match(argvars, rettv, 0);
13688}
13689
13690/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013691 * "matchlist()" function
13692 */
13693 static void
13694f_matchlist(argvars, rettv)
13695 typval_T *argvars;
13696 typval_T *rettv;
13697{
13698 find_some_match(argvars, rettv, 3);
13699}
13700
13701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 * "matchstr()" function
13703 */
13704 static void
13705f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 typval_T *argvars;
13707 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708{
13709 find_some_match(argvars, rettv, 2);
13710}
13711
Bram Moolenaar33570922005-01-25 22:26:29 +000013712static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013713
13714 static void
13715max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013716 typval_T *argvars;
13717 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013718 int domax;
13719{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013720 long n = 0;
13721 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013722 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013723
13724 if (argvars[0].v_type == VAR_LIST)
13725 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013726 list_T *l;
13727 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013728
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013729 l = argvars[0].vval.v_list;
13730 if (l != NULL)
13731 {
13732 li = l->lv_first;
13733 if (li != NULL)
13734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013735 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013736 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013737 {
13738 li = li->li_next;
13739 if (li == NULL)
13740 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013741 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013742 if (domax ? i > n : i < n)
13743 n = i;
13744 }
13745 }
13746 }
13747 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013748 else if (argvars[0].v_type == VAR_DICT)
13749 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013751 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013753 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013754
13755 d = argvars[0].vval.v_dict;
13756 if (d != NULL)
13757 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013758 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013761 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013762 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013763 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013765 if (first)
13766 {
13767 n = i;
13768 first = FALSE;
13769 }
13770 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013771 n = i;
13772 }
13773 }
13774 }
13775 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013776 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013777 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013779}
13780
13781/*
13782 * "max()" function
13783 */
13784 static void
13785f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013786 typval_T *argvars;
13787 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013788{
13789 max_min(argvars, rettv, TRUE);
13790}
13791
13792/*
13793 * "min()" function
13794 */
13795 static void
13796f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013799{
13800 max_min(argvars, rettv, FALSE);
13801}
13802
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013803static int mkdir_recurse __ARGS((char_u *dir, int prot));
13804
13805/*
13806 * Create the directory in which "dir" is located, and higher levels when
13807 * needed.
13808 */
13809 static int
13810mkdir_recurse(dir, prot)
13811 char_u *dir;
13812 int prot;
13813{
13814 char_u *p;
13815 char_u *updir;
13816 int r = FAIL;
13817
13818 /* Get end of directory name in "dir".
13819 * We're done when it's "/" or "c:/". */
13820 p = gettail_sep(dir);
13821 if (p <= get_past_head(dir))
13822 return OK;
13823
13824 /* If the directory exists we're done. Otherwise: create it.*/
13825 updir = vim_strnsave(dir, (int)(p - dir));
13826 if (updir == NULL)
13827 return FAIL;
13828 if (mch_isdir(updir))
13829 r = OK;
13830 else if (mkdir_recurse(updir, prot) == OK)
13831 r = vim_mkdir_emsg(updir, prot);
13832 vim_free(updir);
13833 return r;
13834}
13835
13836#ifdef vim_mkdir
13837/*
13838 * "mkdir()" function
13839 */
13840 static void
13841f_mkdir(argvars, rettv)
13842 typval_T *argvars;
13843 typval_T *rettv;
13844{
13845 char_u *dir;
13846 char_u buf[NUMBUFLEN];
13847 int prot = 0755;
13848
13849 rettv->vval.v_number = FAIL;
13850 if (check_restricted() || check_secure())
13851 return;
13852
13853 dir = get_tv_string_buf(&argvars[0], buf);
13854 if (argvars[1].v_type != VAR_UNKNOWN)
13855 {
13856 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 prot = get_tv_number_chk(&argvars[2], NULL);
13858 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013859 mkdir_recurse(dir, prot);
13860 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013862}
13863#endif
13864
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 * "mode()" function
13867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013869f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013870 typval_T *argvars;
13871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013873 char_u buf[3];
13874
13875 buf[1] = NUL;
13876 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877
13878#ifdef FEAT_VISUAL
13879 if (VIsual_active)
13880 {
13881 if (VIsual_select)
13882 buf[0] = VIsual_mode + 's' - 'v';
13883 else
13884 buf[0] = VIsual_mode;
13885 }
13886 else
13887#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013888 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13889 || State == CONFIRM)
13890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013892 if (State == ASKMORE)
13893 buf[1] = 'm';
13894 else if (State == CONFIRM)
13895 buf[1] = '?';
13896 }
13897 else if (State == EXTERNCMD)
13898 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 else if (State & INSERT)
13900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013901#ifdef FEAT_VREPLACE
13902 if (State & VREPLACE_FLAG)
13903 {
13904 buf[0] = 'R';
13905 buf[1] = 'v';
13906 }
13907 else
13908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 if (State & REPLACE_FLAG)
13910 buf[0] = 'R';
13911 else
13912 buf[0] = 'i';
13913 }
13914 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013917 if (exmode_active)
13918 buf[1] = 'v';
13919 }
13920 else if (exmode_active)
13921 {
13922 buf[0] = 'c';
13923 buf[1] = 'e';
13924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013928 if (finish_op)
13929 buf[1] = 'o';
13930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013932 /* Clear out the minor mode when the argument is not a non-zero number or
13933 * non-empty string. */
13934 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013935 buf[1] = NUL;
13936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 rettv->vval.v_string = vim_strsave(buf);
13938 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939}
13940
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013941#ifdef FEAT_MZSCHEME
13942/*
13943 * "mzeval()" function
13944 */
13945 static void
13946f_mzeval(argvars, rettv)
13947 typval_T *argvars;
13948 typval_T *rettv;
13949{
13950 char_u *str;
13951 char_u buf[NUMBUFLEN];
13952
13953 str = get_tv_string_buf(&argvars[0], buf);
13954 do_mzeval(str, rettv);
13955}
13956#endif
13957
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 * "nextnonblank()" function
13960 */
13961 static void
13962f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013963 typval_T *argvars;
13964 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965{
13966 linenr_T lnum;
13967
13968 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971 {
13972 lnum = 0;
13973 break;
13974 }
13975 if (*skipwhite(ml_get(lnum)) != NUL)
13976 break;
13977 }
13978 rettv->vval.v_number = lnum;
13979}
13980
13981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 * "nr2char()" function
13983 */
13984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013986 typval_T *argvars;
13987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988{
13989 char_u buf[NUMBUFLEN];
13990
13991#ifdef FEAT_MBYTE
13992 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 else
13995#endif
13996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 buf[1] = NUL;
13999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014000 rettv->v_type = VAR_STRING;
14001 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014002}
14003
14004/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014005 * "pathshorten()" function
14006 */
14007 static void
14008f_pathshorten(argvars, rettv)
14009 typval_T *argvars;
14010 typval_T *rettv;
14011{
14012 char_u *p;
14013
14014 rettv->v_type = VAR_STRING;
14015 p = get_tv_string_chk(&argvars[0]);
14016 if (p == NULL)
14017 rettv->vval.v_string = NULL;
14018 else
14019 {
14020 p = vim_strsave(p);
14021 rettv->vval.v_string = p;
14022 if (p != NULL)
14023 shorten_dir(p);
14024 }
14025}
14026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014027#ifdef FEAT_FLOAT
14028/*
14029 * "pow()" function
14030 */
14031 static void
14032f_pow(argvars, rettv)
14033 typval_T *argvars;
14034 typval_T *rettv;
14035{
14036 float_T fx, fy;
14037
14038 rettv->v_type = VAR_FLOAT;
14039 if (get_float_arg(argvars, &fx) == OK
14040 && get_float_arg(&argvars[1], &fy) == OK)
14041 rettv->vval.v_float = pow(fx, fy);
14042 else
14043 rettv->vval.v_float = 0.0;
14044}
14045#endif
14046
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014047/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014048 * "prevnonblank()" function
14049 */
14050 static void
14051f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014052 typval_T *argvars;
14053 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054{
14055 linenr_T lnum;
14056
14057 lnum = get_tv_lnum(argvars);
14058 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14059 lnum = 0;
14060 else
14061 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14062 --lnum;
14063 rettv->vval.v_number = lnum;
14064}
14065
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014066#ifdef HAVE_STDARG_H
14067/* This dummy va_list is here because:
14068 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14069 * - locally in the function results in a "used before set" warning
14070 * - using va_start() to initialize it gives "function with fixed args" error */
14071static va_list ap;
14072#endif
14073
Bram Moolenaar8c711452005-01-14 21:53:12 +000014074/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014075 * "printf()" function
14076 */
14077 static void
14078f_printf(argvars, rettv)
14079 typval_T *argvars;
14080 typval_T *rettv;
14081{
14082 rettv->v_type = VAR_STRING;
14083 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014084#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014085 {
14086 char_u buf[NUMBUFLEN];
14087 int len;
14088 char_u *s;
14089 int saved_did_emsg = did_emsg;
14090 char *fmt;
14091
14092 /* Get the required length, allocate the buffer and do it for real. */
14093 did_emsg = FALSE;
14094 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014095 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014096 if (!did_emsg)
14097 {
14098 s = alloc(len + 1);
14099 if (s != NULL)
14100 {
14101 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014102 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014103 }
14104 }
14105 did_emsg |= saved_did_emsg;
14106 }
14107#endif
14108}
14109
14110/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014111 * "pumvisible()" function
14112 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014113 static void
14114f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014115 typval_T *argvars UNUSED;
14116 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014117{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014118#ifdef FEAT_INS_EXPAND
14119 if (pum_visible())
14120 rettv->vval.v_number = 1;
14121#endif
14122}
14123
14124/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014125 * "range()" function
14126 */
14127 static void
14128f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014129 typval_T *argvars;
14130 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014131{
14132 long start;
14133 long end;
14134 long stride = 1;
14135 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014138 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014139 if (argvars[1].v_type == VAR_UNKNOWN)
14140 {
14141 end = start - 1;
14142 start = 0;
14143 }
14144 else
14145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014149 }
14150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 if (error)
14152 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014153 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014154 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014155 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014156 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014157 else
14158 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014159 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014160 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014161 if (list_append_number(rettv->vval.v_list,
14162 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014163 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014164 }
14165}
14166
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014167/*
14168 * "readfile()" function
14169 */
14170 static void
14171f_readfile(argvars, rettv)
14172 typval_T *argvars;
14173 typval_T *rettv;
14174{
14175 int binary = FALSE;
14176 char_u *fname;
14177 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014178 listitem_T *li;
14179#define FREAD_SIZE 200 /* optimized for text lines */
14180 char_u buf[FREAD_SIZE];
14181 int readlen; /* size of last fread() */
14182 int buflen; /* nr of valid chars in buf[] */
14183 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14184 int tolist; /* first byte in buf[] still to be put in list */
14185 int chop; /* how many CR to chop off */
14186 char_u *prev = NULL; /* previously read bytes, if any */
14187 int prevlen = 0; /* length of "prev" if not NULL */
14188 char_u *s;
14189 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014190 long maxline = MAXLNUM;
14191 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014193 if (argvars[1].v_type != VAR_UNKNOWN)
14194 {
14195 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14196 binary = TRUE;
14197 if (argvars[2].v_type != VAR_UNKNOWN)
14198 maxline = get_tv_number(&argvars[2]);
14199 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014200
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014201 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014202 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014203
14204 /* Always open the file in binary mode, library functions have a mind of
14205 * their own about CR-LF conversion. */
14206 fname = get_tv_string(&argvars[0]);
14207 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14208 {
14209 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14210 return;
14211 }
14212
14213 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014214 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014215 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014216 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014217 buflen = filtd + readlen;
14218 tolist = 0;
14219 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14220 {
14221 if (buf[filtd] == '\n' || readlen <= 0)
14222 {
14223 /* Only when in binary mode add an empty list item when the
14224 * last line ends in a '\n'. */
14225 if (!binary && readlen == 0 && filtd == 0)
14226 break;
14227
14228 /* Found end-of-line or end-of-file: add a text line to the
14229 * list. */
14230 chop = 0;
14231 if (!binary)
14232 while (filtd - chop - 1 >= tolist
14233 && buf[filtd - chop - 1] == '\r')
14234 ++chop;
14235 len = filtd - tolist - chop;
14236 if (prev == NULL)
14237 s = vim_strnsave(buf + tolist, len);
14238 else
14239 {
14240 s = alloc((unsigned)(prevlen + len + 1));
14241 if (s != NULL)
14242 {
14243 mch_memmove(s, prev, prevlen);
14244 vim_free(prev);
14245 prev = NULL;
14246 mch_memmove(s + prevlen, buf + tolist, len);
14247 s[prevlen + len] = NUL;
14248 }
14249 }
14250 tolist = filtd + 1;
14251
14252 li = listitem_alloc();
14253 if (li == NULL)
14254 {
14255 vim_free(s);
14256 break;
14257 }
14258 li->li_tv.v_type = VAR_STRING;
14259 li->li_tv.v_lock = 0;
14260 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014261 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014262
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014263 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014264 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014265 if (readlen <= 0)
14266 break;
14267 }
14268 else if (buf[filtd] == NUL)
14269 buf[filtd] = '\n';
14270 }
14271 if (readlen <= 0)
14272 break;
14273
14274 if (tolist == 0)
14275 {
14276 /* "buf" is full, need to move text to an allocated buffer */
14277 if (prev == NULL)
14278 {
14279 prev = vim_strnsave(buf, buflen);
14280 prevlen = buflen;
14281 }
14282 else
14283 {
14284 s = alloc((unsigned)(prevlen + buflen));
14285 if (s != NULL)
14286 {
14287 mch_memmove(s, prev, prevlen);
14288 mch_memmove(s + prevlen, buf, buflen);
14289 vim_free(prev);
14290 prev = s;
14291 prevlen += buflen;
14292 }
14293 }
14294 filtd = 0;
14295 }
14296 else
14297 {
14298 mch_memmove(buf, buf + tolist, buflen - tolist);
14299 filtd -= tolist;
14300 }
14301 }
14302
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014303 /*
14304 * For a negative line count use only the lines at the end of the file,
14305 * free the rest.
14306 */
14307 if (maxline < 0)
14308 while (cnt > -maxline)
14309 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014310 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014311 --cnt;
14312 }
14313
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014314 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014315 fclose(fd);
14316}
14317
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014318#if defined(FEAT_RELTIME)
14319static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14320
14321/*
14322 * Convert a List to proftime_T.
14323 * Return FAIL when there is something wrong.
14324 */
14325 static int
14326list2proftime(arg, tm)
14327 typval_T *arg;
14328 proftime_T *tm;
14329{
14330 long n1, n2;
14331 int error = FALSE;
14332
14333 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14334 || arg->vval.v_list->lv_len != 2)
14335 return FAIL;
14336 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14337 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14338# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014339 tm->HighPart = n1;
14340 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014341# else
14342 tm->tv_sec = n1;
14343 tm->tv_usec = n2;
14344# endif
14345 return error ? FAIL : OK;
14346}
14347#endif /* FEAT_RELTIME */
14348
14349/*
14350 * "reltime()" function
14351 */
14352 static void
14353f_reltime(argvars, rettv)
14354 typval_T *argvars;
14355 typval_T *rettv;
14356{
14357#ifdef FEAT_RELTIME
14358 proftime_T res;
14359 proftime_T start;
14360
14361 if (argvars[0].v_type == VAR_UNKNOWN)
14362 {
14363 /* No arguments: get current time. */
14364 profile_start(&res);
14365 }
14366 else if (argvars[1].v_type == VAR_UNKNOWN)
14367 {
14368 if (list2proftime(&argvars[0], &res) == FAIL)
14369 return;
14370 profile_end(&res);
14371 }
14372 else
14373 {
14374 /* Two arguments: compute the difference. */
14375 if (list2proftime(&argvars[0], &start) == FAIL
14376 || list2proftime(&argvars[1], &res) == FAIL)
14377 return;
14378 profile_sub(&res, &start);
14379 }
14380
14381 if (rettv_list_alloc(rettv) == OK)
14382 {
14383 long n1, n2;
14384
14385# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014386 n1 = res.HighPart;
14387 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014388# else
14389 n1 = res.tv_sec;
14390 n2 = res.tv_usec;
14391# endif
14392 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14393 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14394 }
14395#endif
14396}
14397
14398/*
14399 * "reltimestr()" function
14400 */
14401 static void
14402f_reltimestr(argvars, rettv)
14403 typval_T *argvars;
14404 typval_T *rettv;
14405{
14406#ifdef FEAT_RELTIME
14407 proftime_T tm;
14408#endif
14409
14410 rettv->v_type = VAR_STRING;
14411 rettv->vval.v_string = NULL;
14412#ifdef FEAT_RELTIME
14413 if (list2proftime(&argvars[0], &tm) == OK)
14414 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14415#endif
14416}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014417
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14419static void make_connection __ARGS((void));
14420static int check_connection __ARGS((void));
14421
14422 static void
14423make_connection()
14424{
14425 if (X_DISPLAY == NULL
14426# ifdef FEAT_GUI
14427 && !gui.in_use
14428# endif
14429 )
14430 {
14431 x_force_connect = TRUE;
14432 setup_term_clip();
14433 x_force_connect = FALSE;
14434 }
14435}
14436
14437 static int
14438check_connection()
14439{
14440 make_connection();
14441 if (X_DISPLAY == NULL)
14442 {
14443 EMSG(_("E240: No connection to Vim server"));
14444 return FAIL;
14445 }
14446 return OK;
14447}
14448#endif
14449
14450#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014451static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014452
14453 static void
14454remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014455 typval_T *argvars;
14456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457 int expr;
14458{
14459 char_u *server_name;
14460 char_u *keys;
14461 char_u *r = NULL;
14462 char_u buf[NUMBUFLEN];
14463# ifdef WIN32
14464 HWND w;
14465# else
14466 Window w;
14467# endif
14468
14469 if (check_restricted() || check_secure())
14470 return;
14471
14472# ifdef FEAT_X11
14473 if (check_connection() == FAIL)
14474 return;
14475# endif
14476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014477 server_name = get_tv_string_chk(&argvars[0]);
14478 if (server_name == NULL)
14479 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480 keys = get_tv_string_buf(&argvars[1], buf);
14481# ifdef WIN32
14482 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14483# else
14484 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14485 < 0)
14486# endif
14487 {
14488 if (r != NULL)
14489 EMSG(r); /* sending worked but evaluation failed */
14490 else
14491 EMSG2(_("E241: Unable to send to %s"), server_name);
14492 return;
14493 }
14494
14495 rettv->vval.v_string = r;
14496
14497 if (argvars[2].v_type != VAR_UNKNOWN)
14498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014499 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014500 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014503 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014504 v.di_tv.v_type = VAR_STRING;
14505 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 idvar = get_tv_string_chk(&argvars[2]);
14507 if (idvar != NULL)
14508 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014510 }
14511}
14512#endif
14513
14514/*
14515 * "remote_expr()" function
14516 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014517 static void
14518f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014519 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014520 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521{
14522 rettv->v_type = VAR_STRING;
14523 rettv->vval.v_string = NULL;
14524#ifdef FEAT_CLIENTSERVER
14525 remote_common(argvars, rettv, TRUE);
14526#endif
14527}
14528
14529/*
14530 * "remote_foreground()" function
14531 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014532 static void
14533f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014534 typval_T *argvars UNUSED;
14535 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014537#ifdef FEAT_CLIENTSERVER
14538# ifdef WIN32
14539 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 {
14541 char_u *server_name = get_tv_string_chk(&argvars[0]);
14542
14543 if (server_name != NULL)
14544 serverForeground(server_name);
14545 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546# else
14547 /* Send a foreground() expression to the server. */
14548 argvars[1].v_type = VAR_STRING;
14549 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14550 argvars[2].v_type = VAR_UNKNOWN;
14551 remote_common(argvars, rettv, TRUE);
14552 vim_free(argvars[1].vval.v_string);
14553# endif
14554#endif
14555}
14556
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557 static void
14558f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561{
14562#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014563 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564 char_u *s = NULL;
14565# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014566 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014569
14570 if (check_restricted() || check_secure())
14571 {
14572 rettv->vval.v_number = -1;
14573 return;
14574 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 serverid = get_tv_string_chk(&argvars[0]);
14576 if (serverid == NULL)
14577 {
14578 rettv->vval.v_number = -1;
14579 return; /* type error; errmsg already given */
14580 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014582 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 if (n == 0)
14584 rettv->vval.v_number = -1;
14585 else
14586 {
14587 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14588 rettv->vval.v_number = (s != NULL);
14589 }
14590# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591 if (check_connection() == FAIL)
14592 return;
14593
14594 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014596# endif
14597
14598 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600 char_u *retvar;
14601
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 v.di_tv.v_type = VAR_STRING;
14603 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014604 retvar = get_tv_string_chk(&argvars[1]);
14605 if (retvar != NULL)
14606 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014607 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608 }
14609#else
14610 rettv->vval.v_number = -1;
14611#endif
14612}
14613
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614 static void
14615f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014616 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014618{
14619 char_u *r = NULL;
14620
14621#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 char_u *serverid = get_tv_string_chk(&argvars[0]);
14623
14624 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625 {
14626# ifdef WIN32
14627 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014628 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014630 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631 if (n != 0)
14632 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14633 if (r == NULL)
14634# else
14635 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637# endif
14638 EMSG(_("E277: Unable to read a server reply"));
14639 }
14640#endif
14641 rettv->v_type = VAR_STRING;
14642 rettv->vval.v_string = r;
14643}
14644
14645/*
14646 * "remote_send()" function
14647 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 static void
14649f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652{
14653 rettv->v_type = VAR_STRING;
14654 rettv->vval.v_string = NULL;
14655#ifdef FEAT_CLIENTSERVER
14656 remote_common(argvars, rettv, FALSE);
14657#endif
14658}
14659
14660/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014661 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014662 */
14663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 typval_T *argvars;
14666 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014667{
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 list_T *l;
14669 listitem_T *item, *item2;
14670 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014671 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014672 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014673 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014674 dict_T *d;
14675 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014676
Bram Moolenaar8c711452005-01-14 21:53:12 +000014677 if (argvars[0].v_type == VAR_DICT)
14678 {
14679 if (argvars[2].v_type != VAR_UNKNOWN)
14680 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014681 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014682 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 key = get_tv_string_chk(&argvars[1]);
14685 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014687 di = dict_find(d, key, -1);
14688 if (di == NULL)
14689 EMSG2(_(e_dictkey), key);
14690 else
14691 {
14692 *rettv = di->di_tv;
14693 init_tv(&di->di_tv);
14694 dictitem_remove(d, di);
14695 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014697 }
14698 }
14699 else if (argvars[0].v_type != VAR_LIST)
14700 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014701 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014702 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014704 int error = FALSE;
14705
14706 idx = get_tv_number_chk(&argvars[1], &error);
14707 if (error)
14708 ; /* type error: do nothing, errmsg already given */
14709 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014710 EMSGN(_(e_listidx), idx);
14711 else
14712 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014713 if (argvars[2].v_type == VAR_UNKNOWN)
14714 {
14715 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014716 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014717 *rettv = item->li_tv;
14718 vim_free(item);
14719 }
14720 else
14721 {
14722 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014723 end = get_tv_number_chk(&argvars[2], &error);
14724 if (error)
14725 ; /* type error: do nothing */
14726 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014727 EMSGN(_(e_listidx), end);
14728 else
14729 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014730 int cnt = 0;
14731
14732 for (li = item; li != NULL; li = li->li_next)
14733 {
14734 ++cnt;
14735 if (li == item2)
14736 break;
14737 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014738 if (li == NULL) /* didn't find "item2" after "item" */
14739 EMSG(_(e_invrange));
14740 else
14741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014742 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014743 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014744 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014745 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014746 l->lv_first = item;
14747 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014748 item->li_prev = NULL;
14749 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014750 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014751 }
14752 }
14753 }
14754 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014755 }
14756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757}
14758
14759/*
14760 * "rename({from}, {to})" function
14761 */
14762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014763f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 typval_T *argvars;
14765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766{
14767 char_u buf[NUMBUFLEN];
14768
14769 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14773 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774}
14775
14776/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014777 * "repeat()" function
14778 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014779 static void
14780f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014781 typval_T *argvars;
14782 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014783{
14784 char_u *p;
14785 int n;
14786 int slen;
14787 int len;
14788 char_u *r;
14789 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014790
14791 n = get_tv_number(&argvars[1]);
14792 if (argvars[0].v_type == VAR_LIST)
14793 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014794 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014795 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014796 if (list_extend(rettv->vval.v_list,
14797 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014798 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014799 }
14800 else
14801 {
14802 p = get_tv_string(&argvars[0]);
14803 rettv->v_type = VAR_STRING;
14804 rettv->vval.v_string = NULL;
14805
14806 slen = (int)STRLEN(p);
14807 len = slen * n;
14808 if (len <= 0)
14809 return;
14810
14811 r = alloc(len + 1);
14812 if (r != NULL)
14813 {
14814 for (i = 0; i < n; i++)
14815 mch_memmove(r + i * slen, p, (size_t)slen);
14816 r[len] = NUL;
14817 }
14818
14819 rettv->vval.v_string = r;
14820 }
14821}
14822
14823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 * "resolve()" function
14825 */
14826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014828 typval_T *argvars;
14829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830{
14831 char_u *p;
14832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834#ifdef FEAT_SHORTCUT
14835 {
14836 char_u *v = NULL;
14837
14838 v = mch_resolve_shortcut(p);
14839 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014840 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014842 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 }
14844#else
14845# ifdef HAVE_READLINK
14846 {
14847 char_u buf[MAXPATHL + 1];
14848 char_u *cpy;
14849 int len;
14850 char_u *remain = NULL;
14851 char_u *q;
14852 int is_relative_to_current = FALSE;
14853 int has_trailing_pathsep = FALSE;
14854 int limit = 100;
14855
14856 p = vim_strsave(p);
14857
14858 if (p[0] == '.' && (vim_ispathsep(p[1])
14859 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14860 is_relative_to_current = TRUE;
14861
14862 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014863 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 has_trailing_pathsep = TRUE;
14865
14866 q = getnextcomp(p);
14867 if (*q != NUL)
14868 {
14869 /* Separate the first path component in "p", and keep the
14870 * remainder (beginning with the path separator). */
14871 remain = vim_strsave(q - 1);
14872 q[-1] = NUL;
14873 }
14874
14875 for (;;)
14876 {
14877 for (;;)
14878 {
14879 len = readlink((char *)p, (char *)buf, MAXPATHL);
14880 if (len <= 0)
14881 break;
14882 buf[len] = NUL;
14883
14884 if (limit-- == 0)
14885 {
14886 vim_free(p);
14887 vim_free(remain);
14888 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 goto fail;
14891 }
14892
14893 /* Ensure that the result will have a trailing path separator
14894 * if the argument has one. */
14895 if (remain == NULL && has_trailing_pathsep)
14896 add_pathsep(buf);
14897
14898 /* Separate the first path component in the link value and
14899 * concatenate the remainders. */
14900 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14901 if (*q != NUL)
14902 {
14903 if (remain == NULL)
14904 remain = vim_strsave(q - 1);
14905 else
14906 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014907 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 if (cpy != NULL)
14909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 vim_free(remain);
14911 remain = cpy;
14912 }
14913 }
14914 q[-1] = NUL;
14915 }
14916
14917 q = gettail(p);
14918 if (q > p && *q == NUL)
14919 {
14920 /* Ignore trailing path separator. */
14921 q[-1] = NUL;
14922 q = gettail(p);
14923 }
14924 if (q > p && !mch_isFullName(buf))
14925 {
14926 /* symlink is relative to directory of argument */
14927 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14928 if (cpy != NULL)
14929 {
14930 STRCPY(cpy, p);
14931 STRCPY(gettail(cpy), buf);
14932 vim_free(p);
14933 p = cpy;
14934 }
14935 }
14936 else
14937 {
14938 vim_free(p);
14939 p = vim_strsave(buf);
14940 }
14941 }
14942
14943 if (remain == NULL)
14944 break;
14945
14946 /* Append the first path component of "remain" to "p". */
14947 q = getnextcomp(remain + 1);
14948 len = q - remain - (*q != NUL);
14949 cpy = vim_strnsave(p, STRLEN(p) + len);
14950 if (cpy != NULL)
14951 {
14952 STRNCAT(cpy, remain, len);
14953 vim_free(p);
14954 p = cpy;
14955 }
14956 /* Shorten "remain". */
14957 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014958 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 else
14960 {
14961 vim_free(remain);
14962 remain = NULL;
14963 }
14964 }
14965
14966 /* If the result is a relative path name, make it explicitly relative to
14967 * the current directory if and only if the argument had this form. */
14968 if (!vim_ispathsep(*p))
14969 {
14970 if (is_relative_to_current
14971 && *p != NUL
14972 && !(p[0] == '.'
14973 && (p[1] == NUL
14974 || vim_ispathsep(p[1])
14975 || (p[1] == '.'
14976 && (p[2] == NUL
14977 || vim_ispathsep(p[2]))))))
14978 {
14979 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014980 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981 if (cpy != NULL)
14982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 vim_free(p);
14984 p = cpy;
14985 }
14986 }
14987 else if (!is_relative_to_current)
14988 {
14989 /* Strip leading "./". */
14990 q = p;
14991 while (q[0] == '.' && vim_ispathsep(q[1]))
14992 q += 2;
14993 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014994 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 }
14996 }
14997
14998 /* Ensure that the result will have no trailing path separator
14999 * if the argument had none. But keep "/" or "//". */
15000 if (!has_trailing_pathsep)
15001 {
15002 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015003 if (after_pathsep(p, q))
15004 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 }
15006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015007 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 }
15009# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011# endif
15012#endif
15013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015
15016#ifdef HAVE_READLINK
15017fail:
15018#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020}
15021
15022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 */
15025 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 typval_T *argvars;
15028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029{
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 list_T *l;
15031 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033 if (argvars[0].v_type != VAR_LIST)
15034 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015035 else if ((l = argvars[0].vval.v_list) != NULL
15036 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 {
15038 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015039 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015040 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041 while (li != NULL)
15042 {
15043 ni = li->li_prev;
15044 list_append(l, li);
15045 li = ni;
15046 }
15047 rettv->vval.v_list = l;
15048 rettv->v_type = VAR_LIST;
15049 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015050 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052}
15053
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015054#define SP_NOMOVE 0x01 /* don't move cursor */
15055#define SP_REPEAT 0x02 /* repeat to find outer pair */
15056#define SP_RETCOUNT 0x04 /* return matchcount */
15057#define SP_SETPCMARK 0x08 /* set previous context mark */
15058#define SP_START 0x10 /* accept match at start position */
15059#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15060#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015061
Bram Moolenaar33570922005-01-25 22:26:29 +000015062static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063
15064/*
15065 * Get flags for a search function.
15066 * Possibly sets "p_ws".
15067 * Returns BACKWARD, FORWARD or zero (for an error).
15068 */
15069 static int
15070get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 int *flagsp;
15073{
15074 int dir = FORWARD;
15075 char_u *flags;
15076 char_u nbuf[NUMBUFLEN];
15077 int mask;
15078
15079 if (varp->v_type != VAR_UNKNOWN)
15080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015081 flags = get_tv_string_buf_chk(varp, nbuf);
15082 if (flags == NULL)
15083 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 while (*flags != NUL)
15085 {
15086 switch (*flags)
15087 {
15088 case 'b': dir = BACKWARD; break;
15089 case 'w': p_ws = TRUE; break;
15090 case 'W': p_ws = FALSE; break;
15091 default: mask = 0;
15092 if (flagsp != NULL)
15093 switch (*flags)
15094 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015095 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015096 case 'e': mask = SP_END; break;
15097 case 'm': mask = SP_RETCOUNT; break;
15098 case 'n': mask = SP_NOMOVE; break;
15099 case 'p': mask = SP_SUBPAT; break;
15100 case 'r': mask = SP_REPEAT; break;
15101 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 }
15103 if (mask == 0)
15104 {
15105 EMSG2(_(e_invarg2), flags);
15106 dir = 0;
15107 }
15108 else
15109 *flagsp |= mask;
15110 }
15111 if (dir == 0)
15112 break;
15113 ++flags;
15114 }
15115 }
15116 return dir;
15117}
15118
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015120 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015122 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015123search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015124 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015125 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015126 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015128 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 char_u *pat;
15130 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015131 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 int save_p_ws = p_ws;
15133 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015134 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015135 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015136 proftime_T tm;
15137#ifdef FEAT_RELTIME
15138 long time_limit = 0;
15139#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015140 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015141 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015144 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015145 if (dir == 0)
15146 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015147 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015148 if (flags & SP_START)
15149 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015150 if (flags & SP_END)
15151 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015152
Bram Moolenaar76929292008-01-06 19:07:36 +000015153 /* Optional arguments: line number to stop searching and timeout. */
15154 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015155 {
15156 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15157 if (lnum_stop < 0)
15158 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015159#ifdef FEAT_RELTIME
15160 if (argvars[3].v_type != VAR_UNKNOWN)
15161 {
15162 time_limit = get_tv_number_chk(&argvars[3], NULL);
15163 if (time_limit < 0)
15164 goto theend;
15165 }
15166#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015167 }
15168
Bram Moolenaar76929292008-01-06 19:07:36 +000015169#ifdef FEAT_RELTIME
15170 /* Set the time limit, if there is one. */
15171 profile_setlimit(time_limit, &tm);
15172#endif
15173
Bram Moolenaar231334e2005-07-25 20:46:57 +000015174 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015175 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015176 * Check to make sure only those flags are set.
15177 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15178 * flags cannot be set. Check for that condition also.
15179 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015180 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015181 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015184 goto theend;
15185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015187 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015188 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015189 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015190 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015192 if (flags & SP_SUBPAT)
15193 retval = subpatnum;
15194 else
15195 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015196 if (flags & SP_SETPCMARK)
15197 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015199 if (match_pos != NULL)
15200 {
15201 /* Store the match cursor position */
15202 match_pos->lnum = pos.lnum;
15203 match_pos->col = pos.col + 1;
15204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 /* "/$" will put the cursor after the end of the line, may need to
15206 * correct that here */
15207 check_cursor();
15208 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015209
15210 /* If 'n' flag is used: restore cursor position. */
15211 if (flags & SP_NOMOVE)
15212 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015213 else
15214 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015215theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015217
15218 return retval;
15219}
15220
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015221#ifdef FEAT_FLOAT
15222/*
15223 * "round({float})" function
15224 */
15225 static void
15226f_round(argvars, rettv)
15227 typval_T *argvars;
15228 typval_T *rettv;
15229{
15230 float_T f;
15231
15232 rettv->v_type = VAR_FLOAT;
15233 if (get_float_arg(argvars, &f) == OK)
15234 /* round() is not in C90, use ceil() or floor() instead. */
15235 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15236 else
15237 rettv->vval.v_float = 0.0;
15238}
15239#endif
15240
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015241/*
15242 * "search()" function
15243 */
15244 static void
15245f_search(argvars, rettv)
15246 typval_T *argvars;
15247 typval_T *rettv;
15248{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015249 int flags = 0;
15250
15251 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252}
15253
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015255 * "searchdecl()" function
15256 */
15257 static void
15258f_searchdecl(argvars, rettv)
15259 typval_T *argvars;
15260 typval_T *rettv;
15261{
15262 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015263 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015264 int error = FALSE;
15265 char_u *name;
15266
15267 rettv->vval.v_number = 1; /* default: FAIL */
15268
15269 name = get_tv_string_chk(&argvars[0]);
15270 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015271 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015272 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015273 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15274 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15275 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015276 if (!error && name != NULL)
15277 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015278 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015279}
15280
15281/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015282 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015284 static int
15285searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015286 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015287 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288{
15289 char_u *spat, *mpat, *epat;
15290 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 int dir;
15293 int flags = 0;
15294 char_u nbuf1[NUMBUFLEN];
15295 char_u nbuf2[NUMBUFLEN];
15296 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015297 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015298 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015299 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015302 spat = get_tv_string_chk(&argvars[0]);
15303 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15304 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15305 if (spat == NULL || mpat == NULL || epat == NULL)
15306 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 /* Handle the optional fourth argument: flags */
15309 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015310 if (dir == 0)
15311 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015312
15313 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015314 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15315 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015316 if ((flags & (SP_END | SP_SUBPAT)) != 0
15317 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015318 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015319 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015320 goto theend;
15321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015323 /* Using 'r' implies 'W', otherwise it doesn't work. */
15324 if (flags & SP_REPEAT)
15325 p_ws = FALSE;
15326
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015327 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015328 if (argvars[3].v_type == VAR_UNKNOWN
15329 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 skip = (char_u *)"";
15331 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015334 if (argvars[5].v_type != VAR_UNKNOWN)
15335 {
15336 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15337 if (lnum_stop < 0)
15338 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015339#ifdef FEAT_RELTIME
15340 if (argvars[6].v_type != VAR_UNKNOWN)
15341 {
15342 time_limit = get_tv_number_chk(&argvars[6], NULL);
15343 if (time_limit < 0)
15344 goto theend;
15345 }
15346#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015347 }
15348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015349 if (skip == NULL)
15350 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015352 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015353 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015354
15355theend:
15356 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015357
15358 return retval;
15359}
15360
15361/*
15362 * "searchpair()" function
15363 */
15364 static void
15365f_searchpair(argvars, rettv)
15366 typval_T *argvars;
15367 typval_T *rettv;
15368{
15369 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15370}
15371
15372/*
15373 * "searchpairpos()" function
15374 */
15375 static void
15376f_searchpairpos(argvars, rettv)
15377 typval_T *argvars;
15378 typval_T *rettv;
15379{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015380 pos_T match_pos;
15381 int lnum = 0;
15382 int col = 0;
15383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015384 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015385 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015386
15387 if (searchpair_cmn(argvars, &match_pos) > 0)
15388 {
15389 lnum = match_pos.lnum;
15390 col = match_pos.col;
15391 }
15392
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015393 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15394 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015395}
15396
15397/*
15398 * Search for a start/middle/end thing.
15399 * Used by searchpair(), see its documentation for the details.
15400 * Returns 0 or -1 for no match,
15401 */
15402 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015403do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15404 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015405 char_u *spat; /* start pattern */
15406 char_u *mpat; /* middle pattern */
15407 char_u *epat; /* end pattern */
15408 int dir; /* BACKWARD or FORWARD */
15409 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015410 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015411 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015412 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015413 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015414{
15415 char_u *save_cpo;
15416 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15417 long retval = 0;
15418 pos_T pos;
15419 pos_T firstpos;
15420 pos_T foundpos;
15421 pos_T save_cursor;
15422 pos_T save_pos;
15423 int n;
15424 int r;
15425 int nest = 1;
15426 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015427 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015428 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015429
15430 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15431 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015432 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015433
Bram Moolenaar76929292008-01-06 19:07:36 +000015434#ifdef FEAT_RELTIME
15435 /* Set the time limit, if there is one. */
15436 profile_setlimit(time_limit, &tm);
15437#endif
15438
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015439 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15440 * start/middle/end (pat3, for the top pair). */
15441 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15442 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15443 if (pat2 == NULL || pat3 == NULL)
15444 goto theend;
15445 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15446 if (*mpat == NUL)
15447 STRCPY(pat3, pat2);
15448 else
15449 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15450 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015451 if (flags & SP_START)
15452 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015453
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 save_cursor = curwin->w_cursor;
15455 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015456 clearpos(&firstpos);
15457 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 pat = pat3;
15459 for (;;)
15460 {
15461 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015462 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15464 /* didn't find it or found the first match again: FAIL */
15465 break;
15466
15467 if (firstpos.lnum == 0)
15468 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015469 if (equalpos(pos, foundpos))
15470 {
15471 /* Found the same position again. Can happen with a pattern that
15472 * has "\zs" at the end and searching backwards. Advance one
15473 * character and try again. */
15474 if (dir == BACKWARD)
15475 decl(&pos);
15476 else
15477 incl(&pos);
15478 }
15479 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015481 /* clear the start flag to avoid getting stuck here */
15482 options &= ~SEARCH_START;
15483
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 /* If the skip pattern matches, ignore this match. */
15485 if (*skip != NUL)
15486 {
15487 save_pos = curwin->w_cursor;
15488 curwin->w_cursor = pos;
15489 r = eval_to_bool(skip, &err, NULL, FALSE);
15490 curwin->w_cursor = save_pos;
15491 if (err)
15492 {
15493 /* Evaluating {skip} caused an error, break here. */
15494 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015495 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 break;
15497 }
15498 if (r)
15499 continue;
15500 }
15501
15502 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15503 {
15504 /* Found end when searching backwards or start when searching
15505 * forward: nested pair. */
15506 ++nest;
15507 pat = pat2; /* nested, don't search for middle */
15508 }
15509 else
15510 {
15511 /* Found end when searching forward or start when searching
15512 * backward: end of (nested) pair; or found middle in outer pair. */
15513 if (--nest == 1)
15514 pat = pat3; /* outer level, search for middle */
15515 }
15516
15517 if (nest == 0)
15518 {
15519 /* Found the match: return matchcount or line number. */
15520 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015521 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015523 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015524 if (flags & SP_SETPCMARK)
15525 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 curwin->w_cursor = pos;
15527 if (!(flags & SP_REPEAT))
15528 break;
15529 nest = 1; /* search for next unmatched */
15530 }
15531 }
15532
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015533 if (match_pos != NULL)
15534 {
15535 /* Store the match cursor position */
15536 match_pos->lnum = curwin->w_cursor.lnum;
15537 match_pos->col = curwin->w_cursor.col + 1;
15538 }
15539
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015541 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 curwin->w_cursor = save_cursor;
15543
15544theend:
15545 vim_free(pat2);
15546 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015547 if (p_cpo == empty_option)
15548 p_cpo = save_cpo;
15549 else
15550 /* Darn, evaluating the {skip} expression changed the value. */
15551 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015552
15553 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015556/*
15557 * "searchpos()" function
15558 */
15559 static void
15560f_searchpos(argvars, rettv)
15561 typval_T *argvars;
15562 typval_T *rettv;
15563{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015564 pos_T match_pos;
15565 int lnum = 0;
15566 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015567 int n;
15568 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015569
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015570 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015571 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015572
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015573 n = search_cmn(argvars, &match_pos, &flags);
15574 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015575 {
15576 lnum = match_pos.lnum;
15577 col = match_pos.col;
15578 }
15579
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015580 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15581 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015582 if (flags & SP_SUBPAT)
15583 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015584}
15585
15586
Bram Moolenaar0d660222005-01-07 21:51:51 +000015587 static void
15588f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015589 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592#ifdef FEAT_CLIENTSERVER
15593 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 char_u *server = get_tv_string_chk(&argvars[0]);
15595 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 if (server == NULL || reply == NULL)
15599 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 if (check_restricted() || check_secure())
15601 return;
15602# ifdef FEAT_X11
15603 if (check_connection() == FAIL)
15604 return;
15605# endif
15606
15607 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015609 EMSG(_("E258: Unable to send to client"));
15610 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015612 rettv->vval.v_number = 0;
15613#else
15614 rettv->vval.v_number = -1;
15615#endif
15616}
15617
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618 static void
15619f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015620 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015621 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015622{
15623 char_u *r = NULL;
15624
15625#ifdef FEAT_CLIENTSERVER
15626# ifdef WIN32
15627 r = serverGetVimNames();
15628# else
15629 make_connection();
15630 if (X_DISPLAY != NULL)
15631 r = serverGetVimNames(X_DISPLAY);
15632# endif
15633#endif
15634 rettv->v_type = VAR_STRING;
15635 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636}
15637
15638/*
15639 * "setbufvar()" function
15640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015644 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645{
15646 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015649 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 char_u nbuf[NUMBUFLEN];
15651
15652 if (check_restricted() || check_secure())
15653 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015654 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15655 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 varp = &argvars[2];
15658
15659 if (buf != NULL && varname != NULL && varp != NULL)
15660 {
15661 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663
15664 if (*varname == '&')
15665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015666 long numval;
15667 char_u *strval;
15668 int error = FALSE;
15669
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 numval = get_tv_number_chk(varp, &error);
15672 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 if (!error && strval != NULL)
15674 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 }
15676 else
15677 {
15678 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15679 if (bufvarname != NULL)
15680 {
15681 STRCPY(bufvarname, "b:");
15682 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015683 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 vim_free(bufvarname);
15685 }
15686 }
15687
15688 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691}
15692
15693/*
15694 * "setcmdpos()" function
15695 */
15696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015698 typval_T *argvars;
15699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015701 int pos = (int)get_tv_number(&argvars[0]) - 1;
15702
15703 if (pos >= 0)
15704 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705}
15706
15707/*
15708 * "setline()" function
15709 */
15710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015711f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015712 typval_T *argvars;
15713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714{
15715 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015716 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015717 list_T *l = NULL;
15718 listitem_T *li = NULL;
15719 long added = 0;
15720 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015722 lnum = get_tv_lnum(&argvars[0]);
15723 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015725 l = argvars[1].vval.v_list;
15726 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015728 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015730
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015731 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015732 for (;;)
15733 {
15734 if (l != NULL)
15735 {
15736 /* list argument, get next string */
15737 if (li == NULL)
15738 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015739 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015740 li = li->li_next;
15741 }
15742
15743 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015744 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015745 break;
15746 if (lnum <= curbuf->b_ml.ml_line_count)
15747 {
15748 /* existing line, replace it */
15749 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15750 {
15751 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015752 if (lnum == curwin->w_cursor.lnum)
15753 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015754 rettv->vval.v_number = 0; /* OK */
15755 }
15756 }
15757 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15758 {
15759 /* lnum is one past the last line, append the line */
15760 ++added;
15761 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15762 rettv->vval.v_number = 0; /* OK */
15763 }
15764
15765 if (l == NULL) /* only one string argument */
15766 break;
15767 ++lnum;
15768 }
15769
15770 if (added > 0)
15771 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772}
15773
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015774static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15775
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015777 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015778 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015779 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015780set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015781 win_T *wp UNUSED;
15782 typval_T *list_arg UNUSED;
15783 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015784 typval_T *rettv;
15785{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015786#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015787 char_u *act;
15788 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015789#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015790
Bram Moolenaar2641f772005-03-25 21:58:17 +000015791 rettv->vval.v_number = -1;
15792
15793#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015794 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015795 EMSG(_(e_listreq));
15796 else
15797 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015798 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015799
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015800 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015801 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015802 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 if (act == NULL)
15804 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015805 if (*act == 'a' || *act == 'r')
15806 action = *act;
15807 }
15808
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015809 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015810 rettv->vval.v_number = 0;
15811 }
15812#endif
15813}
15814
15815/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015816 * "setloclist()" function
15817 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015818 static void
15819f_setloclist(argvars, rettv)
15820 typval_T *argvars;
15821 typval_T *rettv;
15822{
15823 win_T *win;
15824
15825 rettv->vval.v_number = -1;
15826
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015827 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015828 if (win != NULL)
15829 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15830}
15831
15832/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015833 * "setmatches()" function
15834 */
15835 static void
15836f_setmatches(argvars, rettv)
15837 typval_T *argvars;
15838 typval_T *rettv;
15839{
15840#ifdef FEAT_SEARCH_EXTRA
15841 list_T *l;
15842 listitem_T *li;
15843 dict_T *d;
15844
15845 rettv->vval.v_number = -1;
15846 if (argvars[0].v_type != VAR_LIST)
15847 {
15848 EMSG(_(e_listreq));
15849 return;
15850 }
15851 if ((l = argvars[0].vval.v_list) != NULL)
15852 {
15853
15854 /* To some extent make sure that we are dealing with a list from
15855 * "getmatches()". */
15856 li = l->lv_first;
15857 while (li != NULL)
15858 {
15859 if (li->li_tv.v_type != VAR_DICT
15860 || (d = li->li_tv.vval.v_dict) == NULL)
15861 {
15862 EMSG(_(e_invarg));
15863 return;
15864 }
15865 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15866 && dict_find(d, (char_u *)"pattern", -1) != NULL
15867 && dict_find(d, (char_u *)"priority", -1) != NULL
15868 && dict_find(d, (char_u *)"id", -1) != NULL))
15869 {
15870 EMSG(_(e_invarg));
15871 return;
15872 }
15873 li = li->li_next;
15874 }
15875
15876 clear_matches(curwin);
15877 li = l->lv_first;
15878 while (li != NULL)
15879 {
15880 d = li->li_tv.vval.v_dict;
15881 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15882 get_dict_string(d, (char_u *)"pattern", FALSE),
15883 (int)get_dict_number(d, (char_u *)"priority"),
15884 (int)get_dict_number(d, (char_u *)"id"));
15885 li = li->li_next;
15886 }
15887 rettv->vval.v_number = 0;
15888 }
15889#endif
15890}
15891
15892/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015893 * "setpos()" function
15894 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015895 static void
15896f_setpos(argvars, rettv)
15897 typval_T *argvars;
15898 typval_T *rettv;
15899{
15900 pos_T pos;
15901 int fnum;
15902 char_u *name;
15903
Bram Moolenaar08250432008-02-13 11:42:46 +000015904 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015905 name = get_tv_string_chk(argvars);
15906 if (name != NULL)
15907 {
15908 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15909 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015910 if (--pos.col < 0)
15911 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015912 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015913 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015914 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015915 if (fnum == curbuf->b_fnum)
15916 {
15917 curwin->w_cursor = pos;
15918 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015919 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015920 }
15921 else
15922 EMSG(_(e_invarg));
15923 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015924 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15925 {
15926 /* set mark */
15927 if (setmark_pos(name[1], &pos, fnum) == OK)
15928 rettv->vval.v_number = 0;
15929 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015930 else
15931 EMSG(_(e_invarg));
15932 }
15933 }
15934}
15935
15936/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015937 * "setqflist()" function
15938 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015939 static void
15940f_setqflist(argvars, rettv)
15941 typval_T *argvars;
15942 typval_T *rettv;
15943{
15944 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15945}
15946
15947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 * "setreg()" function
15949 */
15950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 typval_T *argvars;
15953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954{
15955 int regname;
15956 char_u *strregname;
15957 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015958 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 int append;
15960 char_u yank_type;
15961 long block_len;
15962
15963 block_len = -1;
15964 yank_type = MAUTO;
15965 append = FALSE;
15966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015967 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 if (strregname == NULL)
15971 return; /* type error; errmsg already given */
15972 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973 if (regname == 0 || regname == '@')
15974 regname = '"';
15975 else if (regname == '=')
15976 return;
15977
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015978 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015980 stropt = get_tv_string_chk(&argvars[2]);
15981 if (stropt == NULL)
15982 return; /* type error */
15983 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 switch (*stropt)
15985 {
15986 case 'a': case 'A': /* append */
15987 append = TRUE;
15988 break;
15989 case 'v': case 'c': /* character-wise selection */
15990 yank_type = MCHAR;
15991 break;
15992 case 'V': case 'l': /* line-wise selection */
15993 yank_type = MLINE;
15994 break;
15995#ifdef FEAT_VISUAL
15996 case 'b': case Ctrl_V: /* block-wise selection */
15997 yank_type = MBLOCK;
15998 if (VIM_ISDIGIT(stropt[1]))
15999 {
16000 ++stropt;
16001 block_len = getdigits(&stropt) - 1;
16002 --stropt;
16003 }
16004 break;
16005#endif
16006 }
16007 }
16008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 strval = get_tv_string_chk(&argvars[1]);
16010 if (strval != NULL)
16011 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016013 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014}
16015
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016016/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016017 * "settabvar()" function
16018 */
16019 static void
16020f_settabvar(argvars, rettv)
16021 typval_T *argvars;
16022 typval_T *rettv;
16023{
16024 tabpage_T *save_curtab;
16025 char_u *varname, *tabvarname;
16026 typval_T *varp;
16027 tabpage_T *tp;
16028
16029 rettv->vval.v_number = 0;
16030
16031 if (check_restricted() || check_secure())
16032 return;
16033
16034 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16035 varname = get_tv_string_chk(&argvars[1]);
16036 varp = &argvars[2];
16037
16038 if (tp != NULL && varname != NULL && varp != NULL)
16039 {
16040 save_curtab = curtab;
16041 goto_tabpage_tp(tp);
16042
16043 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16044 if (tabvarname != NULL)
16045 {
16046 STRCPY(tabvarname, "t:");
16047 STRCPY(tabvarname + 2, varname);
16048 set_var(tabvarname, varp, TRUE);
16049 vim_free(tabvarname);
16050 }
16051
16052 /* Restore current tabpage */
16053 if (valid_tabpage(save_curtab))
16054 goto_tabpage_tp(save_curtab);
16055 }
16056}
16057
16058/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016059 * "settabwinvar()" function
16060 */
16061 static void
16062f_settabwinvar(argvars, rettv)
16063 typval_T *argvars;
16064 typval_T *rettv;
16065{
16066 setwinvar(argvars, rettv, 1);
16067}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068
16069/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016070 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016073f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016074 typval_T *argvars;
16075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016077 setwinvar(argvars, rettv, 0);
16078}
16079
16080/*
16081 * "setwinvar()" and "settabwinvar()" functions
16082 */
16083 static void
16084setwinvar(argvars, rettv, off)
16085 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016086 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016087 int off;
16088{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 win_T *win;
16090#ifdef FEAT_WINDOWS
16091 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016092 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093#endif
16094 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016095 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016097 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098
16099 if (check_restricted() || check_secure())
16100 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016101
16102#ifdef FEAT_WINDOWS
16103 if (off == 1)
16104 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16105 else
16106 tp = curtab;
16107#endif
16108 win = find_win_by_nr(&argvars[off], tp);
16109 varname = get_tv_string_chk(&argvars[off + 1]);
16110 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111
16112 if (win != NULL && varname != NULL && varp != NULL)
16113 {
16114#ifdef FEAT_WINDOWS
16115 /* set curwin to be our win, temporarily */
16116 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016117 save_curtab = curtab;
16118 goto_tabpage_tp(tp);
16119 if (!win_valid(win))
16120 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 curwin = win;
16122 curbuf = curwin->w_buffer;
16123#endif
16124
16125 if (*varname == '&')
16126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016127 long numval;
16128 char_u *strval;
16129 int error = FALSE;
16130
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 numval = get_tv_number_chk(varp, &error);
16133 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016134 if (!error && strval != NULL)
16135 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 }
16137 else
16138 {
16139 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16140 if (winvarname != NULL)
16141 {
16142 STRCPY(winvarname, "w:");
16143 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016144 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 vim_free(winvarname);
16146 }
16147 }
16148
16149#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016150 /* Restore current tabpage and window, if still valid (autocomands can
16151 * make them invalid). */
16152 if (valid_tabpage(save_curtab))
16153 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 if (win_valid(save_curwin))
16155 {
16156 curwin = save_curwin;
16157 curbuf = curwin->w_buffer;
16158 }
16159#endif
16160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161}
16162
16163/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016164 * "shellescape({string})" function
16165 */
16166 static void
16167f_shellescape(argvars, rettv)
16168 typval_T *argvars;
16169 typval_T *rettv;
16170{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016171 rettv->vval.v_string = vim_strsave_shellescape(
16172 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016173 rettv->v_type = VAR_STRING;
16174}
16175
16176/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 */
16179 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 typval_T *argvars;
16182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 p = get_tv_string(&argvars[0]);
16187 rettv->vval.v_string = vim_strsave(p);
16188 simplify_filename(rettv->vval.v_string); /* simplify in place */
16189 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190}
16191
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016192#ifdef FEAT_FLOAT
16193/*
16194 * "sin()" function
16195 */
16196 static void
16197f_sin(argvars, rettv)
16198 typval_T *argvars;
16199 typval_T *rettv;
16200{
16201 float_T f;
16202
16203 rettv->v_type = VAR_FLOAT;
16204 if (get_float_arg(argvars, &f) == OK)
16205 rettv->vval.v_float = sin(f);
16206 else
16207 rettv->vval.v_float = 0.0;
16208}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016209
16210/*
16211 * "sinh()" function
16212 */
16213 static void
16214f_sinh(argvars, rettv)
16215 typval_T *argvars;
16216 typval_T *rettv;
16217{
16218 float_T f;
16219
16220 rettv->v_type = VAR_FLOAT;
16221 if (get_float_arg(argvars, &f) == OK)
16222 rettv->vval.v_float = sinh(f);
16223 else
16224 rettv->vval.v_float = 0.0;
16225}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016226#endif
16227
Bram Moolenaar0d660222005-01-07 21:51:51 +000016228static int
16229#ifdef __BORLANDC__
16230 _RTLENTRYF
16231#endif
16232 item_compare __ARGS((const void *s1, const void *s2));
16233static int
16234#ifdef __BORLANDC__
16235 _RTLENTRYF
16236#endif
16237 item_compare2 __ARGS((const void *s1, const void *s2));
16238
16239static int item_compare_ic;
16240static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242#define ITEM_COMPARE_FAIL 999
16243
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016245 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 static int
16248#ifdef __BORLANDC__
16249_RTLENTRYF
16250#endif
16251item_compare(s1, s2)
16252 const void *s1;
16253 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 char_u *p1, *p2;
16256 char_u *tofree1, *tofree2;
16257 int res;
16258 char_u numbuf1[NUMBUFLEN];
16259 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016261 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16262 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016263 if (p1 == NULL)
16264 p1 = (char_u *)"";
16265 if (p2 == NULL)
16266 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267 if (item_compare_ic)
16268 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270 res = STRCMP(p1, p2);
16271 vim_free(tofree1);
16272 vim_free(tofree2);
16273 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274}
16275
16276 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277#ifdef __BORLANDC__
16278_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016280item_compare2(s1, s2)
16281 const void *s1;
16282 const void *s2;
16283{
16284 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016286 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 /* shortcut after failure in previous call; compare all items equal */
16290 if (item_compare_func_err)
16291 return 0;
16292
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016293 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16294 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016295 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16296 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297
16298 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016299 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016300 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301 clear_tv(&argv[0]);
16302 clear_tv(&argv[1]);
16303
16304 if (res == FAIL)
16305 res = ITEM_COMPARE_FAIL;
16306 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016307 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16308 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016309 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 clear_tv(&rettv);
16311 return res;
16312}
16313
16314/*
16315 * "sort({list})" function
16316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 typval_T *argvars;
16320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321{
Bram Moolenaar33570922005-01-25 22:26:29 +000016322 list_T *l;
16323 listitem_T *li;
16324 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 long len;
16326 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 if (argvars[0].v_type != VAR_LIST)
16329 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 else
16331 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016332 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016333 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016334 return;
16335 rettv->vval.v_list = l;
16336 rettv->v_type = VAR_LIST;
16337 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339 len = list_len(l);
16340 if (len <= 1)
16341 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 item_compare_ic = FALSE;
16344 item_compare_func = NULL;
16345 if (argvars[1].v_type != VAR_UNKNOWN)
16346 {
16347 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016348 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 else
16350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 int error = FALSE;
16352
16353 i = get_tv_number_chk(&argvars[1], &error);
16354 if (error)
16355 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356 if (i == 1)
16357 item_compare_ic = TRUE;
16358 else
16359 item_compare_func = get_tv_string(&argvars[1]);
16360 }
16361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016364 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365 if (ptrs == NULL)
16366 return;
16367 i = 0;
16368 for (li = l->lv_first; li != NULL; li = li->li_next)
16369 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 /* test the compare function */
16373 if (item_compare_func != NULL
16374 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16375 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016376 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016378 {
16379 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 item_compare_func == NULL ? item_compare : item_compare2);
16382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 if (!item_compare_func_err)
16384 {
16385 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016386 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016387 l->lv_len = 0;
16388 for (i = 0; i < len; ++i)
16389 list_append(l, ptrs[i]);
16390 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391 }
16392
16393 vim_free(ptrs);
16394 }
16395}
16396
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016397/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016398 * "soundfold({word})" function
16399 */
16400 static void
16401f_soundfold(argvars, rettv)
16402 typval_T *argvars;
16403 typval_T *rettv;
16404{
16405 char_u *s;
16406
16407 rettv->v_type = VAR_STRING;
16408 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016409#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016410 rettv->vval.v_string = eval_soundfold(s);
16411#else
16412 rettv->vval.v_string = vim_strsave(s);
16413#endif
16414}
16415
16416/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016417 * "spellbadword()" function
16418 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016419 static void
16420f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016421 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016422 typval_T *rettv;
16423{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016424 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016425 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016426 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016427
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016428 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016429 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016430
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016431#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016432 if (argvars[0].v_type == VAR_UNKNOWN)
16433 {
16434 /* Find the start and length of the badly spelled word. */
16435 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16436 if (len != 0)
16437 word = ml_get_cursor();
16438 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016439 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016440 {
16441 char_u *str = get_tv_string_chk(&argvars[0]);
16442 int capcol = -1;
16443
16444 if (str != NULL)
16445 {
16446 /* Check the argument for spelling. */
16447 while (*str != NUL)
16448 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016449 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016450 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016451 {
16452 word = str;
16453 break;
16454 }
16455 str += len;
16456 }
16457 }
16458 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016459#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016461 list_append_string(rettv->vval.v_list, word, len);
16462 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016463 attr == HLF_SPB ? "bad" :
16464 attr == HLF_SPR ? "rare" :
16465 attr == HLF_SPL ? "local" :
16466 attr == HLF_SPC ? "caps" :
16467 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016468}
16469
16470/*
16471 * "spellsuggest()" function
16472 */
16473 static void
16474f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016475 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016476 typval_T *rettv;
16477{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016478#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016479 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016480 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016481 int maxcount;
16482 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016483 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016484 listitem_T *li;
16485 int need_capital = FALSE;
16486#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016487
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016488 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016489 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016490
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016491#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016492 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016493 {
16494 str = get_tv_string(&argvars[0]);
16495 if (argvars[1].v_type != VAR_UNKNOWN)
16496 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016497 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016498 if (maxcount <= 0)
16499 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016500 if (argvars[2].v_type != VAR_UNKNOWN)
16501 {
16502 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16503 if (typeerr)
16504 return;
16505 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016506 }
16507 else
16508 maxcount = 25;
16509
Bram Moolenaar4770d092006-01-12 23:22:24 +000016510 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016511
16512 for (i = 0; i < ga.ga_len; ++i)
16513 {
16514 str = ((char_u **)ga.ga_data)[i];
16515
16516 li = listitem_alloc();
16517 if (li == NULL)
16518 vim_free(str);
16519 else
16520 {
16521 li->li_tv.v_type = VAR_STRING;
16522 li->li_tv.v_lock = 0;
16523 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016524 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016525 }
16526 }
16527 ga_clear(&ga);
16528 }
16529#endif
16530}
16531
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016533f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016534 typval_T *argvars;
16535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016536{
16537 char_u *str;
16538 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016539 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016540 regmatch_T regmatch;
16541 char_u patbuf[NUMBUFLEN];
16542 char_u *save_cpo;
16543 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016545 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016546 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547
16548 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16549 save_cpo = p_cpo;
16550 p_cpo = (char_u *)"";
16551
16552 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016553 if (argvars[1].v_type != VAR_UNKNOWN)
16554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016555 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16556 if (pat == NULL)
16557 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016559 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016560 }
16561 if (pat == NULL || *pat == NUL)
16562 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016564 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016566 if (typeerr)
16567 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16570 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016572 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016573 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016575 if (*str == NUL)
16576 match = FALSE; /* empty item at the end */
16577 else
16578 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579 if (match)
16580 end = regmatch.startp[0];
16581 else
16582 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016583 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16584 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016586 if (list_append_string(rettv->vval.v_list, str,
16587 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016588 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016589 }
16590 if (!match)
16591 break;
16592 /* Advance to just after the match. */
16593 if (regmatch.endp[0] > str)
16594 col = 0;
16595 else
16596 {
16597 /* Don't get stuck at the same match. */
16598#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016599 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600#else
16601 col = 1;
16602#endif
16603 }
16604 str = regmatch.endp[0];
16605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609
Bram Moolenaar0d660222005-01-07 21:51:51 +000016610 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611}
16612
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016613#ifdef FEAT_FLOAT
16614/*
16615 * "sqrt()" function
16616 */
16617 static void
16618f_sqrt(argvars, rettv)
16619 typval_T *argvars;
16620 typval_T *rettv;
16621{
16622 float_T f;
16623
16624 rettv->v_type = VAR_FLOAT;
16625 if (get_float_arg(argvars, &f) == OK)
16626 rettv->vval.v_float = sqrt(f);
16627 else
16628 rettv->vval.v_float = 0.0;
16629}
16630
16631/*
16632 * "str2float()" function
16633 */
16634 static void
16635f_str2float(argvars, rettv)
16636 typval_T *argvars;
16637 typval_T *rettv;
16638{
16639 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16640
16641 if (*p == '+')
16642 p = skipwhite(p + 1);
16643 (void)string2float(p, &rettv->vval.v_float);
16644 rettv->v_type = VAR_FLOAT;
16645}
16646#endif
16647
Bram Moolenaar2c932302006-03-18 21:42:09 +000016648/*
16649 * "str2nr()" function
16650 */
16651 static void
16652f_str2nr(argvars, rettv)
16653 typval_T *argvars;
16654 typval_T *rettv;
16655{
16656 int base = 10;
16657 char_u *p;
16658 long n;
16659
16660 if (argvars[1].v_type != VAR_UNKNOWN)
16661 {
16662 base = get_tv_number(&argvars[1]);
16663 if (base != 8 && base != 10 && base != 16)
16664 {
16665 EMSG(_(e_invarg));
16666 return;
16667 }
16668 }
16669
16670 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016671 if (*p == '+')
16672 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016673 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16674 rettv->vval.v_number = n;
16675}
16676
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677#ifdef HAVE_STRFTIME
16678/*
16679 * "strftime({format}[, {time}])" function
16680 */
16681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016682f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 typval_T *argvars;
16684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685{
16686 char_u result_buf[256];
16687 struct tm *curtime;
16688 time_t seconds;
16689 char_u *p;
16690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016691 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016693 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016694 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 seconds = time(NULL);
16696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016697 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 curtime = localtime(&seconds);
16699 /* MSVC returns NULL for an invalid value of seconds. */
16700 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 else
16703 {
16704# ifdef FEAT_MBYTE
16705 vimconv_T conv;
16706 char_u *enc;
16707
16708 conv.vc_type = CONV_NONE;
16709 enc = enc_locale();
16710 convert_setup(&conv, p_enc, enc);
16711 if (conv.vc_type != CONV_NONE)
16712 p = string_convert(&conv, p, NULL);
16713# endif
16714 if (p != NULL)
16715 (void)strftime((char *)result_buf, sizeof(result_buf),
16716 (char *)p, curtime);
16717 else
16718 result_buf[0] = NUL;
16719
16720# ifdef FEAT_MBYTE
16721 if (conv.vc_type != CONV_NONE)
16722 vim_free(p);
16723 convert_setup(&conv, enc, p_enc);
16724 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 else
16727# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729
16730# ifdef FEAT_MBYTE
16731 /* Release conversion descriptors */
16732 convert_setup(&conv, NULL, NULL);
16733 vim_free(enc);
16734# endif
16735 }
16736}
16737#endif
16738
16739/*
16740 * "stridx()" function
16741 */
16742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 typval_T *argvars;
16745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746{
16747 char_u buf[NUMBUFLEN];
16748 char_u *needle;
16749 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016750 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016752 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016754 needle = get_tv_string_chk(&argvars[1]);
16755 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016756 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016757 if (needle == NULL || haystack == NULL)
16758 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759
Bram Moolenaar33570922005-01-25 22:26:29 +000016760 if (argvars[2].v_type != VAR_UNKNOWN)
16761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016762 int error = FALSE;
16763
16764 start_idx = get_tv_number_chk(&argvars[2], &error);
16765 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016767 if (start_idx >= 0)
16768 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016769 }
16770
16771 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16772 if (pos != NULL)
16773 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774}
16775
16776/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016777 * "string()" function
16778 */
16779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016780f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 typval_T *argvars;
16782 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016783{
16784 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016785 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016788 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016789 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016790 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792}
16793
16794/*
16795 * "strlen()" function
16796 */
16797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016798f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016799 typval_T *argvars;
16800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016802 rettv->vval.v_number = (varnumber_T)(STRLEN(
16803 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804}
16805
16806/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016807 * "strchars()" function
16808 */
16809 static void
16810f_strchars(argvars, rettv)
16811 typval_T *argvars;
16812 typval_T *rettv;
16813{
16814 char_u *s = get_tv_string(&argvars[0]);
16815#ifdef FEAT_MBYTE
16816 varnumber_T len = 0;
16817
16818 while (*s != NUL)
16819 {
16820 mb_cptr2char_adv(&s);
16821 ++len;
16822 }
16823 rettv->vval.v_number = len;
16824#else
16825 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16826#endif
16827}
16828
16829/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016830 * "strdisplaywidth()" function
16831 */
16832 static void
16833f_strdisplaywidth(argvars, rettv)
16834 typval_T *argvars;
16835 typval_T *rettv;
16836{
16837 char_u *s = get_tv_string(&argvars[0]);
16838 int col = 0;
16839
16840 if (argvars[1].v_type != VAR_UNKNOWN)
16841 col = get_tv_number(&argvars[1]);
16842
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016843 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016844}
16845
16846/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016847 * "strwidth()" function
16848 */
16849 static void
16850f_strwidth(argvars, rettv)
16851 typval_T *argvars;
16852 typval_T *rettv;
16853{
16854 char_u *s = get_tv_string(&argvars[0]);
16855
16856 rettv->vval.v_number = (varnumber_T)(
16857#ifdef FEAT_MBYTE
16858 mb_string2cells(s, -1)
16859#else
16860 STRLEN(s)
16861#endif
16862 );
16863}
16864
16865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 * "strpart()" function
16867 */
16868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016869f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 typval_T *argvars;
16871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872{
16873 char_u *p;
16874 int n;
16875 int len;
16876 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016877 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016879 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 slen = (int)STRLEN(p);
16881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 n = get_tv_number_chk(&argvars[1], &error);
16883 if (error)
16884 len = 0;
16885 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016886 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 else
16888 len = slen - n; /* default len: all bytes that are available. */
16889
16890 /*
16891 * Only return the overlap between the specified part and the actual
16892 * string.
16893 */
16894 if (n < 0)
16895 {
16896 len += n;
16897 n = 0;
16898 }
16899 else if (n > slen)
16900 n = slen;
16901 if (len < 0)
16902 len = 0;
16903 else if (n + len > slen)
16904 len = slen - n;
16905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016906 rettv->v_type = VAR_STRING;
16907 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908}
16909
16910/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 * "strridx()" function
16912 */
16913 static void
16914f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016915 typval_T *argvars;
16916 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917{
16918 char_u buf[NUMBUFLEN];
16919 char_u *needle;
16920 char_u *haystack;
16921 char_u *rest;
16922 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016923 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016925 needle = get_tv_string_chk(&argvars[1]);
16926 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016927
16928 rettv->vval.v_number = -1;
16929 if (needle == NULL || haystack == NULL)
16930 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016931
16932 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016933 if (argvars[2].v_type != VAR_UNKNOWN)
16934 {
16935 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016937 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016939 }
16940 else
16941 end_idx = haystack_len;
16942
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016944 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016946 lastmatch = haystack + end_idx;
16947 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016949 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 for (rest = haystack; *rest != '\0'; ++rest)
16951 {
16952 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016953 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 break;
16955 lastmatch = rest;
16956 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016957 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958
16959 if (lastmatch == NULL)
16960 rettv->vval.v_number = -1;
16961 else
16962 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16963}
16964
16965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 * "strtrans()" function
16967 */
16968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016970 typval_T *argvars;
16971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 rettv->v_type = VAR_STRING;
16974 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975}
16976
16977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 * "submatch()" function
16979 */
16980 static void
16981f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 typval_T *argvars;
16983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984{
16985 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 rettv->vval.v_string =
16987 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988}
16989
16990/*
16991 * "substitute()" function
16992 */
16993 static void
16994f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 typval_T *argvars;
16996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997{
16998 char_u patbuf[NUMBUFLEN];
16999 char_u subbuf[NUMBUFLEN];
17000 char_u flagsbuf[NUMBUFLEN];
17001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 char_u *str = get_tv_string_chk(&argvars[0]);
17003 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17004 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17005 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17006
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17009 rettv->vval.v_string = NULL;
17010 else
17011 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012}
17013
17014/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017015 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017018f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017019 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021{
17022 int id = 0;
17023#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017024 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 long col;
17026 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017027 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 lnum = get_tv_lnum(argvars); /* -1 on type error */
17030 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17031 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017034 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017035 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036#endif
17037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017038 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039}
17040
17041/*
17042 * "synIDattr(id, what [, mode])" function
17043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048{
17049 char_u *p = NULL;
17050#ifdef FEAT_SYN_HL
17051 int id;
17052 char_u *what;
17053 char_u *mode;
17054 char_u modebuf[NUMBUFLEN];
17055 int modec;
17056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 id = get_tv_number(&argvars[0]);
17058 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017059 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017063 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 modec = 0; /* replace invalid with current */
17065 }
17066 else
17067 {
17068#ifdef FEAT_GUI
17069 if (gui.in_use)
17070 modec = 'g';
17071 else
17072#endif
17073 if (t_colors > 1)
17074 modec = 'c';
17075 else
17076 modec = 't';
17077 }
17078
17079
17080 switch (TOLOWER_ASC(what[0]))
17081 {
17082 case 'b':
17083 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17084 p = highlight_color(id, what, modec);
17085 else /* bold */
17086 p = highlight_has_attr(id, HL_BOLD, modec);
17087 break;
17088
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017089 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 p = highlight_color(id, what, modec);
17091 break;
17092
17093 case 'i':
17094 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17095 p = highlight_has_attr(id, HL_INVERSE, modec);
17096 else /* italic */
17097 p = highlight_has_attr(id, HL_ITALIC, modec);
17098 break;
17099
17100 case 'n': /* name */
17101 p = get_highlight_name(NULL, id - 1);
17102 break;
17103
17104 case 'r': /* reverse */
17105 p = highlight_has_attr(id, HL_INVERSE, modec);
17106 break;
17107
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017108 case 's':
17109 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17110 p = highlight_color(id, what, modec);
17111 else /* standout */
17112 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 break;
17114
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017115 case 'u':
17116 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17117 /* underline */
17118 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17119 else
17120 /* undercurl */
17121 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 break;
17123 }
17124
17125 if (p != NULL)
17126 p = vim_strsave(p);
17127#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 rettv->v_type = VAR_STRING;
17129 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130}
17131
17132/*
17133 * "synIDtrans(id)" function
17134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017136f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139{
17140 int id;
17141
17142#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144
17145 if (id > 0)
17146 id = syn_get_final_id(id);
17147 else
17148#endif
17149 id = 0;
17150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017151 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152}
17153
17154/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017155 * "synconcealed(lnum, col)" function
17156 */
17157 static void
17158f_synconcealed(argvars, rettv)
17159 typval_T *argvars UNUSED;
17160 typval_T *rettv;
17161{
17162#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17163 long lnum;
17164 long col;
17165 int syntax_flags = 0;
17166 int cchar;
17167 int matchid = 0;
17168 char_u str[NUMBUFLEN];
17169#endif
17170
17171 rettv->v_type = VAR_LIST;
17172 rettv->vval.v_list = NULL;
17173
17174#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17175 lnum = get_tv_lnum(argvars); /* -1 on type error */
17176 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17177
17178 vim_memset(str, NUL, sizeof(str));
17179
17180 if (rettv_list_alloc(rettv) != FAIL)
17181 {
17182 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17183 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17184 && curwin->w_p_cole > 0)
17185 {
17186 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17187 syntax_flags = get_syntax_info(&matchid);
17188
17189 /* get the conceal character */
17190 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17191 {
17192 cchar = syn_get_sub_char();
17193 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17194 cchar = lcs_conceal;
17195 if (cchar != NUL)
17196 {
17197# ifdef FEAT_MBYTE
17198 if (has_mbyte)
17199 (*mb_char2bytes)(cchar, str);
17200 else
17201# endif
17202 str[0] = cchar;
17203 }
17204 }
17205 }
17206
17207 list_append_number(rettv->vval.v_list,
17208 (syntax_flags & HL_CONCEAL) != 0);
17209 /* -1 to auto-determine strlen */
17210 list_append_string(rettv->vval.v_list, str, -1);
17211 list_append_number(rettv->vval.v_list, matchid);
17212 }
17213#endif
17214}
17215
17216/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017217 * "synstack(lnum, col)" function
17218 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017219 static void
17220f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017221 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017222 typval_T *rettv;
17223{
17224#ifdef FEAT_SYN_HL
17225 long lnum;
17226 long col;
17227 int i;
17228 int id;
17229#endif
17230
17231 rettv->v_type = VAR_LIST;
17232 rettv->vval.v_list = NULL;
17233
17234#ifdef FEAT_SYN_HL
17235 lnum = get_tv_lnum(argvars); /* -1 on type error */
17236 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17237
17238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017239 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017240 && rettv_list_alloc(rettv) != FAIL)
17241 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017242 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017243 for (i = 0; ; ++i)
17244 {
17245 id = syn_get_stack_item(i);
17246 if (id < 0)
17247 break;
17248 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17249 break;
17250 }
17251 }
17252#endif
17253}
17254
17255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 * "system()" function
17257 */
17258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *argvars;
17261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017263 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017265 char_u *infile = NULL;
17266 char_u buf[NUMBUFLEN];
17267 int err = FALSE;
17268 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017270 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017271 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017274 {
17275 /*
17276 * Write the string to a temp file, to be used for input of the shell
17277 * command.
17278 */
17279 if ((infile = vim_tempname('i')) == NULL)
17280 {
17281 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017282 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017283 }
17284
17285 fd = mch_fopen((char *)infile, WRITEBIN);
17286 if (fd == NULL)
17287 {
17288 EMSG2(_(e_notopen), infile);
17289 goto done;
17290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 p = get_tv_string_buf_chk(&argvars[1], buf);
17292 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017293 {
17294 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017295 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017296 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017297 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17298 err = TRUE;
17299 if (fclose(fd) != 0)
17300 err = TRUE;
17301 if (err)
17302 {
17303 EMSG(_("E677: Error writing temp file"));
17304 goto done;
17305 }
17306 }
17307
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017308 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17309 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017310
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311#ifdef USE_CR
17312 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017313 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 {
17315 char_u *s;
17316
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017317 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 {
17319 if (*s == CAR)
17320 *s = NL;
17321 }
17322 }
17323#else
17324# ifdef USE_CRNL
17325 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017326 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 {
17328 char_u *s, *d;
17329
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017330 d = res;
17331 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 {
17333 if (s[0] == CAR && s[1] == NL)
17334 ++s;
17335 *d++ = *s;
17336 }
17337 *d = NUL;
17338 }
17339# endif
17340#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017341
17342done:
17343 if (infile != NULL)
17344 {
17345 mch_remove(infile);
17346 vim_free(infile);
17347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->v_type = VAR_STRING;
17349 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350}
17351
17352/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017353 * "tabpagebuflist()" function
17354 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017355 static void
17356f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017357 typval_T *argvars UNUSED;
17358 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017359{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017360#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017361 tabpage_T *tp;
17362 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017363
17364 if (argvars[0].v_type == VAR_UNKNOWN)
17365 wp = firstwin;
17366 else
17367 {
17368 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17369 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017370 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017371 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017372 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017373 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017374 for (; wp != NULL; wp = wp->w_next)
17375 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017376 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017377 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017378 }
17379#endif
17380}
17381
17382
17383/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017384 * "tabpagenr()" function
17385 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017386 static void
17387f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017388 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017389 typval_T *rettv;
17390{
17391 int nr = 1;
17392#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017393 char_u *arg;
17394
17395 if (argvars[0].v_type != VAR_UNKNOWN)
17396 {
17397 arg = get_tv_string_chk(&argvars[0]);
17398 nr = 0;
17399 if (arg != NULL)
17400 {
17401 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017402 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017403 else
17404 EMSG2(_(e_invexpr2), arg);
17405 }
17406 }
17407 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017408 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017409#endif
17410 rettv->vval.v_number = nr;
17411}
17412
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017413
17414#ifdef FEAT_WINDOWS
17415static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17416
17417/*
17418 * Common code for tabpagewinnr() and winnr().
17419 */
17420 static int
17421get_winnr(tp, argvar)
17422 tabpage_T *tp;
17423 typval_T *argvar;
17424{
17425 win_T *twin;
17426 int nr = 1;
17427 win_T *wp;
17428 char_u *arg;
17429
17430 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17431 if (argvar->v_type != VAR_UNKNOWN)
17432 {
17433 arg = get_tv_string_chk(argvar);
17434 if (arg == NULL)
17435 nr = 0; /* type error; errmsg already given */
17436 else if (STRCMP(arg, "$") == 0)
17437 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17438 else if (STRCMP(arg, "#") == 0)
17439 {
17440 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17441 if (twin == NULL)
17442 nr = 0;
17443 }
17444 else
17445 {
17446 EMSG2(_(e_invexpr2), arg);
17447 nr = 0;
17448 }
17449 }
17450
17451 if (nr > 0)
17452 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17453 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017454 {
17455 if (wp == NULL)
17456 {
17457 /* didn't find it in this tabpage */
17458 nr = 0;
17459 break;
17460 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017461 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017462 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017463 return nr;
17464}
17465#endif
17466
17467/*
17468 * "tabpagewinnr()" function
17469 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017470 static void
17471f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017472 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017473 typval_T *rettv;
17474{
17475 int nr = 1;
17476#ifdef FEAT_WINDOWS
17477 tabpage_T *tp;
17478
17479 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17480 if (tp == NULL)
17481 nr = 0;
17482 else
17483 nr = get_winnr(tp, &argvars[1]);
17484#endif
17485 rettv->vval.v_number = nr;
17486}
17487
17488
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017489/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017490 * "tagfiles()" function
17491 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017492 static void
17493f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017494 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017495 typval_T *rettv;
17496{
17497 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017498 tagname_T tn;
17499 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017502 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017503
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017504 for (first = TRUE; ; first = FALSE)
17505 if (get_tagfname(&tn, first, fname) == FAIL
17506 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017507 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017508 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017509}
17510
17511/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017512 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017513 */
17514 static void
17515f_taglist(argvars, rettv)
17516 typval_T *argvars;
17517 typval_T *rettv;
17518{
17519 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017520
17521 tag_pattern = get_tv_string(&argvars[0]);
17522
17523 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017524 if (*tag_pattern == NUL)
17525 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017526
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017527 if (rettv_list_alloc(rettv) == OK)
17528 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017529}
17530
17531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 * "tempname()" function
17533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017535f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017536 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538{
17539 static int x = 'A';
17540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017541 rettv->v_type = VAR_STRING;
17542 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543
17544 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17545 * names. Skip 'I' and 'O', they are used for shell redirection. */
17546 do
17547 {
17548 if (x == 'Z')
17549 x = '0';
17550 else if (x == '9')
17551 x = 'A';
17552 else
17553 {
17554#ifdef EBCDIC
17555 if (x == 'I')
17556 x = 'J';
17557 else if (x == 'R')
17558 x = 'S';
17559 else
17560#endif
17561 ++x;
17562 }
17563 } while (x == 'I' || x == 'O');
17564}
17565
17566/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017567 * "test(list)" function: Just checking the walls...
17568 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017569 static void
17570f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017571 typval_T *argvars UNUSED;
17572 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017573{
17574 /* Used for unit testing. Change the code below to your liking. */
17575#if 0
17576 listitem_T *li;
17577 list_T *l;
17578 char_u *bad, *good;
17579
17580 if (argvars[0].v_type != VAR_LIST)
17581 return;
17582 l = argvars[0].vval.v_list;
17583 if (l == NULL)
17584 return;
17585 li = l->lv_first;
17586 if (li == NULL)
17587 return;
17588 bad = get_tv_string(&li->li_tv);
17589 li = li->li_next;
17590 if (li == NULL)
17591 return;
17592 good = get_tv_string(&li->li_tv);
17593 rettv->vval.v_number = test_edit_score(bad, good);
17594#endif
17595}
17596
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017597#ifdef FEAT_FLOAT
17598/*
17599 * "tan()" function
17600 */
17601 static void
17602f_tan(argvars, rettv)
17603 typval_T *argvars;
17604 typval_T *rettv;
17605{
17606 float_T f;
17607
17608 rettv->v_type = VAR_FLOAT;
17609 if (get_float_arg(argvars, &f) == OK)
17610 rettv->vval.v_float = tan(f);
17611 else
17612 rettv->vval.v_float = 0.0;
17613}
17614
17615/*
17616 * "tanh()" function
17617 */
17618 static void
17619f_tanh(argvars, rettv)
17620 typval_T *argvars;
17621 typval_T *rettv;
17622{
17623 float_T f;
17624
17625 rettv->v_type = VAR_FLOAT;
17626 if (get_float_arg(argvars, &f) == OK)
17627 rettv->vval.v_float = tanh(f);
17628 else
17629 rettv->vval.v_float = 0.0;
17630}
17631#endif
17632
Bram Moolenaard52d9742005-08-21 22:20:28 +000017633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 * "tolower(string)" function
17635 */
17636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017637f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017638 typval_T *argvars;
17639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640{
17641 char_u *p;
17642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017643 p = vim_strsave(get_tv_string(&argvars[0]));
17644 rettv->v_type = VAR_STRING;
17645 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646
17647 if (p != NULL)
17648 while (*p != NUL)
17649 {
17650#ifdef FEAT_MBYTE
17651 int l;
17652
17653 if (enc_utf8)
17654 {
17655 int c, lc;
17656
17657 c = utf_ptr2char(p);
17658 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017659 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 /* TODO: reallocate string when byte count changes. */
17661 if (utf_char2len(lc) == l)
17662 utf_char2bytes(lc, p);
17663 p += l;
17664 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017665 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 p += l; /* skip multi-byte character */
17667 else
17668#endif
17669 {
17670 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17671 ++p;
17672 }
17673 }
17674}
17675
17676/*
17677 * "toupper(string)" function
17678 */
17679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017681 typval_T *argvars;
17682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017685 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686}
17687
17688/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017689 * "tr(string, fromstr, tostr)" function
17690 */
17691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017692f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017693 typval_T *argvars;
17694 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017695{
17696 char_u *instr;
17697 char_u *fromstr;
17698 char_u *tostr;
17699 char_u *p;
17700#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017701 int inlen;
17702 int fromlen;
17703 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017704 int idx;
17705 char_u *cpstr;
17706 int cplen;
17707 int first = TRUE;
17708#endif
17709 char_u buf[NUMBUFLEN];
17710 char_u buf2[NUMBUFLEN];
17711 garray_T ga;
17712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017714 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17715 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017716
17717 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017718 rettv->v_type = VAR_STRING;
17719 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 if (fromstr == NULL || tostr == NULL)
17721 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017722 ga_init2(&ga, (int)sizeof(char), 80);
17723
17724#ifdef FEAT_MBYTE
17725 if (!has_mbyte)
17726#endif
17727 /* not multi-byte: fromstr and tostr must be the same length */
17728 if (STRLEN(fromstr) != STRLEN(tostr))
17729 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017730#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017731error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017732#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017733 EMSG2(_(e_invarg2), fromstr);
17734 ga_clear(&ga);
17735 return;
17736 }
17737
17738 /* fromstr and tostr have to contain the same number of chars */
17739 while (*instr != NUL)
17740 {
17741#ifdef FEAT_MBYTE
17742 if (has_mbyte)
17743 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017744 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017745 cpstr = instr;
17746 cplen = inlen;
17747 idx = 0;
17748 for (p = fromstr; *p != NUL; p += fromlen)
17749 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017750 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017751 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17752 {
17753 for (p = tostr; *p != NUL; p += tolen)
17754 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017755 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017756 if (idx-- == 0)
17757 {
17758 cplen = tolen;
17759 cpstr = p;
17760 break;
17761 }
17762 }
17763 if (*p == NUL) /* tostr is shorter than fromstr */
17764 goto error;
17765 break;
17766 }
17767 ++idx;
17768 }
17769
17770 if (first && cpstr == instr)
17771 {
17772 /* Check that fromstr and tostr have the same number of
17773 * (multi-byte) characters. Done only once when a character
17774 * of instr doesn't appear in fromstr. */
17775 first = FALSE;
17776 for (p = tostr; *p != NUL; p += tolen)
17777 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017778 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017779 --idx;
17780 }
17781 if (idx != 0)
17782 goto error;
17783 }
17784
17785 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017786 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017787 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017788
17789 instr += inlen;
17790 }
17791 else
17792#endif
17793 {
17794 /* When not using multi-byte chars we can do it faster. */
17795 p = vim_strchr(fromstr, *instr);
17796 if (p != NULL)
17797 ga_append(&ga, tostr[p - fromstr]);
17798 else
17799 ga_append(&ga, *instr);
17800 ++instr;
17801 }
17802 }
17803
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017804 /* add a terminating NUL */
17805 ga_grow(&ga, 1);
17806 ga_append(&ga, NUL);
17807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017808 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017809}
17810
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017811#ifdef FEAT_FLOAT
17812/*
17813 * "trunc({float})" function
17814 */
17815 static void
17816f_trunc(argvars, rettv)
17817 typval_T *argvars;
17818 typval_T *rettv;
17819{
17820 float_T f;
17821
17822 rettv->v_type = VAR_FLOAT;
17823 if (get_float_arg(argvars, &f) == OK)
17824 /* trunc() is not in C90, use floor() or ceil() instead. */
17825 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17826 else
17827 rettv->vval.v_float = 0.0;
17828}
17829#endif
17830
Bram Moolenaar8299df92004-07-10 09:47:34 +000017831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 * "type(expr)" function
17833 */
17834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 typval_T *argvars;
17837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017839 int n;
17840
17841 switch (argvars[0].v_type)
17842 {
17843 case VAR_NUMBER: n = 0; break;
17844 case VAR_STRING: n = 1; break;
17845 case VAR_FUNC: n = 2; break;
17846 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017847 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017848#ifdef FEAT_FLOAT
17849 case VAR_FLOAT: n = 5; break;
17850#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017851 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17852 }
17853 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854}
17855
17856/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017857 * "undofile(name)" function
17858 */
17859 static void
17860f_undofile(argvars, rettv)
17861 typval_T *argvars;
17862 typval_T *rettv;
17863{
17864 rettv->v_type = VAR_STRING;
17865#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017866 {
17867 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17868
17869 if (ffname != NULL)
17870 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17871 vim_free(ffname);
17872 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017873#else
17874 rettv->vval.v_string = NULL;
17875#endif
17876}
17877
17878/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017879 * "undotree()" function
17880 */
17881 static void
17882f_undotree(argvars, rettv)
17883 typval_T *argvars UNUSED;
17884 typval_T *rettv;
17885{
17886 if (rettv_dict_alloc(rettv) == OK)
17887 {
17888 dict_T *dict = rettv->vval.v_dict;
17889 list_T *list;
17890
Bram Moolenaar730cde92010-06-27 05:18:54 +020017891 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017892 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017893 dict_add_nr_str(dict, "save_last",
17894 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017895 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17896 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017897 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017898
17899 list = list_alloc();
17900 if (list != NULL)
17901 {
17902 u_eval_tree(curbuf->b_u_oldhead, list);
17903 dict_add_list(dict, "entries", list);
17904 }
17905 }
17906}
17907
17908/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017909 * "values(dict)" function
17910 */
17911 static void
17912f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017913 typval_T *argvars;
17914 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017915{
17916 dict_list(argvars, rettv, 1);
17917}
17918
17919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 * "virtcol(string)" function
17921 */
17922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017923f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017924 typval_T *argvars;
17925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926{
17927 colnr_T vcol = 0;
17928 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017929 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017931 fp = var2fpos(&argvars[0], FALSE, &fnum);
17932 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17933 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 {
17935 getvvcol(curwin, fp, NULL, NULL, &vcol);
17936 ++vcol;
17937 }
17938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017939 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940}
17941
17942/*
17943 * "visualmode()" function
17944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017946f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017947 typval_T *argvars UNUSED;
17948 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949{
17950#ifdef FEAT_VISUAL
17951 char_u str[2];
17952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 str[0] = curbuf->b_visual_mode_eval;
17955 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017956 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957
17958 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017959 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961#endif
17962}
17963
17964/*
17965 * "winbufnr(nr)" function
17966 */
17967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017968f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017969 typval_T *argvars;
17970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971{
17972 win_T *wp;
17973
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017974 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017978 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979}
17980
17981/*
17982 * "wincol()" function
17983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017985f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988{
17989 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991}
17992
17993/*
17994 * "winheight(nr)" function
17995 */
17996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 typval_T *argvars;
17999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000{
18001 win_T *wp;
18002
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018003 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018005 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018007 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008}
18009
18010/*
18011 * "winline()" function
18012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018014f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018015 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017{
18018 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018019 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020}
18021
18022/*
18023 * "winnr()" function
18024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018026f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018027 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029{
18030 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018031
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018033 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018035 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036}
18037
18038/*
18039 * "winrestcmd()" function
18040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018042f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045{
18046#ifdef FEAT_WINDOWS
18047 win_T *wp;
18048 int winnr = 1;
18049 garray_T ga;
18050 char_u buf[50];
18051
18052 ga_init2(&ga, (int)sizeof(char), 70);
18053 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18054 {
18055 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18056 ga_concat(&ga, buf);
18057# ifdef FEAT_VERTSPLIT
18058 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18059 ga_concat(&ga, buf);
18060# endif
18061 ++winnr;
18062 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018063 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018065 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018069 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070}
18071
18072/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018073 * "winrestview()" function
18074 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018075 static void
18076f_winrestview(argvars, rettv)
18077 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018078 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018079{
18080 dict_T *dict;
18081
18082 if (argvars[0].v_type != VAR_DICT
18083 || (dict = argvars[0].vval.v_dict) == NULL)
18084 EMSG(_(e_invarg));
18085 else
18086 {
18087 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18088 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18089#ifdef FEAT_VIRTUALEDIT
18090 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18091#endif
18092 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018093 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018094
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018095 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018096#ifdef FEAT_DIFF
18097 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18098#endif
18099 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18100 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18101
18102 check_cursor();
18103 changed_cline_bef_curs();
18104 invalidate_botline();
18105 redraw_later(VALID);
18106
18107 if (curwin->w_topline == 0)
18108 curwin->w_topline = 1;
18109 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18110 curwin->w_topline = curbuf->b_ml.ml_line_count;
18111#ifdef FEAT_DIFF
18112 check_topfill(curwin, TRUE);
18113#endif
18114 }
18115}
18116
18117/*
18118 * "winsaveview()" function
18119 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018120 static void
18121f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018122 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018123 typval_T *rettv;
18124{
18125 dict_T *dict;
18126
Bram Moolenaara800b422010-06-27 01:15:55 +020018127 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018128 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018129 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018130
18131 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18132 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18133#ifdef FEAT_VIRTUALEDIT
18134 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18135#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018136 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018137 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18138
18139 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18140#ifdef FEAT_DIFF
18141 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18142#endif
18143 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18144 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18145}
18146
18147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 * "winwidth(nr)" function
18149 */
18150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 typval_T *argvars;
18153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
18155 win_T *wp;
18156
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018157 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 else
18161#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018164 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165#endif
18166}
18167
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018169 * "writefile()" function
18170 */
18171 static void
18172f_writefile(argvars, rettv)
18173 typval_T *argvars;
18174 typval_T *rettv;
18175{
18176 int binary = FALSE;
18177 char_u *fname;
18178 FILE *fd;
18179 listitem_T *li;
18180 char_u *s;
18181 int ret = 0;
18182 int c;
18183
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018184 if (check_restricted() || check_secure())
18185 return;
18186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018187 if (argvars[0].v_type != VAR_LIST)
18188 {
18189 EMSG2(_(e_listarg), "writefile()");
18190 return;
18191 }
18192 if (argvars[0].vval.v_list == NULL)
18193 return;
18194
18195 if (argvars[2].v_type != VAR_UNKNOWN
18196 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18197 binary = TRUE;
18198
18199 /* Always open the file in binary mode, library functions have a mind of
18200 * their own about CR-LF conversion. */
18201 fname = get_tv_string(&argvars[1]);
18202 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18203 {
18204 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18205 ret = -1;
18206 }
18207 else
18208 {
18209 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18210 li = li->li_next)
18211 {
18212 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18213 {
18214 if (*s == '\n')
18215 c = putc(NUL, fd);
18216 else
18217 c = putc(*s, fd);
18218 if (c == EOF)
18219 {
18220 ret = -1;
18221 break;
18222 }
18223 }
18224 if (!binary || li->li_next != NULL)
18225 if (putc('\n', fd) == EOF)
18226 {
18227 ret = -1;
18228 break;
18229 }
18230 if (ret < 0)
18231 {
18232 EMSG(_(e_write));
18233 break;
18234 }
18235 }
18236 fclose(fd);
18237 }
18238
18239 rettv->vval.v_number = ret;
18240}
18241
18242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018244 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 */
18246 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018247var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018248 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018249 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018250 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018252 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018254 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255
Bram Moolenaara5525202006-03-02 22:52:09 +000018256 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018257 if (varp->v_type == VAR_LIST)
18258 {
18259 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018260 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018261 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018262 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018263
18264 l = varp->vval.v_list;
18265 if (l == NULL)
18266 return NULL;
18267
18268 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018269 pos.lnum = list_find_nr(l, 0L, &error);
18270 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018271 return NULL; /* invalid line number */
18272
18273 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018274 pos.col = list_find_nr(l, 1L, &error);
18275 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018276 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018277 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018278
18279 /* We accept "$" for the column number: last column. */
18280 li = list_find(l, 1L);
18281 if (li != NULL && li->li_tv.v_type == VAR_STRING
18282 && li->li_tv.vval.v_string != NULL
18283 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18284 pos.col = len + 1;
18285
Bram Moolenaara5525202006-03-02 22:52:09 +000018286 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018287 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018288 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018289 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018290
Bram Moolenaara5525202006-03-02 22:52:09 +000018291#ifdef FEAT_VIRTUALEDIT
18292 /* Get the virtual offset. Defaults to zero. */
18293 pos.coladd = list_find_nr(l, 2L, &error);
18294 if (error)
18295 pos.coladd = 0;
18296#endif
18297
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018298 return &pos;
18299 }
18300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 name = get_tv_string_chk(varp);
18302 if (name == NULL)
18303 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018304 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018306#ifdef FEAT_VISUAL
18307 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18308 {
18309 if (VIsual_active)
18310 return &VIsual;
18311 return &curwin->w_cursor;
18312 }
18313#endif
18314 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018316 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18318 return NULL;
18319 return pp;
18320 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018321
18322#ifdef FEAT_VIRTUALEDIT
18323 pos.coladd = 0;
18324#endif
18325
Bram Moolenaar477933c2007-07-17 14:32:23 +000018326 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018327 {
18328 pos.col = 0;
18329 if (name[1] == '0') /* "w0": first visible line */
18330 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018331 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018332 pos.lnum = curwin->w_topline;
18333 return &pos;
18334 }
18335 else if (name[1] == '$') /* "w$": last visible line */
18336 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018337 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018338 pos.lnum = curwin->w_botline - 1;
18339 return &pos;
18340 }
18341 }
18342 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018344 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 {
18346 pos.lnum = curbuf->b_ml.ml_line_count;
18347 pos.col = 0;
18348 }
18349 else
18350 {
18351 pos.lnum = curwin->w_cursor.lnum;
18352 pos.col = (colnr_T)STRLEN(ml_get_curline());
18353 }
18354 return &pos;
18355 }
18356 return NULL;
18357}
18358
18359/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018360 * Convert list in "arg" into a position and optional file number.
18361 * When "fnump" is NULL there is no file number, only 3 items.
18362 * Note that the column is passed on as-is, the caller may want to decrement
18363 * it to use 1 for the first column.
18364 * Return FAIL when conversion is not possible, doesn't check the position for
18365 * validity.
18366 */
18367 static int
18368list2fpos(arg, posp, fnump)
18369 typval_T *arg;
18370 pos_T *posp;
18371 int *fnump;
18372{
18373 list_T *l = arg->vval.v_list;
18374 long i = 0;
18375 long n;
18376
Bram Moolenaarbde35262006-07-23 20:12:24 +000018377 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18378 * when "fnump" isn't NULL and "coladd" is optional. */
18379 if (arg->v_type != VAR_LIST
18380 || l == NULL
18381 || l->lv_len < (fnump == NULL ? 2 : 3)
18382 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018383 return FAIL;
18384
18385 if (fnump != NULL)
18386 {
18387 n = list_find_nr(l, i++, NULL); /* fnum */
18388 if (n < 0)
18389 return FAIL;
18390 if (n == 0)
18391 n = curbuf->b_fnum; /* current buffer */
18392 *fnump = n;
18393 }
18394
18395 n = list_find_nr(l, i++, NULL); /* lnum */
18396 if (n < 0)
18397 return FAIL;
18398 posp->lnum = n;
18399
18400 n = list_find_nr(l, i++, NULL); /* col */
18401 if (n < 0)
18402 return FAIL;
18403 posp->col = n;
18404
18405#ifdef FEAT_VIRTUALEDIT
18406 n = list_find_nr(l, i, NULL);
18407 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018408 posp->coladd = 0;
18409 else
18410 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018411#endif
18412
18413 return OK;
18414}
18415
18416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 * Get the length of an environment variable name.
18418 * Advance "arg" to the first character after the name.
18419 * Return 0 for error.
18420 */
18421 static int
18422get_env_len(arg)
18423 char_u **arg;
18424{
18425 char_u *p;
18426 int len;
18427
18428 for (p = *arg; vim_isIDc(*p); ++p)
18429 ;
18430 if (p == *arg) /* no name found */
18431 return 0;
18432
18433 len = (int)(p - *arg);
18434 *arg = p;
18435 return len;
18436}
18437
18438/*
18439 * Get the length of the name of a function or internal variable.
18440 * "arg" is advanced to the first non-white character after the name.
18441 * Return 0 if something is wrong.
18442 */
18443 static int
18444get_id_len(arg)
18445 char_u **arg;
18446{
18447 char_u *p;
18448 int len;
18449
18450 /* Find the end of the name. */
18451 for (p = *arg; eval_isnamec(*p); ++p)
18452 ;
18453 if (p == *arg) /* no name found */
18454 return 0;
18455
18456 len = (int)(p - *arg);
18457 *arg = skipwhite(p);
18458
18459 return len;
18460}
18461
18462/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018463 * Get the length of the name of a variable or function.
18464 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018466 * Return -1 if curly braces expansion failed.
18467 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 * If the name contains 'magic' {}'s, expand them and return the
18469 * expanded name in an allocated string via 'alias' - caller must free.
18470 */
18471 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018472get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 char_u **arg;
18474 char_u **alias;
18475 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018476 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477{
18478 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 char_u *p;
18480 char_u *expr_start;
18481 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482
18483 *alias = NULL; /* default to no alias */
18484
18485 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18486 && (*arg)[2] == (int)KE_SNR)
18487 {
18488 /* hard coded <SNR>, already translated */
18489 *arg += 3;
18490 return get_id_len(arg) + 3;
18491 }
18492 len = eval_fname_script(*arg);
18493 if (len > 0)
18494 {
18495 /* literal "<SID>", "s:" or "<SNR>" */
18496 *arg += len;
18497 }
18498
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018500 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018502 p = find_name_end(*arg, &expr_start, &expr_end,
18503 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 if (expr_start != NULL)
18505 {
18506 char_u *temp_string;
18507
18508 if (!evaluate)
18509 {
18510 len += (int)(p - *arg);
18511 *arg = skipwhite(p);
18512 return len;
18513 }
18514
18515 /*
18516 * Include any <SID> etc in the expanded string:
18517 * Thus the -len here.
18518 */
18519 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18520 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018521 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 *alias = temp_string;
18523 *arg = skipwhite(p);
18524 return (int)STRLEN(temp_string);
18525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526
18527 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018528 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 EMSG2(_(e_invexpr2), *arg);
18530
18531 return len;
18532}
18533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534/*
18535 * Find the end of a variable or function name, taking care of magic braces.
18536 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18537 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018538 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018539 * Return a pointer to just after the name. Equal to "arg" if there is no
18540 * valid name.
18541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018543find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 char_u *arg;
18545 char_u **expr_start;
18546 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018547 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018549 int mb_nest = 0;
18550 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 char_u *p;
18552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018555 *expr_start = NULL;
18556 *expr_end = NULL;
18557 }
18558
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018559 /* Quick check for valid starting character. */
18560 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18561 return arg;
18562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 for (p = arg; *p != NUL
18564 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018565 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018566 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018568 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018570 if (*p == '\'')
18571 {
18572 /* skip over 'string' to avoid counting [ and ] inside it. */
18573 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18574 ;
18575 if (*p == NUL)
18576 break;
18577 }
18578 else if (*p == '"')
18579 {
18580 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18581 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18582 if (*p == '\\' && p[1] != NUL)
18583 ++p;
18584 if (*p == NUL)
18585 break;
18586 }
18587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590 if (*p == '[')
18591 ++br_nest;
18592 else if (*p == ']')
18593 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018596 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598 if (*p == '{')
18599 {
18600 mb_nest++;
18601 if (expr_start != NULL && *expr_start == NULL)
18602 *expr_start = p;
18603 }
18604 else if (*p == '}')
18605 {
18606 mb_nest--;
18607 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18608 *expr_end = p;
18609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 }
18612
18613 return p;
18614}
18615
18616/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018617 * Expands out the 'magic' {}'s in a variable/function name.
18618 * Note that this can call itself recursively, to deal with
18619 * constructs like foo{bar}{baz}{bam}
18620 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18621 * "in_start" ^
18622 * "expr_start" ^
18623 * "expr_end" ^
18624 * "in_end" ^
18625 *
18626 * Returns a new allocated string, which the caller must free.
18627 * Returns NULL for failure.
18628 */
18629 static char_u *
18630make_expanded_name(in_start, expr_start, expr_end, in_end)
18631 char_u *in_start;
18632 char_u *expr_start;
18633 char_u *expr_end;
18634 char_u *in_end;
18635{
18636 char_u c1;
18637 char_u *retval = NULL;
18638 char_u *temp_result;
18639 char_u *nextcmd = NULL;
18640
18641 if (expr_end == NULL || in_end == NULL)
18642 return NULL;
18643 *expr_start = NUL;
18644 *expr_end = NUL;
18645 c1 = *in_end;
18646 *in_end = NUL;
18647
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018648 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018649 if (temp_result != NULL && nextcmd == NULL)
18650 {
18651 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18652 + (in_end - expr_end) + 1));
18653 if (retval != NULL)
18654 {
18655 STRCPY(retval, in_start);
18656 STRCAT(retval, temp_result);
18657 STRCAT(retval, expr_end + 1);
18658 }
18659 }
18660 vim_free(temp_result);
18661
18662 *in_end = c1; /* put char back for error messages */
18663 *expr_start = '{';
18664 *expr_end = '}';
18665
18666 if (retval != NULL)
18667 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018668 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018669 if (expr_start != NULL)
18670 {
18671 /* Further expansion! */
18672 temp_result = make_expanded_name(retval, expr_start,
18673 expr_end, temp_result);
18674 vim_free(retval);
18675 retval = temp_result;
18676 }
18677 }
18678
18679 return retval;
18680}
18681
18682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018684 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 */
18686 static int
18687eval_isnamec(c)
18688 int c;
18689{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018690 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18691}
18692
18693/*
18694 * Return TRUE if character "c" can be used as the first character in a
18695 * variable or function name (excluding '{' and '}').
18696 */
18697 static int
18698eval_isnamec1(c)
18699 int c;
18700{
18701 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702}
18703
18704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 * Set number v: variable to "val".
18706 */
18707 void
18708set_vim_var_nr(idx, val)
18709 int idx;
18710 long val;
18711{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018712 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713}
18714
18715/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018716 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 */
18718 long
18719get_vim_var_nr(idx)
18720 int idx;
18721{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018722 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723}
18724
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018725/*
18726 * Get string v: variable value. Uses a static buffer, can only be used once.
18727 */
18728 char_u *
18729get_vim_var_str(idx)
18730 int idx;
18731{
18732 return get_tv_string(&vimvars[idx].vv_tv);
18733}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018734
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018736 * Get List v: variable value. Caller must take care of reference count when
18737 * needed.
18738 */
18739 list_T *
18740get_vim_var_list(idx)
18741 int idx;
18742{
18743 return vimvars[idx].vv_list;
18744}
18745
18746/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018747 * Set v:char to character "c".
18748 */
18749 void
18750set_vim_var_char(c)
18751 int c;
18752{
18753#ifdef FEAT_MBYTE
18754 char_u buf[MB_MAXBYTES];
18755#else
18756 char_u buf[2];
18757#endif
18758
18759#ifdef FEAT_MBYTE
18760 if (has_mbyte)
18761 buf[(*mb_char2bytes)(c, buf)] = NUL;
18762 else
18763#endif
18764 {
18765 buf[0] = c;
18766 buf[1] = NUL;
18767 }
18768 set_vim_var_string(VV_CHAR, buf, -1);
18769}
18770
18771/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018772 * Set v:count to "count" and v:count1 to "count1".
18773 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 */
18775 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018776set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 long count;
18778 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018779 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018781 if (set_prevcount)
18782 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018783 vimvars[VV_COUNT].vv_nr = count;
18784 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785}
18786
18787/*
18788 * Set string v: variable to a copy of "val".
18789 */
18790 void
18791set_vim_var_string(idx, val, len)
18792 int idx;
18793 char_u *val;
18794 int len; /* length of "val" to use or -1 (whole string) */
18795{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018796 /* Need to do this (at least) once, since we can't initialize a union.
18797 * Will always be invoked when "v:progname" is set. */
18798 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18799
Bram Moolenaare9a41262005-01-15 22:18:47 +000018800 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018802 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018804 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018806 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807}
18808
18809/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018810 * Set List v: variable to "val".
18811 */
18812 void
18813set_vim_var_list(idx, val)
18814 int idx;
18815 list_T *val;
18816{
18817 list_unref(vimvars[idx].vv_list);
18818 vimvars[idx].vv_list = val;
18819 if (val != NULL)
18820 ++val->lv_refcount;
18821}
18822
18823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 * Set v:register if needed.
18825 */
18826 void
18827set_reg_var(c)
18828 int c;
18829{
18830 char_u regname;
18831
18832 if (c == 0 || c == ' ')
18833 regname = '"';
18834 else
18835 regname = c;
18836 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018837 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 set_vim_var_string(VV_REG, &regname, 1);
18839}
18840
18841/*
18842 * Get or set v:exception. If "oldval" == NULL, return the current value.
18843 * Otherwise, restore the value to "oldval" and return NULL.
18844 * Must always be called in pairs to save and restore v:exception! Does not
18845 * take care of memory allocations.
18846 */
18847 char_u *
18848v_exception(oldval)
18849 char_u *oldval;
18850{
18851 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018852 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853
Bram Moolenaare9a41262005-01-15 22:18:47 +000018854 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 return NULL;
18856}
18857
18858/*
18859 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18860 * Otherwise, restore the value to "oldval" and return NULL.
18861 * Must always be called in pairs to save and restore v:throwpoint! Does not
18862 * take care of memory allocations.
18863 */
18864 char_u *
18865v_throwpoint(oldval)
18866 char_u *oldval;
18867{
18868 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018869 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
Bram Moolenaare9a41262005-01-15 22:18:47 +000018871 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 return NULL;
18873}
18874
18875#if defined(FEAT_AUTOCMD) || defined(PROTO)
18876/*
18877 * Set v:cmdarg.
18878 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18879 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18880 * Must always be called in pairs!
18881 */
18882 char_u *
18883set_cmdarg(eap, oldarg)
18884 exarg_T *eap;
18885 char_u *oldarg;
18886{
18887 char_u *oldval;
18888 char_u *newval;
18889 unsigned len;
18890
Bram Moolenaare9a41262005-01-15 22:18:47 +000018891 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018892 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018894 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018895 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018896 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 }
18898
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018899 if (eap->force_bin == FORCE_BIN)
18900 len = 6;
18901 else if (eap->force_bin == FORCE_NOBIN)
18902 len = 8;
18903 else
18904 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018905
18906 if (eap->read_edit)
18907 len += 7;
18908
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018909 if (eap->force_ff != 0)
18910 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18911# ifdef FEAT_MBYTE
18912 if (eap->force_enc != 0)
18913 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018914 if (eap->bad_char != 0)
18915 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018916# endif
18917
18918 newval = alloc(len + 1);
18919 if (newval == NULL)
18920 return NULL;
18921
18922 if (eap->force_bin == FORCE_BIN)
18923 sprintf((char *)newval, " ++bin");
18924 else if (eap->force_bin == FORCE_NOBIN)
18925 sprintf((char *)newval, " ++nobin");
18926 else
18927 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018928
18929 if (eap->read_edit)
18930 STRCAT(newval, " ++edit");
18931
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018932 if (eap->force_ff != 0)
18933 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18934 eap->cmd + eap->force_ff);
18935# ifdef FEAT_MBYTE
18936 if (eap->force_enc != 0)
18937 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18938 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018939 if (eap->bad_char == BAD_KEEP)
18940 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18941 else if (eap->bad_char == BAD_DROP)
18942 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18943 else if (eap->bad_char != 0)
18944 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018945# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018946 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018947 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948}
18949#endif
18950
18951/*
18952 * Get the value of internal variable "name".
18953 * Return OK or FAIL.
18954 */
18955 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018956get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 char_u *name;
18958 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018959 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018960 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961{
18962 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018963 typval_T *tv = NULL;
18964 typval_T atv;
18965 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
18968 /* truncate the name, so that we can use strcmp() */
18969 cc = name[len];
18970 name[len] = NUL;
18971
18972 /*
18973 * Check for "b:changedtick".
18974 */
18975 if (STRCMP(name, "b:changedtick") == 0)
18976 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018977 atv.v_type = VAR_NUMBER;
18978 atv.vval.v_number = curbuf->b_changedtick;
18979 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 }
18981
18982 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 * Check for user-defined variables.
18984 */
18985 else
18986 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018987 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 }
18991
Bram Moolenaare9a41262005-01-15 22:18:47 +000018992 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018994 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018995 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 ret = FAIL;
18997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018998 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018999 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000
19001 name[len] = cc;
19002
19003 return ret;
19004}
19005
19006/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019007 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19008 * Also handle function call with Funcref variable: func(expr)
19009 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19010 */
19011 static int
19012handle_subscript(arg, rettv, evaluate, verbose)
19013 char_u **arg;
19014 typval_T *rettv;
19015 int evaluate; /* do more than finding the end */
19016 int verbose; /* give error messages */
19017{
19018 int ret = OK;
19019 dict_T *selfdict = NULL;
19020 char_u *s;
19021 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019022 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019023
19024 while (ret == OK
19025 && (**arg == '['
19026 || (**arg == '.' && rettv->v_type == VAR_DICT)
19027 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19028 && !vim_iswhite(*(*arg - 1)))
19029 {
19030 if (**arg == '(')
19031 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019032 /* need to copy the funcref so that we can clear rettv */
19033 functv = *rettv;
19034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019035
19036 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019037 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019038 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019039 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19040 &len, evaluate, selfdict);
19041
19042 /* Clear the funcref afterwards, so that deleting it while
19043 * evaluating the arguments is possible (see test55). */
19044 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019045
19046 /* Stop the expression evaluation when immediately aborting on
19047 * error, or when an interrupt occurred or an exception was thrown
19048 * but not caught. */
19049 if (aborting())
19050 {
19051 if (ret == OK)
19052 clear_tv(rettv);
19053 ret = FAIL;
19054 }
19055 dict_unref(selfdict);
19056 selfdict = NULL;
19057 }
19058 else /* **arg == '[' || **arg == '.' */
19059 {
19060 dict_unref(selfdict);
19061 if (rettv->v_type == VAR_DICT)
19062 {
19063 selfdict = rettv->vval.v_dict;
19064 if (selfdict != NULL)
19065 ++selfdict->dv_refcount;
19066 }
19067 else
19068 selfdict = NULL;
19069 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19070 {
19071 clear_tv(rettv);
19072 ret = FAIL;
19073 }
19074 }
19075 }
19076 dict_unref(selfdict);
19077 return ret;
19078}
19079
19080/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019081 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 * value).
19083 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019084 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019085alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019086{
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019088}
19089
19090/*
19091 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 * The string "s" must have been allocated, it is consumed.
19093 * Return NULL for out of memory, the variable otherwise.
19094 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019095 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 char_u *s;
19098{
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 rettv = alloc_tv();
19102 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104 rettv->v_type = VAR_STRING;
19105 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 }
19107 else
19108 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019109 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110}
19111
19112/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019113 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019115 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019116free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118{
19119 if (varp != NULL)
19120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019121 switch (varp->v_type)
19122 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019123 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019124 func_unref(varp->vval.v_string);
19125 /*FALLTHROUGH*/
19126 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019127 vim_free(varp->vval.v_string);
19128 break;
19129 case VAR_LIST:
19130 list_unref(varp->vval.v_list);
19131 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019132 case VAR_DICT:
19133 dict_unref(varp->vval.v_dict);
19134 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019135 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019136#ifdef FEAT_FLOAT
19137 case VAR_FLOAT:
19138#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019139 case VAR_UNKNOWN:
19140 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019142 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143 break;
19144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 vim_free(varp);
19146 }
19147}
19148
19149/*
19150 * Free the memory for a variable value and set the value to NULL or 0.
19151 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019152 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019154 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155{
19156 if (varp != NULL)
19157 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019158 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019160 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019161 func_unref(varp->vval.v_string);
19162 /*FALLTHROUGH*/
19163 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019164 vim_free(varp->vval.v_string);
19165 varp->vval.v_string = NULL;
19166 break;
19167 case VAR_LIST:
19168 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019169 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019170 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019171 case VAR_DICT:
19172 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019173 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019174 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 varp->vval.v_number = 0;
19177 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019178#ifdef FEAT_FLOAT
19179 case VAR_FLOAT:
19180 varp->vval.v_float = 0.0;
19181 break;
19182#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 case VAR_UNKNOWN:
19184 break;
19185 default:
19186 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019188 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 }
19190}
19191
19192/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019193 * Set the value of a variable to NULL without freeing items.
19194 */
19195 static void
19196init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198{
19199 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019200 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201}
19202
19203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 * Get the number value of a variable.
19205 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019206 * For incompatible types, return 0.
19207 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19208 * caller of incompatible types: it sets *denote to TRUE if "denote"
19209 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 */
19211 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019212get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019215 int error = FALSE;
19216
19217 return get_tv_number_chk(varp, &error); /* return 0L on error */
19218}
19219
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019220 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019221get_tv_number_chk(varp, denote)
19222 typval_T *varp;
19223 int *denote;
19224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019225 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019227 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019229 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019230 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019231#ifdef FEAT_FLOAT
19232 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019233 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019234 break;
19235#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019236 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019237 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019238 break;
19239 case VAR_STRING:
19240 if (varp->vval.v_string != NULL)
19241 vim_str2nr(varp->vval.v_string, NULL, NULL,
19242 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019243 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019244 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019245 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019246 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019247 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019248 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019249 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019250 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019251 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019252 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019254 if (denote == NULL) /* useful for values that must be unsigned */
19255 n = -1;
19256 else
19257 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019258 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259}
19260
19261/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019262 * Get the lnum from the first argument.
19263 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019264 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 */
19266 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019268 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269{
Bram Moolenaar33570922005-01-25 22:26:29 +000019270 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 linenr_T lnum;
19272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 if (lnum == 0) /* no valid number, try using line() */
19275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019276 rettv.v_type = VAR_NUMBER;
19277 f_line(argvars, &rettv);
19278 lnum = rettv.vval.v_number;
19279 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 }
19281 return lnum;
19282}
19283
19284/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019285 * Get the lnum from the first argument.
19286 * Also accepts "$", then "buf" is used.
19287 * Returns 0 on error.
19288 */
19289 static linenr_T
19290get_tv_lnum_buf(argvars, buf)
19291 typval_T *argvars;
19292 buf_T *buf;
19293{
19294 if (argvars[0].v_type == VAR_STRING
19295 && argvars[0].vval.v_string != NULL
19296 && argvars[0].vval.v_string[0] == '$'
19297 && buf != NULL)
19298 return buf->b_ml.ml_line_count;
19299 return get_tv_number_chk(&argvars[0], NULL);
19300}
19301
19302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 * Get the string value of a variable.
19304 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019305 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19306 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 * If the String variable has never been set, return an empty string.
19308 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019309 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19310 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 */
19312 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019313get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019314 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315{
19316 static char_u mybuf[NUMBUFLEN];
19317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319}
19320
19321 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019323 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019326 char_u *res = get_tv_string_buf_chk(varp, buf);
19327
19328 return res != NULL ? res : (char_u *)"";
19329}
19330
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019331 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019332get_tv_string_chk(varp)
19333 typval_T *varp;
19334{
19335 static char_u mybuf[NUMBUFLEN];
19336
19337 return get_tv_string_buf_chk(varp, mybuf);
19338}
19339
19340 static char_u *
19341get_tv_string_buf_chk(varp, buf)
19342 typval_T *varp;
19343 char_u *buf;
19344{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019345 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019347 case VAR_NUMBER:
19348 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19349 return buf;
19350 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019351 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 break;
19353 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019354 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019355 break;
19356 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019357 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019358 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019359#ifdef FEAT_FLOAT
19360 case VAR_FLOAT:
19361 EMSG(_("E806: using Float as a String"));
19362 break;
19363#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019364 case VAR_STRING:
19365 if (varp->vval.v_string != NULL)
19366 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019367 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019368 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019369 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019372 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373}
19374
19375/*
19376 * Find variable "name" in the list of variables.
19377 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019378 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019379 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019380 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019383find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019385 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389
Bram Moolenaara7043832005-01-21 11:56:39 +000019390 ht = find_var_ht(name, &varname);
19391 if (htp != NULL)
19392 *htp = ht;
19393 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019395 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396}
19397
19398/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019399 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019400 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019403find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019405 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019406 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019407{
Bram Moolenaar33570922005-01-25 22:26:29 +000019408 hashitem_T *hi;
19409
19410 if (*varname == NUL)
19411 {
19412 /* Must be something like "s:", otherwise "ht" would be NULL. */
19413 switch (varname[-2])
19414 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019415 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019416 case 'g': return &globvars_var;
19417 case 'v': return &vimvars_var;
19418 case 'b': return &curbuf->b_bufvar;
19419 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019420#ifdef FEAT_WINDOWS
19421 case 't': return &curtab->tp_winvar;
19422#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019423 case 'l': return current_funccal == NULL
19424 ? NULL : &current_funccal->l_vars_var;
19425 case 'a': return current_funccal == NULL
19426 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019427 }
19428 return NULL;
19429 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019430
19431 hi = hash_find(ht, varname);
19432 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019433 {
19434 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019435 * worked find the variable again. Don't auto-load a script if it was
19436 * loaded already, otherwise it would be loaded every time when
19437 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019438 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019439 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019440 hi = hash_find(ht, varname);
19441 if (HASHITEM_EMPTY(hi))
19442 return NULL;
19443 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019444 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019445}
19446
19447/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019448 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019449 * Set "varname" to the start of name without ':'.
19450 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019451 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019452find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 char_u *name;
19454 char_u **varname;
19455{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019456 hashitem_T *hi;
19457
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 if (name[1] != ':')
19459 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019460 /* The name must not start with a colon or #. */
19461 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 return NULL;
19463 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019464
19465 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019466 hi = hash_find(&compat_hashtab, name);
19467 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019468 return &compat_hashtab;
19469
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019471 return &globvarht; /* global variable */
19472 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 }
19474 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019475 if (*name == 'g') /* global variable */
19476 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019477 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19478 */
19479 if (vim_strchr(name + 2, ':') != NULL
19480 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019481 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019483 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019486#ifdef FEAT_WINDOWS
19487 if (*name == 't') /* tab page variable */
19488 return &curtab->tp_vars.dv_hashtab;
19489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 if (*name == 'v') /* v: variable */
19491 return &vimvarht;
19492 if (*name == 'a' && current_funccal != NULL) /* function argument */
19493 return &current_funccal->l_avars.dv_hashtab;
19494 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19495 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 if (*name == 's' /* script variable */
19497 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19498 return &SCRIPT_VARS(current_SID);
19499 return NULL;
19500}
19501
19502/*
19503 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019504 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 * Returns NULL when it doesn't exist.
19506 */
19507 char_u *
19508get_var_value(name)
19509 char_u *name;
19510{
Bram Moolenaar33570922005-01-25 22:26:29 +000019511 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512
Bram Moolenaara7043832005-01-21 11:56:39 +000019513 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 if (v == NULL)
19515 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019516 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517}
19518
19519/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 * sourcing this script and when executing functions defined in the script.
19522 */
19523 void
19524new_script_vars(id)
19525 scid_T id;
19526{
Bram Moolenaara7043832005-01-21 11:56:39 +000019527 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019528 hashtab_T *ht;
19529 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019530
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19532 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019533 /* Re-allocating ga_data means that an ht_array pointing to
19534 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019535 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019536 for (i = 1; i <= ga_scripts.ga_len; ++i)
19537 {
19538 ht = &SCRIPT_VARS(i);
19539 if (ht->ht_mask == HT_INIT_SIZE - 1)
19540 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019541 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019542 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019543 }
19544
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 while (ga_scripts.ga_len < id)
19546 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019547 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019548 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 }
19552 }
19553}
19554
19555/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019556 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19557 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 */
19559 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019560init_var_dict(dict, dict_var)
19561 dict_T *dict;
19562 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019565 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019566 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 dict_var->di_tv.vval.v_dict = dict;
19568 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019569 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019570 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19571 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572}
19573
19574/*
19575 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019576 * Frees all allocated variables and the value they contain.
19577 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 */
19579 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019580vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 hashtab_T *ht;
19582{
19583 vars_clear_ext(ht, TRUE);
19584}
19585
19586/*
19587 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19588 */
19589 static void
19590vars_clear_ext(ht, free_val)
19591 hashtab_T *ht;
19592 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593{
Bram Moolenaara7043832005-01-21 11:56:39 +000019594 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 hashitem_T *hi;
19596 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597
Bram Moolenaar33570922005-01-25 22:26:29 +000019598 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019599 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019600 for (hi = ht->ht_array; todo > 0; ++hi)
19601 {
19602 if (!HASHITEM_EMPTY(hi))
19603 {
19604 --todo;
19605
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019607 * ht_array might change then. hash_clear() takes care of it
19608 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019609 v = HI2DI(hi);
19610 if (free_val)
19611 clear_tv(&v->di_tv);
19612 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19613 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019614 }
19615 }
19616 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019617 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618}
19619
Bram Moolenaara7043832005-01-21 11:56:39 +000019620/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 * Delete a variable from hashtab "ht" at item "hi".
19622 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019625delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 hashtab_T *ht;
19627 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628{
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019630
19631 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019632 clear_tv(&di->di_tv);
19633 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634}
19635
19636/*
19637 * List the value of one internal variable.
19638 */
19639 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019640list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019643 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019645 char_u *tofree;
19646 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019647 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019648
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019649 current_copyID += COPYID_INC;
19650 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019651 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019652 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019653 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654}
19655
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019657list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 char_u *prefix;
19659 char_u *name;
19660 int type;
19661 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019662 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663{
Bram Moolenaar31859182007-08-14 20:41:13 +000019664 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19665 msg_start();
19666 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 if (name != NULL) /* "a:" vars don't have a name stored */
19668 msg_puts(name);
19669 msg_putchar(' ');
19670 msg_advance(22);
19671 if (type == VAR_NUMBER)
19672 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019673 else if (type == VAR_FUNC)
19674 msg_putchar('*');
19675 else if (type == VAR_LIST)
19676 {
19677 msg_putchar('[');
19678 if (*string == '[')
19679 ++string;
19680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019681 else if (type == VAR_DICT)
19682 {
19683 msg_putchar('{');
19684 if (*string == '{')
19685 ++string;
19686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 else
19688 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019689
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019691
19692 if (type == VAR_FUNC)
19693 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019694 if (*first)
19695 {
19696 msg_clr_eos();
19697 *first = FALSE;
19698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699}
19700
19701/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 * If the variable already exists, the value is updated.
19704 * Otherwise the variable is created.
19705 */
19706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019709 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019710 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711{
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019714 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019715 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019717 ht = find_var_ht(name, &varname);
19718 if (ht == NULL || *varname == NUL)
19719 {
19720 EMSG2(_(e_illvar), name);
19721 return;
19722 }
19723 v = find_var_in_ht(ht, varname, TRUE);
19724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019726 {
19727 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19728 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19729 ? name[2] : name[0]))
19730 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019731 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019732 return;
19733 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019734 /* Don't allow hiding a function. When "v" is not NULL we migth be
19735 * assigning another function to the same var, the type is checked
19736 * below. */
19737 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019738 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019739 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019740 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019741 return;
19742 }
19743 }
19744
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019748 if (var_check_ro(v->di_flags, name)
19749 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 return;
19751 if (v->di_tv.v_type != tv->v_type
19752 && !((v->di_tv.v_type == VAR_STRING
19753 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019755 || tv->v_type == VAR_NUMBER))
19756#ifdef FEAT_FLOAT
19757 && !((v->di_tv.v_type == VAR_NUMBER
19758 || v->di_tv.v_type == VAR_FLOAT)
19759 && (tv->v_type == VAR_NUMBER
19760 || tv->v_type == VAR_FLOAT))
19761#endif
19762 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019763 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019764 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019765 return;
19766 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019767
19768 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019769 * Handle setting internal v: variables separately: we don't change
19770 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019771 */
19772 if (ht == &vimvarht)
19773 {
19774 if (v->di_tv.v_type == VAR_STRING)
19775 {
19776 vim_free(v->di_tv.vval.v_string);
19777 if (copy || tv->v_type != VAR_STRING)
19778 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19779 else
19780 {
19781 /* Take over the string to avoid an extra alloc/free. */
19782 v->di_tv.vval.v_string = tv->vval.v_string;
19783 tv->vval.v_string = NULL;
19784 }
19785 }
19786 else if (v->di_tv.v_type != VAR_NUMBER)
19787 EMSG2(_(e_intern2), "set_var()");
19788 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019789 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019790 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019791 if (STRCMP(varname, "searchforward") == 0)
19792 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19793 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 return;
19795 }
19796
19797 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 }
19799 else /* add a new variable */
19800 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019801 /* Can't add "v:" variable. */
19802 if (ht == &vimvarht)
19803 {
19804 EMSG2(_(e_illvar), name);
19805 return;
19806 }
19807
Bram Moolenaar92124a32005-06-17 22:03:40 +000019808 /* Make sure the variable name is valid. */
19809 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019810 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19811 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019812 {
19813 EMSG2(_(e_illvar), varname);
19814 return;
19815 }
19816
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019817 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19818 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019819 if (v == NULL)
19820 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019824 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 return;
19826 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019827 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019829
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019830 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019831 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019832 else
19833 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019834 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019835 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838}
19839
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019840/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019841 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 * Also give an error message.
19843 */
19844 static int
19845var_check_ro(flags, name)
19846 int flags;
19847 char_u *name;
19848{
19849 if (flags & DI_FLAGS_RO)
19850 {
19851 EMSG2(_(e_readonlyvar), name);
19852 return TRUE;
19853 }
19854 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19855 {
19856 EMSG2(_(e_readonlysbx), name);
19857 return TRUE;
19858 }
19859 return FALSE;
19860}
19861
19862/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019863 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19864 * Also give an error message.
19865 */
19866 static int
19867var_check_fixed(flags, name)
19868 int flags;
19869 char_u *name;
19870{
19871 if (flags & DI_FLAGS_FIX)
19872 {
19873 EMSG2(_("E795: Cannot delete variable %s"), name);
19874 return TRUE;
19875 }
19876 return FALSE;
19877}
19878
19879/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019880 * Return TRUE if typeval "tv" is set to be locked (immutable).
19881 * Also give an error message, using "name".
19882 */
19883 static int
19884tv_check_lock(lock, name)
19885 int lock;
19886 char_u *name;
19887{
19888 if (lock & VAR_LOCKED)
19889 {
19890 EMSG2(_("E741: Value is locked: %s"),
19891 name == NULL ? (char_u *)_("Unknown") : name);
19892 return TRUE;
19893 }
19894 if (lock & VAR_FIXED)
19895 {
19896 EMSG2(_("E742: Cannot change value of %s"),
19897 name == NULL ? (char_u *)_("Unknown") : name);
19898 return TRUE;
19899 }
19900 return FALSE;
19901}
19902
19903/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019905 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019906 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019907 * It is OK for "from" and "to" to point to the same item. This is used to
19908 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019910 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019911copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019912 typval_T *from;
19913 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019915 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019916 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019917 switch (from->v_type)
19918 {
19919 case VAR_NUMBER:
19920 to->vval.v_number = from->vval.v_number;
19921 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019922#ifdef FEAT_FLOAT
19923 case VAR_FLOAT:
19924 to->vval.v_float = from->vval.v_float;
19925 break;
19926#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927 case VAR_STRING:
19928 case VAR_FUNC:
19929 if (from->vval.v_string == NULL)
19930 to->vval.v_string = NULL;
19931 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019932 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019933 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019934 if (from->v_type == VAR_FUNC)
19935 func_ref(to->vval.v_string);
19936 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019937 break;
19938 case VAR_LIST:
19939 if (from->vval.v_list == NULL)
19940 to->vval.v_list = NULL;
19941 else
19942 {
19943 to->vval.v_list = from->vval.v_list;
19944 ++to->vval.v_list->lv_refcount;
19945 }
19946 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019947 case VAR_DICT:
19948 if (from->vval.v_dict == NULL)
19949 to->vval.v_dict = NULL;
19950 else
19951 {
19952 to->vval.v_dict = from->vval.v_dict;
19953 ++to->vval.v_dict->dv_refcount;
19954 }
19955 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019957 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019958 break;
19959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960}
19961
19962/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019963 * Make a copy of an item.
19964 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019965 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19966 * reference to an already copied list/dict can be used.
19967 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019968 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019969 static int
19970item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 typval_T *from;
19972 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019973 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019974 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019975{
19976 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019977 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019978
Bram Moolenaar33570922005-01-25 22:26:29 +000019979 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019980 {
19981 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019982 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019983 }
19984 ++recurse;
19985
19986 switch (from->v_type)
19987 {
19988 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019989#ifdef FEAT_FLOAT
19990 case VAR_FLOAT:
19991#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019992 case VAR_STRING:
19993 case VAR_FUNC:
19994 copy_tv(from, to);
19995 break;
19996 case VAR_LIST:
19997 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019998 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019999 if (from->vval.v_list == NULL)
20000 to->vval.v_list = NULL;
20001 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20002 {
20003 /* use the copy made earlier */
20004 to->vval.v_list = from->vval.v_list->lv_copylist;
20005 ++to->vval.v_list->lv_refcount;
20006 }
20007 else
20008 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20009 if (to->vval.v_list == NULL)
20010 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020011 break;
20012 case VAR_DICT:
20013 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020014 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020015 if (from->vval.v_dict == NULL)
20016 to->vval.v_dict = NULL;
20017 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20018 {
20019 /* use the copy made earlier */
20020 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20021 ++to->vval.v_dict->dv_refcount;
20022 }
20023 else
20024 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20025 if (to->vval.v_dict == NULL)
20026 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020027 break;
20028 default:
20029 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020030 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020031 }
20032 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020033 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020034}
20035
20036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 * ":echo expr1 ..." print each argument separated with a space, add a
20038 * newline at the end.
20039 * ":echon expr1 ..." print each argument plain.
20040 */
20041 void
20042ex_echo(eap)
20043 exarg_T *eap;
20044{
20045 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020046 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020047 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 char_u *p;
20049 int needclr = TRUE;
20050 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052
20053 if (eap->skip)
20054 ++emsg_skip;
20055 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20056 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020057 /* If eval1() causes an error message the text from the command may
20058 * still need to be cleared. E.g., "echo 22,44". */
20059 need_clr_eos = needclr;
20060
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 {
20064 /*
20065 * Report the invalid expression unless the expression evaluation
20066 * has been cancelled due to an aborting error, an interrupt, or an
20067 * exception.
20068 */
20069 if (!aborting())
20070 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020071 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 break;
20073 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020074 need_clr_eos = FALSE;
20075
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 if (!eap->skip)
20077 {
20078 if (atstart)
20079 {
20080 atstart = FALSE;
20081 /* Call msg_start() after eval1(), evaluating the expression
20082 * may cause a message to appear. */
20083 if (eap->cmdidx == CMD_echo)
20084 msg_start();
20085 }
20086 else if (eap->cmdidx == CMD_echo)
20087 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020088 current_copyID += COPYID_INC;
20089 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020090 if (p != NULL)
20091 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020093 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020095 if (*p != TAB && needclr)
20096 {
20097 /* remove any text still there from the command */
20098 msg_clr_eos();
20099 needclr = FALSE;
20100 }
20101 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 }
20103 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020104 {
20105#ifdef FEAT_MBYTE
20106 if (has_mbyte)
20107 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020108 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020109
20110 (void)msg_outtrans_len_attr(p, i, echo_attr);
20111 p += i - 1;
20112 }
20113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020115 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020118 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 arg = skipwhite(arg);
20122 }
20123 eap->nextcmd = check_nextcmd(arg);
20124
20125 if (eap->skip)
20126 --emsg_skip;
20127 else
20128 {
20129 /* remove text that may still be there from the command */
20130 if (needclr)
20131 msg_clr_eos();
20132 if (eap->cmdidx == CMD_echo)
20133 msg_end();
20134 }
20135}
20136
20137/*
20138 * ":echohl {name}".
20139 */
20140 void
20141ex_echohl(eap)
20142 exarg_T *eap;
20143{
20144 int id;
20145
20146 id = syn_name2id(eap->arg);
20147 if (id == 0)
20148 echo_attr = 0;
20149 else
20150 echo_attr = syn_id2attr(id);
20151}
20152
20153/*
20154 * ":execute expr1 ..." execute the result of an expression.
20155 * ":echomsg expr1 ..." Print a message
20156 * ":echoerr expr1 ..." Print an error
20157 * Each gets spaces around each argument and a newline at the end for
20158 * echo commands
20159 */
20160 void
20161ex_execute(eap)
20162 exarg_T *eap;
20163{
20164 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020165 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 int ret = OK;
20167 char_u *p;
20168 garray_T ga;
20169 int len;
20170 int save_did_emsg;
20171
20172 ga_init2(&ga, 1, 80);
20173
20174 if (eap->skip)
20175 ++emsg_skip;
20176 while (*arg != NUL && *arg != '|' && *arg != '\n')
20177 {
20178 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 {
20181 /*
20182 * Report the invalid expression unless the expression evaluation
20183 * has been cancelled due to an aborting error, an interrupt, or an
20184 * exception.
20185 */
20186 if (!aborting())
20187 EMSG2(_(e_invexpr2), p);
20188 ret = FAIL;
20189 break;
20190 }
20191
20192 if (!eap->skip)
20193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 len = (int)STRLEN(p);
20196 if (ga_grow(&ga, len + 2) == FAIL)
20197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 ret = FAIL;
20200 break;
20201 }
20202 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 ga.ga_len += len;
20206 }
20207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020208 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 arg = skipwhite(arg);
20210 }
20211
20212 if (ret != FAIL && ga.ga_data != NULL)
20213 {
20214 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020217 out_flush();
20218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 else if (eap->cmdidx == CMD_echoerr)
20220 {
20221 /* We don't want to abort following commands, restore did_emsg. */
20222 save_did_emsg = did_emsg;
20223 EMSG((char_u *)ga.ga_data);
20224 if (!force_abort)
20225 did_emsg = save_did_emsg;
20226 }
20227 else if (eap->cmdidx == CMD_execute)
20228 do_cmdline((char_u *)ga.ga_data,
20229 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20230 }
20231
20232 ga_clear(&ga);
20233
20234 if (eap->skip)
20235 --emsg_skip;
20236
20237 eap->nextcmd = check_nextcmd(arg);
20238}
20239
20240/*
20241 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20242 * "arg" points to the "&" or '+' when called, to "option" when returning.
20243 * Returns NULL when no option name found. Otherwise pointer to the char
20244 * after the option name.
20245 */
20246 static char_u *
20247find_option_end(arg, opt_flags)
20248 char_u **arg;
20249 int *opt_flags;
20250{
20251 char_u *p = *arg;
20252
20253 ++p;
20254 if (*p == 'g' && p[1] == ':')
20255 {
20256 *opt_flags = OPT_GLOBAL;
20257 p += 2;
20258 }
20259 else if (*p == 'l' && p[1] == ':')
20260 {
20261 *opt_flags = OPT_LOCAL;
20262 p += 2;
20263 }
20264 else
20265 *opt_flags = 0;
20266
20267 if (!ASCII_ISALPHA(*p))
20268 return NULL;
20269 *arg = p;
20270
20271 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20272 p += 4; /* termcap option */
20273 else
20274 while (ASCII_ISALPHA(*p))
20275 ++p;
20276 return p;
20277}
20278
20279/*
20280 * ":function"
20281 */
20282 void
20283ex_function(eap)
20284 exarg_T *eap;
20285{
20286 char_u *theline;
20287 int j;
20288 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 char_u *name = NULL;
20291 char_u *p;
20292 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020293 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 garray_T newargs;
20295 garray_T newlines;
20296 int varargs = FALSE;
20297 int mustend = FALSE;
20298 int flags = 0;
20299 ufunc_T *fp;
20300 int indent;
20301 int nesting;
20302 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 dictitem_T *v;
20304 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020305 static int func_nr = 0; /* number for nameless function */
20306 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020307 hashtab_T *ht;
20308 int todo;
20309 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020310 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311
20312 /*
20313 * ":function" without argument: list functions.
20314 */
20315 if (ends_excmd(*eap->arg))
20316 {
20317 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020318 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020319 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020320 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020321 {
20322 if (!HASHITEM_EMPTY(hi))
20323 {
20324 --todo;
20325 fp = HI2UF(hi);
20326 if (!isdigit(*fp->uf_name))
20327 list_func_head(fp, FALSE);
20328 }
20329 }
20330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 eap->nextcmd = check_nextcmd(eap->arg);
20332 return;
20333 }
20334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020335 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020336 * ":function /pat": list functions matching pattern.
20337 */
20338 if (*eap->arg == '/')
20339 {
20340 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20341 if (!eap->skip)
20342 {
20343 regmatch_T regmatch;
20344
20345 c = *p;
20346 *p = NUL;
20347 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20348 *p = c;
20349 if (regmatch.regprog != NULL)
20350 {
20351 regmatch.rm_ic = p_ic;
20352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020353 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020354 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20355 {
20356 if (!HASHITEM_EMPTY(hi))
20357 {
20358 --todo;
20359 fp = HI2UF(hi);
20360 if (!isdigit(*fp->uf_name)
20361 && vim_regexec(&regmatch, fp->uf_name, 0))
20362 list_func_head(fp, FALSE);
20363 }
20364 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020365 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020366 }
20367 }
20368 if (*p == '/')
20369 ++p;
20370 eap->nextcmd = check_nextcmd(p);
20371 return;
20372 }
20373
20374 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020375 * Get the function name. There are these situations:
20376 * func normal function name
20377 * "name" == func, "fudi.fd_dict" == NULL
20378 * dict.func new dictionary entry
20379 * "name" == NULL, "fudi.fd_dict" set,
20380 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20381 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020382 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020383 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20384 * dict.func existing dict entry that's not a Funcref
20385 * "name" == NULL, "fudi.fd_dict" set,
20386 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020389 name = trans_function_name(&p, eap->skip, 0, &fudi);
20390 paren = (vim_strchr(p, '(') != NULL);
20391 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 {
20393 /*
20394 * Return on an invalid expression in braces, unless the expression
20395 * evaluation has been cancelled due to an aborting error, an
20396 * interrupt, or an exception.
20397 */
20398 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020400 if (!eap->skip && fudi.fd_newkey != NULL)
20401 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020402 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 else
20406 eap->skip = TRUE;
20407 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020408
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 /* An error in a function call during evaluation of an expression in magic
20410 * braces should not cause the function not to be defined. */
20411 saved_did_emsg = did_emsg;
20412 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413
20414 /*
20415 * ":function func" with only function name: list function.
20416 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020417 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 {
20419 if (!ends_excmd(*skipwhite(p)))
20420 {
20421 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020422 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 }
20424 eap->nextcmd = check_nextcmd(p);
20425 if (eap->nextcmd != NULL)
20426 *p = NUL;
20427 if (!eap->skip && !got_int)
20428 {
20429 fp = find_func(name);
20430 if (fp != NULL)
20431 {
20432 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020433 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020435 if (FUNCLINE(fp, j) == NULL)
20436 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 msg_putchar('\n');
20438 msg_outnum((long)(j + 1));
20439 if (j < 9)
20440 msg_putchar(' ');
20441 if (j < 99)
20442 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020443 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 out_flush(); /* show a line at a time */
20445 ui_breakcheck();
20446 }
20447 if (!got_int)
20448 {
20449 msg_putchar('\n');
20450 msg_puts((char_u *)" endfunction");
20451 }
20452 }
20453 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020454 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020456 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 }
20458
20459 /*
20460 * ":function name(arg1, arg2)" Define function.
20461 */
20462 p = skipwhite(p);
20463 if (*p != '(')
20464 {
20465 if (!eap->skip)
20466 {
20467 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020468 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 }
20470 /* attempt to continue by skipping some text */
20471 if (vim_strchr(p, '(') != NULL)
20472 p = vim_strchr(p, '(');
20473 }
20474 p = skipwhite(p + 1);
20475
20476 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20477 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20478
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020479 if (!eap->skip)
20480 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020481 /* Check the name of the function. Unless it's a dictionary function
20482 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020483 if (name != NULL)
20484 arg = name;
20485 else
20486 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020487 if (arg != NULL && (fudi.fd_di == NULL
20488 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020489 {
20490 if (*arg == K_SPECIAL)
20491 j = 3;
20492 else
20493 j = 0;
20494 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20495 : eval_isnamec(arg[j])))
20496 ++j;
20497 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020498 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020499 }
20500 }
20501
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 /*
20503 * Isolate the arguments: "arg1, arg2, ...)"
20504 */
20505 while (*p != ')')
20506 {
20507 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20508 {
20509 varargs = TRUE;
20510 p += 3;
20511 mustend = TRUE;
20512 }
20513 else
20514 {
20515 arg = p;
20516 while (ASCII_ISALNUM(*p) || *p == '_')
20517 ++p;
20518 if (arg == p || isdigit(*arg)
20519 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20520 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20521 {
20522 if (!eap->skip)
20523 EMSG2(_("E125: Illegal argument: %s"), arg);
20524 break;
20525 }
20526 if (ga_grow(&newargs, 1) == FAIL)
20527 goto erret;
20528 c = *p;
20529 *p = NUL;
20530 arg = vim_strsave(arg);
20531 if (arg == NULL)
20532 goto erret;
20533 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20534 *p = c;
20535 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 if (*p == ',')
20537 ++p;
20538 else
20539 mustend = TRUE;
20540 }
20541 p = skipwhite(p);
20542 if (mustend && *p != ')')
20543 {
20544 if (!eap->skip)
20545 EMSG2(_(e_invarg2), eap->arg);
20546 break;
20547 }
20548 }
20549 ++p; /* skip the ')' */
20550
Bram Moolenaare9a41262005-01-15 22:18:47 +000020551 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 for (;;)
20553 {
20554 p = skipwhite(p);
20555 if (STRNCMP(p, "range", 5) == 0)
20556 {
20557 flags |= FC_RANGE;
20558 p += 5;
20559 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020560 else if (STRNCMP(p, "dict", 4) == 0)
20561 {
20562 flags |= FC_DICT;
20563 p += 4;
20564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 else if (STRNCMP(p, "abort", 5) == 0)
20566 {
20567 flags |= FC_ABORT;
20568 p += 5;
20569 }
20570 else
20571 break;
20572 }
20573
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020574 /* When there is a line break use what follows for the function body.
20575 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20576 if (*p == '\n')
20577 line_arg = p + 1;
20578 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 EMSG(_(e_trailing));
20580
20581 /*
20582 * Read the body of the function, until ":endfunction" is found.
20583 */
20584 if (KeyTyped)
20585 {
20586 /* Check if the function already exists, don't let the user type the
20587 * whole function before telling him it doesn't work! For a script we
20588 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020589 if (!eap->skip && !eap->forceit)
20590 {
20591 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20592 EMSG(_(e_funcdict));
20593 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020594 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020597 if (!eap->skip && did_emsg)
20598 goto erret;
20599
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 msg_putchar('\n'); /* don't overwrite the function name */
20601 cmdline_row = msg_row;
20602 }
20603
20604 indent = 2;
20605 nesting = 0;
20606 for (;;)
20607 {
20608 msg_scroll = TRUE;
20609 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020610 sourcing_lnum_off = sourcing_lnum;
20611
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020612 if (line_arg != NULL)
20613 {
20614 /* Use eap->arg, split up in parts by line breaks. */
20615 theline = line_arg;
20616 p = vim_strchr(theline, '\n');
20617 if (p == NULL)
20618 line_arg += STRLEN(line_arg);
20619 else
20620 {
20621 *p = NUL;
20622 line_arg = p + 1;
20623 }
20624 }
20625 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 theline = getcmdline(':', 0L, indent);
20627 else
20628 theline = eap->getline(':', eap->cookie, indent);
20629 if (KeyTyped)
20630 lines_left = Rows - 1;
20631 if (theline == NULL)
20632 {
20633 EMSG(_("E126: Missing :endfunction"));
20634 goto erret;
20635 }
20636
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020637 /* Detect line continuation: sourcing_lnum increased more than one. */
20638 if (sourcing_lnum > sourcing_lnum_off + 1)
20639 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20640 else
20641 sourcing_lnum_off = 0;
20642
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 if (skip_until != NULL)
20644 {
20645 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20646 * don't check for ":endfunc". */
20647 if (STRCMP(theline, skip_until) == 0)
20648 {
20649 vim_free(skip_until);
20650 skip_until = NULL;
20651 }
20652 }
20653 else
20654 {
20655 /* skip ':' and blanks*/
20656 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20657 ;
20658
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020659 /* Check for "endfunction". */
20660 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020662 if (line_arg == NULL)
20663 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 break;
20665 }
20666
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020667 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 * at "end". */
20669 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20670 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020671 else if (STRNCMP(p, "if", 2) == 0
20672 || STRNCMP(p, "wh", 2) == 0
20673 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 || STRNCMP(p, "try", 3) == 0)
20675 indent += 2;
20676
20677 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020678 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020680 if (*p == '!')
20681 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 p += eval_fname_script(p);
20683 if (ASCII_ISALPHA(*p))
20684 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020685 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 if (*skipwhite(p) == '(')
20687 {
20688 ++nesting;
20689 indent += 2;
20690 }
20691 }
20692 }
20693
20694 /* Check for ":append" or ":insert". */
20695 p = skip_range(p, NULL);
20696 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20697 || (p[0] == 'i'
20698 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20699 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20700 skip_until = vim_strsave((char_u *)".");
20701
20702 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20703 arg = skipwhite(skiptowhite(p));
20704 if (arg[0] == '<' && arg[1] =='<'
20705 && ((p[0] == 'p' && p[1] == 'y'
20706 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20707 || (p[0] == 'p' && p[1] == 'e'
20708 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20709 || (p[0] == 't' && p[1] == 'c'
20710 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20711 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20712 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020713 || (p[0] == 'm' && p[1] == 'z'
20714 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 ))
20716 {
20717 /* ":python <<" continues until a dot, like ":append" */
20718 p = skipwhite(arg + 2);
20719 if (*p == NUL)
20720 skip_until = vim_strsave((char_u *)".");
20721 else
20722 skip_until = vim_strsave(p);
20723 }
20724 }
20725
20726 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020727 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020728 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020729 if (line_arg == NULL)
20730 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020732 }
20733
20734 /* Copy the line to newly allocated memory. get_one_sourceline()
20735 * allocates 250 bytes per line, this saves 80% on average. The cost
20736 * is an extra alloc/free. */
20737 p = vim_strsave(theline);
20738 if (p != NULL)
20739 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020740 if (line_arg == NULL)
20741 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020742 theline = p;
20743 }
20744
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020745 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20746
20747 /* Add NULL lines for continuation lines, so that the line count is
20748 * equal to the index in the growarray. */
20749 while (sourcing_lnum_off-- > 0)
20750 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020751
20752 /* Check for end of eap->arg. */
20753 if (line_arg != NULL && *line_arg == NUL)
20754 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 }
20756
20757 /* Don't define the function when skipping commands or when an error was
20758 * detected. */
20759 if (eap->skip || did_emsg)
20760 goto erret;
20761
20762 /*
20763 * If there are no errors, add the function
20764 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020765 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020766 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020767 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020768 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020769 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020770 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020771 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020772 goto erret;
20773 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020774
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020775 fp = find_func(name);
20776 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020778 if (!eap->forceit)
20779 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020780 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020781 goto erret;
20782 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020783 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020784 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020785 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020786 name);
20787 goto erret;
20788 }
20789 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020790 ga_clear_strings(&(fp->uf_args));
20791 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020792 vim_free(name);
20793 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 }
20796 else
20797 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020798 char numbuf[20];
20799
20800 fp = NULL;
20801 if (fudi.fd_newkey == NULL && !eap->forceit)
20802 {
20803 EMSG(_(e_funcdict));
20804 goto erret;
20805 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020806 if (fudi.fd_di == NULL)
20807 {
20808 /* Can't add a function to a locked dictionary */
20809 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20810 goto erret;
20811 }
20812 /* Can't change an existing function if it is locked */
20813 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20814 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020815
20816 /* Give the function a sequential number. Can only be used with a
20817 * Funcref! */
20818 vim_free(name);
20819 sprintf(numbuf, "%d", ++func_nr);
20820 name = vim_strsave((char_u *)numbuf);
20821 if (name == NULL)
20822 goto erret;
20823 }
20824
20825 if (fp == NULL)
20826 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020827 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020828 {
20829 int slen, plen;
20830 char_u *scriptname;
20831
20832 /* Check that the autoload name matches the script name. */
20833 j = FAIL;
20834 if (sourcing_name != NULL)
20835 {
20836 scriptname = autoload_name(name);
20837 if (scriptname != NULL)
20838 {
20839 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020840 plen = (int)STRLEN(p);
20841 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020842 if (slen > plen && fnamecmp(p,
20843 sourcing_name + slen - plen) == 0)
20844 j = OK;
20845 vim_free(scriptname);
20846 }
20847 }
20848 if (j == FAIL)
20849 {
20850 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20851 goto erret;
20852 }
20853 }
20854
20855 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 if (fp == NULL)
20857 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020858
20859 if (fudi.fd_dict != NULL)
20860 {
20861 if (fudi.fd_di == NULL)
20862 {
20863 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020864 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020865 if (fudi.fd_di == NULL)
20866 {
20867 vim_free(fp);
20868 goto erret;
20869 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020870 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20871 {
20872 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020873 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020874 goto erret;
20875 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020876 }
20877 else
20878 /* overwrite existing dict entry */
20879 clear_tv(&fudi.fd_di->di_tv);
20880 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020881 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020882 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020883 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020884
20885 /* behave like "dict" was used */
20886 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020887 }
20888
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020890 STRCPY(fp->uf_name, name);
20891 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020893 fp->uf_args = newargs;
20894 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020895#ifdef FEAT_PROFILE
20896 fp->uf_tml_count = NULL;
20897 fp->uf_tml_total = NULL;
20898 fp->uf_tml_self = NULL;
20899 fp->uf_profiling = FALSE;
20900 if (prof_def_func())
20901 func_do_profile(fp);
20902#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020903 fp->uf_varargs = varargs;
20904 fp->uf_flags = flags;
20905 fp->uf_calls = 0;
20906 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020907 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908
20909erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 ga_clear_strings(&newargs);
20911 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020912ret_free:
20913 vim_free(skip_until);
20914 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917}
20918
20919/*
20920 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020921 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020923 * flags:
20924 * TFN_INT: internal function name OK
20925 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 * Advances "pp" to just after the function name (if no error).
20927 */
20928 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020929trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 char_u **pp;
20931 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020933 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020935 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 char_u *start;
20937 char_u *end;
20938 int lead;
20939 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020941 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020942
20943 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020944 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020946
20947 /* Check for hard coded <SNR>: already translated function ID (from a user
20948 * command). */
20949 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20950 && (*pp)[2] == (int)KE_SNR)
20951 {
20952 *pp += 3;
20953 len = get_id_len(pp) + 3;
20954 return vim_strnsave(start, len);
20955 }
20956
20957 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20958 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020960 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020962
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020963 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20964 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020965 if (end == start)
20966 {
20967 if (!skip)
20968 EMSG(_("E129: Function name required"));
20969 goto theend;
20970 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020971 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020972 {
20973 /*
20974 * Report an invalid expression in braces, unless the expression
20975 * evaluation has been cancelled due to an aborting error, an
20976 * interrupt, or an exception.
20977 */
20978 if (!aborting())
20979 {
20980 if (end != NULL)
20981 EMSG2(_(e_invarg2), start);
20982 }
20983 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020984 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020985 goto theend;
20986 }
20987
20988 if (lv.ll_tv != NULL)
20989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020990 if (fdp != NULL)
20991 {
20992 fdp->fd_dict = lv.ll_dict;
20993 fdp->fd_newkey = lv.ll_newkey;
20994 lv.ll_newkey = NULL;
20995 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020997 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20998 {
20999 name = vim_strsave(lv.ll_tv->vval.v_string);
21000 *pp = end;
21001 }
21002 else
21003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021004 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21005 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021006 EMSG(_(e_funcref));
21007 else
21008 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021009 name = NULL;
21010 }
21011 goto theend;
21012 }
21013
21014 if (lv.ll_name == NULL)
21015 {
21016 /* Error found, but continue after the function name. */
21017 *pp = end;
21018 goto theend;
21019 }
21020
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021021 /* Check if the name is a Funcref. If so, use the value. */
21022 if (lv.ll_exp_name != NULL)
21023 {
21024 len = (int)STRLEN(lv.ll_exp_name);
21025 name = deref_func_name(lv.ll_exp_name, &len);
21026 if (name == lv.ll_exp_name)
21027 name = NULL;
21028 }
21029 else
21030 {
21031 len = (int)(end - *pp);
21032 name = deref_func_name(*pp, &len);
21033 if (name == *pp)
21034 name = NULL;
21035 }
21036 if (name != NULL)
21037 {
21038 name = vim_strsave(name);
21039 *pp = end;
21040 goto theend;
21041 }
21042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021043 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021044 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021045 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021046 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21047 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21048 {
21049 /* When there was "s:" already or the name expanded to get a
21050 * leading "s:" then remove it. */
21051 lv.ll_name += 2;
21052 len -= 2;
21053 lead = 2;
21054 }
21055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021056 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021057 {
21058 if (lead == 2) /* skip over "s:" */
21059 lv.ll_name += 2;
21060 len = (int)(end - lv.ll_name);
21061 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021062
21063 /*
21064 * Copy the function name to allocated memory.
21065 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21066 * Accept <SNR>123_name() outside a script.
21067 */
21068 if (skip)
21069 lead = 0; /* do nothing */
21070 else if (lead > 0)
21071 {
21072 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021073 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21074 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021075 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021076 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021077 if (current_SID <= 0)
21078 {
21079 EMSG(_(e_usingsid));
21080 goto theend;
21081 }
21082 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21083 lead += (int)STRLEN(sid_buf);
21084 }
21085 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021086 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021087 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021088 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021089 goto theend;
21090 }
21091 name = alloc((unsigned)(len + lead + 1));
21092 if (name != NULL)
21093 {
21094 if (lead > 0)
21095 {
21096 name[0] = K_SPECIAL;
21097 name[1] = KS_EXTRA;
21098 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021099 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021100 STRCPY(name + 3, sid_buf);
21101 }
21102 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21103 name[len + lead] = NUL;
21104 }
21105 *pp = end;
21106
21107theend:
21108 clear_lval(&lv);
21109 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111
21112/*
21113 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21114 * Return 2 if "p" starts with "s:".
21115 * Return 0 otherwise.
21116 */
21117 static int
21118eval_fname_script(p)
21119 char_u *p;
21120{
21121 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21122 || STRNICMP(p + 1, "SNR>", 4) == 0))
21123 return 5;
21124 if (p[0] == 's' && p[1] == ':')
21125 return 2;
21126 return 0;
21127}
21128
21129/*
21130 * Return TRUE if "p" starts with "<SID>" or "s:".
21131 * Only works if eval_fname_script() returned non-zero for "p"!
21132 */
21133 static int
21134eval_fname_sid(p)
21135 char_u *p;
21136{
21137 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21138}
21139
21140/*
21141 * List the head of the function: "name(arg1, arg2)".
21142 */
21143 static void
21144list_func_head(fp, indent)
21145 ufunc_T *fp;
21146 int indent;
21147{
21148 int j;
21149
21150 msg_start();
21151 if (indent)
21152 MSG_PUTS(" ");
21153 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021154 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 {
21156 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021157 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 }
21159 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021160 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021162 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 {
21164 if (j)
21165 MSG_PUTS(", ");
21166 msg_puts(FUNCARG(fp, j));
21167 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021168 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 {
21170 if (j)
21171 MSG_PUTS(", ");
21172 MSG_PUTS("...");
21173 }
21174 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021175 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021176 if (p_verbose > 0)
21177 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178}
21179
21180/*
21181 * Find a function by name, return pointer to it in ufuncs.
21182 * Return NULL for unknown function.
21183 */
21184 static ufunc_T *
21185find_func(name)
21186 char_u *name;
21187{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021188 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021190 hi = hash_find(&func_hashtab, name);
21191 if (!HASHITEM_EMPTY(hi))
21192 return HI2UF(hi);
21193 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194}
21195
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021196#if defined(EXITFREE) || defined(PROTO)
21197 void
21198free_all_functions()
21199{
21200 hashitem_T *hi;
21201
21202 /* Need to start all over every time, because func_free() may change the
21203 * hash table. */
21204 while (func_hashtab.ht_used > 0)
21205 for (hi = func_hashtab.ht_array; ; ++hi)
21206 if (!HASHITEM_EMPTY(hi))
21207 {
21208 func_free(HI2UF(hi));
21209 break;
21210 }
21211}
21212#endif
21213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021214/*
21215 * Return TRUE if a function "name" exists.
21216 */
21217 static int
21218function_exists(name)
21219 char_u *name;
21220{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021221 char_u *nm = name;
21222 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021223 int n = FALSE;
21224
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021225 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021226 nm = skipwhite(nm);
21227
21228 /* Only accept "funcname", "funcname ", "funcname (..." and
21229 * "funcname(...", not "funcname!...". */
21230 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021231 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021232 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021234 else
21235 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021236 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021237 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021238 return n;
21239}
21240
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021241/*
21242 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021243 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021244 */
21245 static int
21246builtin_function(name)
21247 char_u *name;
21248{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021249 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21250 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021251}
21252
Bram Moolenaar05159a02005-02-26 23:04:13 +000021253#if defined(FEAT_PROFILE) || defined(PROTO)
21254/*
21255 * Start profiling function "fp".
21256 */
21257 static void
21258func_do_profile(fp)
21259 ufunc_T *fp;
21260{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021261 int len = fp->uf_lines.ga_len;
21262
21263 if (len == 0)
21264 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021265 fp->uf_tm_count = 0;
21266 profile_zero(&fp->uf_tm_self);
21267 profile_zero(&fp->uf_tm_total);
21268 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021269 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021270 if (fp->uf_tml_total == NULL)
21271 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021272 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021273 if (fp->uf_tml_self == NULL)
21274 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021275 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021276 fp->uf_tml_idx = -1;
21277 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21278 || fp->uf_tml_self == NULL)
21279 return; /* out of memory */
21280
21281 fp->uf_profiling = TRUE;
21282}
21283
21284/*
21285 * Dump the profiling results for all functions in file "fd".
21286 */
21287 void
21288func_dump_profile(fd)
21289 FILE *fd;
21290{
21291 hashitem_T *hi;
21292 int todo;
21293 ufunc_T *fp;
21294 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021295 ufunc_T **sorttab;
21296 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021298 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021299 if (todo == 0)
21300 return; /* nothing to dump */
21301
Bram Moolenaar73830342005-02-28 22:48:19 +000021302 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21303
Bram Moolenaar05159a02005-02-26 23:04:13 +000021304 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21305 {
21306 if (!HASHITEM_EMPTY(hi))
21307 {
21308 --todo;
21309 fp = HI2UF(hi);
21310 if (fp->uf_profiling)
21311 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021312 if (sorttab != NULL)
21313 sorttab[st_len++] = fp;
21314
Bram Moolenaar05159a02005-02-26 23:04:13 +000021315 if (fp->uf_name[0] == K_SPECIAL)
21316 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21317 else
21318 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21319 if (fp->uf_tm_count == 1)
21320 fprintf(fd, "Called 1 time\n");
21321 else
21322 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21323 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21324 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21325 fprintf(fd, "\n");
21326 fprintf(fd, "count total (s) self (s)\n");
21327
21328 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21329 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021330 if (FUNCLINE(fp, i) == NULL)
21331 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021332 prof_func_line(fd, fp->uf_tml_count[i],
21333 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021334 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21335 }
21336 fprintf(fd, "\n");
21337 }
21338 }
21339 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021340
21341 if (sorttab != NULL && st_len > 0)
21342 {
21343 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21344 prof_total_cmp);
21345 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21346 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21347 prof_self_cmp);
21348 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21349 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021350
21351 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021352}
Bram Moolenaar73830342005-02-28 22:48:19 +000021353
21354 static void
21355prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21356 FILE *fd;
21357 ufunc_T **sorttab;
21358 int st_len;
21359 char *title;
21360 int prefer_self; /* when equal print only self time */
21361{
21362 int i;
21363 ufunc_T *fp;
21364
21365 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21366 fprintf(fd, "count total (s) self (s) function\n");
21367 for (i = 0; i < 20 && i < st_len; ++i)
21368 {
21369 fp = sorttab[i];
21370 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21371 prefer_self);
21372 if (fp->uf_name[0] == K_SPECIAL)
21373 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21374 else
21375 fprintf(fd, " %s()\n", fp->uf_name);
21376 }
21377 fprintf(fd, "\n");
21378}
21379
21380/*
21381 * Print the count and times for one function or function line.
21382 */
21383 static void
21384prof_func_line(fd, count, total, self, prefer_self)
21385 FILE *fd;
21386 int count;
21387 proftime_T *total;
21388 proftime_T *self;
21389 int prefer_self; /* when equal print only self time */
21390{
21391 if (count > 0)
21392 {
21393 fprintf(fd, "%5d ", count);
21394 if (prefer_self && profile_equal(total, self))
21395 fprintf(fd, " ");
21396 else
21397 fprintf(fd, "%s ", profile_msg(total));
21398 if (!prefer_self && profile_equal(total, self))
21399 fprintf(fd, " ");
21400 else
21401 fprintf(fd, "%s ", profile_msg(self));
21402 }
21403 else
21404 fprintf(fd, " ");
21405}
21406
21407/*
21408 * Compare function for total time sorting.
21409 */
21410 static int
21411#ifdef __BORLANDC__
21412_RTLENTRYF
21413#endif
21414prof_total_cmp(s1, s2)
21415 const void *s1;
21416 const void *s2;
21417{
21418 ufunc_T *p1, *p2;
21419
21420 p1 = *(ufunc_T **)s1;
21421 p2 = *(ufunc_T **)s2;
21422 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21423}
21424
21425/*
21426 * Compare function for self time sorting.
21427 */
21428 static int
21429#ifdef __BORLANDC__
21430_RTLENTRYF
21431#endif
21432prof_self_cmp(s1, s2)
21433 const void *s1;
21434 const void *s2;
21435{
21436 ufunc_T *p1, *p2;
21437
21438 p1 = *(ufunc_T **)s1;
21439 p2 = *(ufunc_T **)s2;
21440 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21441}
21442
Bram Moolenaar05159a02005-02-26 23:04:13 +000021443#endif
21444
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021445/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021446 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021447 * Return TRUE if a package was loaded.
21448 */
21449 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021450script_autoload(name, reload)
21451 char_u *name;
21452 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021453{
21454 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021455 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021456 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021457 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021458
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021459 /* Return quickly when autoload disabled. */
21460 if (no_autoload)
21461 return FALSE;
21462
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021463 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021464 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021465 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021466 return FALSE;
21467
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021468 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021470 /* Find the name in the list of previously loaded package names. Skip
21471 * "autoload/", it's always the same. */
21472 for (i = 0; i < ga_loaded.ga_len; ++i)
21473 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21474 break;
21475 if (!reload && i < ga_loaded.ga_len)
21476 ret = FALSE; /* was loaded already */
21477 else
21478 {
21479 /* Remember the name if it wasn't loaded already. */
21480 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21481 {
21482 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21483 tofree = NULL;
21484 }
21485
21486 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021487 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021488 ret = TRUE;
21489 }
21490
21491 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021492 return ret;
21493}
21494
21495/*
21496 * Return the autoload script name for a function or variable name.
21497 * Returns NULL when out of memory.
21498 */
21499 static char_u *
21500autoload_name(name)
21501 char_u *name;
21502{
21503 char_u *p;
21504 char_u *scriptname;
21505
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021506 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021507 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21508 if (scriptname == NULL)
21509 return FALSE;
21510 STRCPY(scriptname, "autoload/");
21511 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021512 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021513 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021514 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021515 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021516 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021517}
21518
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21520
21521/*
21522 * Function given to ExpandGeneric() to obtain the list of user defined
21523 * function names.
21524 */
21525 char_u *
21526get_user_func_name(xp, idx)
21527 expand_T *xp;
21528 int idx;
21529{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021530 static long_u done;
21531 static hashitem_T *hi;
21532 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533
21534 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021536 done = 0;
21537 hi = func_hashtab.ht_array;
21538 }
21539 if (done < func_hashtab.ht_used)
21540 {
21541 if (done++ > 0)
21542 ++hi;
21543 while (HASHITEM_EMPTY(hi))
21544 ++hi;
21545 fp = HI2UF(hi);
21546
21547 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21548 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549
21550 cat_func_name(IObuff, fp);
21551 if (xp->xp_context != EXPAND_USER_FUNC)
21552 {
21553 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021554 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 STRCAT(IObuff, ")");
21556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 return IObuff;
21558 }
21559 return NULL;
21560}
21561
21562#endif /* FEAT_CMDL_COMPL */
21563
21564/*
21565 * Copy the function name of "fp" to buffer "buf".
21566 * "buf" must be able to hold the function name plus three bytes.
21567 * Takes care of script-local function names.
21568 */
21569 static void
21570cat_func_name(buf, fp)
21571 char_u *buf;
21572 ufunc_T *fp;
21573{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021574 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 {
21576 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021577 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 }
21579 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021580 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581}
21582
21583/*
21584 * ":delfunction {name}"
21585 */
21586 void
21587ex_delfunction(eap)
21588 exarg_T *eap;
21589{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021590 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 char_u *p;
21592 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021593 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594
21595 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 name = trans_function_name(&p, eap->skip, 0, &fudi);
21597 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021599 {
21600 if (fudi.fd_dict != NULL && !eap->skip)
21601 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 if (!ends_excmd(*skipwhite(p)))
21605 {
21606 vim_free(name);
21607 EMSG(_(e_trailing));
21608 return;
21609 }
21610 eap->nextcmd = check_nextcmd(p);
21611 if (eap->nextcmd != NULL)
21612 *p = NUL;
21613
21614 if (!eap->skip)
21615 fp = find_func(name);
21616 vim_free(name);
21617
21618 if (!eap->skip)
21619 {
21620 if (fp == NULL)
21621 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021622 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 return;
21624 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 {
21627 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21628 return;
21629 }
21630
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021631 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 /* Delete the dict item that refers to the function, it will
21634 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021635 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021637 else
21638 func_free(fp);
21639 }
21640}
21641
21642/*
21643 * Free a function and remove it from the list of functions.
21644 */
21645 static void
21646func_free(fp)
21647 ufunc_T *fp;
21648{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021649 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021650
21651 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021652 ga_clear_strings(&(fp->uf_args));
21653 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021654#ifdef FEAT_PROFILE
21655 vim_free(fp->uf_tml_count);
21656 vim_free(fp->uf_tml_total);
21657 vim_free(fp->uf_tml_self);
21658#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021659
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 /* remove the function from the function hashtable */
21661 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21662 if (HASHITEM_EMPTY(hi))
21663 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021664 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021665 hash_remove(&func_hashtab, hi);
21666
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021667 vim_free(fp);
21668}
21669
21670/*
21671 * Unreference a Function: decrement the reference count and free it when it
21672 * becomes zero. Only for numbered functions.
21673 */
21674 static void
21675func_unref(name)
21676 char_u *name;
21677{
21678 ufunc_T *fp;
21679
21680 if (name != NULL && isdigit(*name))
21681 {
21682 fp = find_func(name);
21683 if (fp == NULL)
21684 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021685 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021686 {
21687 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021688 * when "uf_calls" becomes zero. */
21689 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021690 func_free(fp);
21691 }
21692 }
21693}
21694
21695/*
21696 * Count a reference to a Function.
21697 */
21698 static void
21699func_ref(name)
21700 char_u *name;
21701{
21702 ufunc_T *fp;
21703
21704 if (name != NULL && isdigit(*name))
21705 {
21706 fp = find_func(name);
21707 if (fp == NULL)
21708 EMSG2(_(e_intern2), "func_ref()");
21709 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 }
21712}
21713
21714/*
21715 * Call a user function.
21716 */
21717 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021718call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 ufunc_T *fp; /* pointer to function */
21720 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 typval_T *argvars; /* arguments */
21722 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 linenr_T firstline; /* first line of range */
21724 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021725 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726{
Bram Moolenaar33570922005-01-25 22:26:29 +000021727 char_u *save_sourcing_name;
21728 linenr_T save_sourcing_lnum;
21729 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021730 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021731 int save_did_emsg;
21732 static int depth = 0;
21733 dictitem_T *v;
21734 int fixvar_idx = 0; /* index in fixvar[] */
21735 int i;
21736 int ai;
21737 char_u numbuf[NUMBUFLEN];
21738 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021739#ifdef FEAT_PROFILE
21740 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021741 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
21744 /* If depth of calling is getting too high, don't execute the function */
21745 if (depth >= p_mfd)
21746 {
21747 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021748 rettv->v_type = VAR_NUMBER;
21749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 return;
21751 }
21752 ++depth;
21753
21754 line_breakcheck(); /* check for CTRL-C hit */
21755
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021756 fc = (funccall_T *)alloc(sizeof(funccall_T));
21757 fc->caller = current_funccal;
21758 current_funccal = fc;
21759 fc->func = fp;
21760 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021762 fc->linenr = 0;
21763 fc->returned = FALSE;
21764 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021766 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21767 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021770 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21772 * each argument variable and saves a lot of time.
21773 */
21774 /*
21775 * Init l: variables.
21776 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021777 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021778 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021779 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021780 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21781 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021782 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021783 name = v->di_key;
21784 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021785 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021786 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021788 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021789 v->di_tv.vval.v_dict = selfdict;
21790 ++selfdict->dv_refcount;
21791 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021792
Bram Moolenaar33570922005-01-25 22:26:29 +000021793 /*
21794 * Init a: variables.
21795 * Set a:0 to "argcount".
21796 * Set a:000 to a list with room for the "..." arguments.
21797 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021798 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21799 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021800 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021801 /* Use "name" to avoid a warning from some compiler that checks the
21802 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021803 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021804 name = v->di_key;
21805 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021807 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021809 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021810 v->di_tv.vval.v_list = &fc->l_varlist;
21811 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21812 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21813 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021814
21815 /*
21816 * Set a:firstline to "firstline" and a:lastline to "lastline".
21817 * Set a:name to named arguments.
21818 * Set a:N to the "..." arguments.
21819 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021820 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021822 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021823 (varnumber_T)lastline);
21824 for (i = 0; i < argcount; ++i)
21825 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021826 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 if (ai < 0)
21828 /* named argument a:name */
21829 name = FUNCARG(fp, i);
21830 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 /* "..." argument a:1, a:2, etc. */
21833 sprintf((char *)numbuf, "%d", ai + 1);
21834 name = numbuf;
21835 }
21836 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21837 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021838 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21840 }
21841 else
21842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021843 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21844 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 if (v == NULL)
21846 break;
21847 v->di_flags = DI_FLAGS_RO;
21848 }
21849 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021850 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021851
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021852 /* Note: the values are copied directly to avoid alloc/free.
21853 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021855 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021856
21857 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21858 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021859 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21860 fc->l_listitems[ai].li_tv = argvars[i];
21861 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021862 }
21863 }
21864
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 /* Don't redraw while executing the function. */
21866 ++RedrawingDisabled;
21867 save_sourcing_name = sourcing_name;
21868 save_sourcing_lnum = sourcing_lnum;
21869 sourcing_lnum = 1;
21870 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021871 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 if (sourcing_name != NULL)
21873 {
21874 if (save_sourcing_name != NULL
21875 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21876 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21877 else
21878 STRCPY(sourcing_name, "function ");
21879 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21880
21881 if (p_verbose >= 12)
21882 {
21883 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021884 verbose_enter_scroll();
21885
Bram Moolenaar555b2802005-05-19 21:08:39 +000021886 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 if (p_verbose >= 14)
21888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021890 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021891 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021892 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893
21894 msg_puts((char_u *)"(");
21895 for (i = 0; i < argcount; ++i)
21896 {
21897 if (i > 0)
21898 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021899 if (argvars[i].v_type == VAR_NUMBER)
21900 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 else
21902 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021903 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21904 if (s != NULL)
21905 {
21906 trunc_string(s, buf, MSG_BUF_CLEN);
21907 msg_puts(buf);
21908 vim_free(tofree);
21909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 }
21911 }
21912 msg_puts((char_u *)")");
21913 }
21914 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021915
21916 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 --no_wait_return;
21918 }
21919 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021920#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021921 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021922 {
21923 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21924 func_do_profile(fp);
21925 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021926 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021927 {
21928 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021929 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021930 profile_zero(&fp->uf_tm_children);
21931 }
21932 script_prof_save(&wait_start);
21933 }
21934#endif
21935
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021937 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 save_did_emsg = did_emsg;
21939 did_emsg = FALSE;
21940
21941 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021942 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21944
21945 --RedrawingDisabled;
21946
21947 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021948 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021950 clear_tv(rettv);
21951 rettv->v_type = VAR_NUMBER;
21952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 }
21954
Bram Moolenaar05159a02005-02-26 23:04:13 +000021955#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021956 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021957 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021958 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021959 profile_end(&call_start);
21960 profile_sub_wait(&wait_start, &call_start);
21961 profile_add(&fp->uf_tm_total, &call_start);
21962 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021963 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021964 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021965 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21966 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021967 }
21968 }
21969#endif
21970
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 /* when being verbose, mention the return value */
21972 if (p_verbose >= 12)
21973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021975 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021978 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021979 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021980 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021981 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021984 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021985 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021986 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021987 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021988
Bram Moolenaar555b2802005-05-19 21:08:39 +000021989 /* The value may be very long. Skip the middle part, so that we
21990 * have some idea how it starts and ends. smsg() would always
21991 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021992 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021993 if (s != NULL)
21994 {
21995 trunc_string(s, buf, MSG_BUF_CLEN);
21996 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21997 vim_free(tofree);
21998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 }
22000 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022001
22002 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 --no_wait_return;
22004 }
22005
22006 vim_free(sourcing_name);
22007 sourcing_name = save_sourcing_name;
22008 sourcing_lnum = save_sourcing_lnum;
22009 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022010#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022011 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022012 script_prof_restore(&wait_start);
22013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014
22015 if (p_verbose >= 12 && sourcing_name != NULL)
22016 {
22017 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022018 verbose_enter_scroll();
22019
Bram Moolenaar555b2802005-05-19 21:08:39 +000022020 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022022
22023 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 --no_wait_return;
22025 }
22026
22027 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022028 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022030
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022031 /* If the a:000 list and the l: and a: dicts are not referenced we can
22032 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022033 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22034 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22035 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22036 {
22037 free_funccal(fc, FALSE);
22038 }
22039 else
22040 {
22041 hashitem_T *hi;
22042 listitem_T *li;
22043 int todo;
22044
22045 /* "fc" is still in use. This can happen when returning "a:000" or
22046 * assigning "l:" to a global variable.
22047 * Link "fc" in the list for garbage collection later. */
22048 fc->caller = previous_funccal;
22049 previous_funccal = fc;
22050
22051 /* Make a copy of the a: variables, since we didn't do that above. */
22052 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22053 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22054 {
22055 if (!HASHITEM_EMPTY(hi))
22056 {
22057 --todo;
22058 v = HI2DI(hi);
22059 copy_tv(&v->di_tv, &v->di_tv);
22060 }
22061 }
22062
22063 /* Make a copy of the a:000 items, since we didn't do that above. */
22064 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22065 copy_tv(&li->li_tv, &li->li_tv);
22066 }
22067}
22068
22069/*
22070 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022071 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022072 */
22073 static int
22074can_free_funccal(fc, copyID)
22075 funccall_T *fc;
22076 int copyID;
22077{
22078 return (fc->l_varlist.lv_copyID != copyID
22079 && fc->l_vars.dv_copyID != copyID
22080 && fc->l_avars.dv_copyID != copyID);
22081}
22082
22083/*
22084 * Free "fc" and what it contains.
22085 */
22086 static void
22087free_funccal(fc, free_val)
22088 funccall_T *fc;
22089 int free_val; /* a: vars were allocated */
22090{
22091 listitem_T *li;
22092
22093 /* The a: variables typevals may not have been allocated, only free the
22094 * allocated variables. */
22095 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22096
22097 /* free all l: variables */
22098 vars_clear(&fc->l_vars.dv_hashtab);
22099
22100 /* Free the a:000 variables if they were allocated. */
22101 if (free_val)
22102 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22103 clear_tv(&li->li_tv);
22104
22105 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106}
22107
22108/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022109 * Add a number variable "name" to dict "dp" with value "nr".
22110 */
22111 static void
22112add_nr_var(dp, v, name, nr)
22113 dict_T *dp;
22114 dictitem_T *v;
22115 char *name;
22116 varnumber_T nr;
22117{
22118 STRCPY(v->di_key, name);
22119 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22120 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22121 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022122 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 v->di_tv.vval.v_number = nr;
22124}
22125
22126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 * ":return [expr]"
22128 */
22129 void
22130ex_return(eap)
22131 exarg_T *eap;
22132{
22133 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 int returning = FALSE;
22136
22137 if (current_funccal == NULL)
22138 {
22139 EMSG(_("E133: :return not inside a function"));
22140 return;
22141 }
22142
22143 if (eap->skip)
22144 ++emsg_skip;
22145
22146 eap->nextcmd = NULL;
22147 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022148 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 {
22150 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022151 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022153 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 }
22155 /* It's safer to return also on error. */
22156 else if (!eap->skip)
22157 {
22158 /*
22159 * Return unless the expression evaluation has been cancelled due to an
22160 * aborting error, an interrupt, or an exception.
22161 */
22162 if (!aborting())
22163 returning = do_return(eap, FALSE, TRUE, NULL);
22164 }
22165
22166 /* When skipping or the return gets pending, advance to the next command
22167 * in this line (!returning). Otherwise, ignore the rest of the line.
22168 * Following lines will be ignored by get_func_line(). */
22169 if (returning)
22170 eap->nextcmd = NULL;
22171 else if (eap->nextcmd == NULL) /* no argument */
22172 eap->nextcmd = check_nextcmd(arg);
22173
22174 if (eap->skip)
22175 --emsg_skip;
22176}
22177
22178/*
22179 * Return from a function. Possibly makes the return pending. Also called
22180 * for a pending return at the ":endtry" or after returning from an extra
22181 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022183 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 * FALSE when the return gets pending.
22185 */
22186 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022187do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 exarg_T *eap;
22189 int reanimate;
22190 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022191 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192{
22193 int idx;
22194 struct condstack *cstack = eap->cstack;
22195
22196 if (reanimate)
22197 /* Undo the return. */
22198 current_funccal->returned = FALSE;
22199
22200 /*
22201 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22202 * not in its finally clause (which then is to be executed next) is found.
22203 * In this case, make the ":return" pending for execution at the ":endtry".
22204 * Otherwise, return normally.
22205 */
22206 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22207 if (idx >= 0)
22208 {
22209 cstack->cs_pending[idx] = CSTP_RETURN;
22210
22211 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022212 /* A pending return again gets pending. "rettv" points to an
22213 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022215 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 else
22217 {
22218 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022219 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022221 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022223 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 {
22225 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022226 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022227 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 else
22229 EMSG(_(e_outofmem));
22230 }
22231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022232 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233
22234 if (reanimate)
22235 {
22236 /* The pending return value could be overwritten by a ":return"
22237 * without argument in a finally clause; reset the default
22238 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 current_funccal->rettv->v_type = VAR_NUMBER;
22240 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 }
22242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022243 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 }
22245 else
22246 {
22247 current_funccal->returned = TRUE;
22248
22249 /* If the return is carried out now, store the return value. For
22250 * a return immediately after reanimation, the value is already
22251 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022254 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022257 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 }
22259 }
22260
22261 return idx < 0;
22262}
22263
22264/*
22265 * Free the variable with a pending return value.
22266 */
22267 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022268discard_pending_return(rettv)
22269 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270{
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272}
22273
22274/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022275 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 * is an allocated string. Used by report_pending() for verbose messages.
22277 */
22278 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022279get_return_cmd(rettv)
22280 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022282 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022283 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022284 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022286 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022287 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022288 if (s == NULL)
22289 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022290
22291 STRCPY(IObuff, ":return ");
22292 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22293 if (STRLEN(s) + 8 >= IOSIZE)
22294 STRCPY(IObuff + IOSIZE - 4, "...");
22295 vim_free(tofree);
22296 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297}
22298
22299/*
22300 * Get next function line.
22301 * Called by do_cmdline() to get the next line.
22302 * Returns allocated string, or NULL for end of function.
22303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 char_u *
22305get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022306 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022308 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309{
Bram Moolenaar33570922005-01-25 22:26:29 +000022310 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022311 ufunc_T *fp = fcp->func;
22312 char_u *retval;
22313 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314
22315 /* If breakpoints have been added/deleted need to check for it. */
22316 if (fcp->dbg_tick != debug_tick)
22317 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022318 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 sourcing_lnum);
22320 fcp->dbg_tick = debug_tick;
22321 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022322#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022323 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022324 func_line_end(cookie);
22325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326
Bram Moolenaar05159a02005-02-26 23:04:13 +000022327 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022328 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22329 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 retval = NULL;
22331 else
22332 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022333 /* Skip NULL lines (continuation lines). */
22334 while (fcp->linenr < gap->ga_len
22335 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22336 ++fcp->linenr;
22337 if (fcp->linenr >= gap->ga_len)
22338 retval = NULL;
22339 else
22340 {
22341 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22342 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022343#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022344 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022345 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022346#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 }
22349
22350 /* Did we encounter a breakpoint? */
22351 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22352 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022353 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022355 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 sourcing_lnum);
22357 fcp->dbg_tick = debug_tick;
22358 }
22359
22360 return retval;
22361}
22362
Bram Moolenaar05159a02005-02-26 23:04:13 +000022363#if defined(FEAT_PROFILE) || defined(PROTO)
22364/*
22365 * Called when starting to read a function line.
22366 * "sourcing_lnum" must be correct!
22367 * When skipping lines it may not actually be executed, but we won't find out
22368 * until later and we need to store the time now.
22369 */
22370 void
22371func_line_start(cookie)
22372 void *cookie;
22373{
22374 funccall_T *fcp = (funccall_T *)cookie;
22375 ufunc_T *fp = fcp->func;
22376
22377 if (fp->uf_profiling && sourcing_lnum >= 1
22378 && sourcing_lnum <= fp->uf_lines.ga_len)
22379 {
22380 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022381 /* Skip continuation lines. */
22382 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22383 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022384 fp->uf_tml_execed = FALSE;
22385 profile_start(&fp->uf_tml_start);
22386 profile_zero(&fp->uf_tml_children);
22387 profile_get_wait(&fp->uf_tml_wait);
22388 }
22389}
22390
22391/*
22392 * Called when actually executing a function line.
22393 */
22394 void
22395func_line_exec(cookie)
22396 void *cookie;
22397{
22398 funccall_T *fcp = (funccall_T *)cookie;
22399 ufunc_T *fp = fcp->func;
22400
22401 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22402 fp->uf_tml_execed = TRUE;
22403}
22404
22405/*
22406 * Called when done with a function line.
22407 */
22408 void
22409func_line_end(cookie)
22410 void *cookie;
22411{
22412 funccall_T *fcp = (funccall_T *)cookie;
22413 ufunc_T *fp = fcp->func;
22414
22415 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22416 {
22417 if (fp->uf_tml_execed)
22418 {
22419 ++fp->uf_tml_count[fp->uf_tml_idx];
22420 profile_end(&fp->uf_tml_start);
22421 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022422 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022423 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22424 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022425 }
22426 fp->uf_tml_idx = -1;
22427 }
22428}
22429#endif
22430
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431/*
22432 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022433 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 */
22435 int
22436func_has_ended(cookie)
22437 void *cookie;
22438{
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440
22441 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22442 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 || fcp->returned);
22445}
22446
22447/*
22448 * return TRUE if cookie indicates a function which "abort"s on errors.
22449 */
22450 int
22451func_has_abort(cookie)
22452 void *cookie;
22453{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022454 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455}
22456
22457#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22458typedef enum
22459{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022460 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22461 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22462 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463} var_flavour_T;
22464
22465static var_flavour_T var_flavour __ARGS((char_u *varname));
22466
22467 static var_flavour_T
22468var_flavour(varname)
22469 char_u *varname;
22470{
22471 char_u *p = varname;
22472
22473 if (ASCII_ISUPPER(*p))
22474 {
22475 while (*(++p))
22476 if (ASCII_ISLOWER(*p))
22477 return VAR_FLAVOUR_SESSION;
22478 return VAR_FLAVOUR_VIMINFO;
22479 }
22480 else
22481 return VAR_FLAVOUR_DEFAULT;
22482}
22483#endif
22484
22485#if defined(FEAT_VIMINFO) || defined(PROTO)
22486/*
22487 * Restore global vars that start with a capital from the viminfo file
22488 */
22489 int
22490read_viminfo_varlist(virp, writing)
22491 vir_T *virp;
22492 int writing;
22493{
22494 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022495 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497
22498 if (!writing && (find_viminfo_parameter('!') != NULL))
22499 {
22500 tab = vim_strchr(virp->vir_line + 1, '\t');
22501 if (tab != NULL)
22502 {
22503 *tab++ = '\0'; /* isolate the variable name */
22504 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022505 type = VAR_STRING;
22506#ifdef FEAT_FLOAT
22507 else if (*tab == 'F')
22508 type = VAR_FLOAT;
22509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510
22511 tab = vim_strchr(tab, '\t');
22512 if (tab != NULL)
22513 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022514 tv.v_type = type;
22515 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022516 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022518#ifdef FEAT_FLOAT
22519 else if (type == VAR_FLOAT)
22520 (void)string2float(tab + 1, &tv.vval.v_float);
22521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022523 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022524 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022525 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022526 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 }
22528 }
22529 }
22530
22531 return viminfo_readline(virp);
22532}
22533
22534/*
22535 * Write global vars that start with a capital to the viminfo file
22536 */
22537 void
22538write_viminfo_varlist(fp)
22539 FILE *fp;
22540{
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 hashitem_T *hi;
22542 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022543 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022544 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022545 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022546 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022547 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548
22549 if (find_viminfo_parameter('!') == NULL)
22550 return;
22551
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022552 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022553
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022554 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022557 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022559 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 this_var = HI2DI(hi);
22561 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022562 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022564 {
22565 case VAR_STRING: s = "STR"; break;
22566 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022567#ifdef FEAT_FLOAT
22568 case VAR_FLOAT: s = "FLO"; break;
22569#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022570 default: continue;
22571 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022572 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022573 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022574 if (p != NULL)
22575 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022576 vim_free(tofree);
22577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 }
22579 }
22580}
22581#endif
22582
22583#if defined(FEAT_SESSION) || defined(PROTO)
22584 int
22585store_session_globals(fd)
22586 FILE *fd;
22587{
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 hashitem_T *hi;
22589 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022590 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 char_u *p, *t;
22592
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022593 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022596 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022598 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022599 this_var = HI2DI(hi);
22600 if ((this_var->di_tv.v_type == VAR_NUMBER
22601 || this_var->di_tv.v_type == VAR_STRING)
22602 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022603 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022604 /* Escape special characters with a backslash. Turn a LF and
22605 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022607 (char_u *)"\\\"\n\r");
22608 if (p == NULL) /* out of memory */
22609 break;
22610 for (t = p; *t != NUL; ++t)
22611 if (*t == '\n')
22612 *t = 'n';
22613 else if (*t == '\r')
22614 *t = 'r';
22615 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022616 this_var->di_key,
22617 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22618 : ' ',
22619 p,
22620 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22621 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022622 || put_eol(fd) == FAIL)
22623 {
22624 vim_free(p);
22625 return FAIL;
22626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 vim_free(p);
22628 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022629#ifdef FEAT_FLOAT
22630 else if (this_var->di_tv.v_type == VAR_FLOAT
22631 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22632 {
22633 float_T f = this_var->di_tv.vval.v_float;
22634 int sign = ' ';
22635
22636 if (f < 0)
22637 {
22638 f = -f;
22639 sign = '-';
22640 }
22641 if ((fprintf(fd, "let %s = %c&%f",
22642 this_var->di_key, sign, f) < 0)
22643 || put_eol(fd) == FAIL)
22644 return FAIL;
22645 }
22646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 }
22648 }
22649 return OK;
22650}
22651#endif
22652
Bram Moolenaar661b1822005-07-28 22:36:45 +000022653/*
22654 * Display script name where an item was last set.
22655 * Should only be invoked when 'verbose' is non-zero.
22656 */
22657 void
22658last_set_msg(scriptID)
22659 scid_T scriptID;
22660{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022661 char_u *p;
22662
Bram Moolenaar661b1822005-07-28 22:36:45 +000022663 if (scriptID != 0)
22664 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022665 p = home_replace_save(NULL, get_scriptname(scriptID));
22666 if (p != NULL)
22667 {
22668 verbose_enter();
22669 MSG_PUTS(_("\n\tLast set from "));
22670 MSG_PUTS(p);
22671 vim_free(p);
22672 verbose_leave();
22673 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022674 }
22675}
22676
Bram Moolenaard812df62008-11-09 12:46:09 +000022677/*
22678 * List v:oldfiles in a nice way.
22679 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022680 void
22681ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022682 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022683{
22684 list_T *l = vimvars[VV_OLDFILES].vv_list;
22685 listitem_T *li;
22686 int nr = 0;
22687
22688 if (l == NULL)
22689 msg((char_u *)_("No old files"));
22690 else
22691 {
22692 msg_start();
22693 msg_scroll = TRUE;
22694 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22695 {
22696 msg_outnum((long)++nr);
22697 MSG_PUTS(": ");
22698 msg_outtrans(get_tv_string(&li->li_tv));
22699 msg_putchar('\n');
22700 out_flush(); /* output one line at a time */
22701 ui_breakcheck();
22702 }
22703 /* Assume "got_int" was set to truncate the listing. */
22704 got_int = FALSE;
22705
22706#ifdef FEAT_BROWSE_CMD
22707 if (cmdmod.browse)
22708 {
22709 quit_more = FALSE;
22710 nr = prompt_for_number(FALSE);
22711 msg_starthere();
22712 if (nr > 0)
22713 {
22714 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22715 (long)nr);
22716
22717 if (p != NULL)
22718 {
22719 p = expand_env_save(p);
22720 eap->arg = p;
22721 eap->cmdidx = CMD_edit;
22722 cmdmod.browse = FALSE;
22723 do_exedit(eap, NULL);
22724 vim_free(p);
22725 }
22726 }
22727 }
22728#endif
22729 }
22730}
22731
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732#endif /* FEAT_EVAL */
22733
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022735#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736
22737#ifdef WIN3264
22738/*
22739 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22740 */
22741static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22742static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22743static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22744
22745/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022746 * Get the short path (8.3) for the filename in "fnamep".
22747 * Only works for a valid file name.
22748 * When the path gets longer "fnamep" is changed and the allocated buffer
22749 * is put in "bufp".
22750 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22751 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 */
22753 static int
22754get_short_pathname(fnamep, bufp, fnamelen)
22755 char_u **fnamep;
22756 char_u **bufp;
22757 int *fnamelen;
22758{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022759 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 char_u *newbuf;
22761
22762 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 l = GetShortPathName(*fnamep, *fnamep, len);
22764 if (l > len - 1)
22765 {
22766 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022767 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 newbuf = vim_strnsave(*fnamep, l);
22769 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022770 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771
22772 vim_free(*bufp);
22773 *fnamep = *bufp = newbuf;
22774
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022775 /* Really should always succeed, as the buffer is big enough. */
22776 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 }
22778
22779 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022780 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781}
22782
22783/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022784 * Get the short path (8.3) for the filename in "fname". The converted
22785 * path is returned in "bufp".
22786 *
22787 * Some of the directories specified in "fname" may not exist. This function
22788 * will shorten the existing directories at the beginning of the path and then
22789 * append the remaining non-existing path.
22790 *
22791 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022792 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022793 * bufp - Pointer to an allocated buffer for the filename.
22794 * fnamelen - Length of the filename pointed to by fname
22795 *
22796 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 */
22798 static int
22799shortpath_for_invalid_fname(fname, bufp, fnamelen)
22800 char_u **fname;
22801 char_u **bufp;
22802 int *fnamelen;
22803{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022804 char_u *short_fname, *save_fname, *pbuf_unused;
22805 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022807 int old_len, len;
22808 int new_len, sfx_len;
22809 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810
22811 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022812 old_len = *fnamelen;
22813 save_fname = vim_strnsave(*fname, old_len);
22814 pbuf_unused = NULL;
22815 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022817 endp = save_fname + old_len - 1; /* Find the end of the copy */
22818 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022820 /*
22821 * Try shortening the supplied path till it succeeds by removing one
22822 * directory at a time from the tail of the path.
22823 */
22824 len = 0;
22825 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022827 /* go back one path-separator */
22828 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22829 --endp;
22830 if (endp <= save_fname)
22831 break; /* processed the complete path */
22832
22833 /*
22834 * Replace the path separator with a NUL and try to shorten the
22835 * resulting path.
22836 */
22837 ch = *endp;
22838 *endp = 0;
22839 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022840 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022841 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22842 {
22843 retval = FAIL;
22844 goto theend;
22845 }
22846 *endp = ch; /* preserve the string */
22847
22848 if (len > 0)
22849 break; /* successfully shortened the path */
22850
22851 /* failed to shorten the path. Skip the path separator */
22852 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 }
22854
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022855 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022857 /*
22858 * Succeeded in shortening the path. Now concatenate the shortened
22859 * path with the remaining path at the tail.
22860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022862 /* Compute the length of the new path. */
22863 sfx_len = (int)(save_endp - endp) + 1;
22864 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022866 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022868 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022870 /* There is not enough space in the currently allocated string,
22871 * copy it to a buffer big enough. */
22872 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022874 {
22875 retval = FAIL;
22876 goto theend;
22877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 }
22879 else
22880 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022881 /* Transfer short_fname to the main buffer (it's big enough),
22882 * unless get_short_pathname() did its work in-place. */
22883 *fname = *bufp = save_fname;
22884 if (short_fname != save_fname)
22885 vim_strncpy(save_fname, short_fname, len);
22886 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022888
22889 /* concat the not-shortened part of the path */
22890 vim_strncpy(*fname + len, endp, sfx_len);
22891 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022893
22894theend:
22895 vim_free(pbuf_unused);
22896 vim_free(save_fname);
22897
22898 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899}
22900
22901/*
22902 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022903 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 */
22905 static int
22906shortpath_for_partial(fnamep, bufp, fnamelen)
22907 char_u **fnamep;
22908 char_u **bufp;
22909 int *fnamelen;
22910{
22911 int sepcount, len, tflen;
22912 char_u *p;
22913 char_u *pbuf, *tfname;
22914 int hasTilde;
22915
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022916 /* Count up the path separators from the RHS.. so we know which part
22917 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022919 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 if (vim_ispathsep(*p))
22921 ++sepcount;
22922
22923 /* Need full path first (use expand_env() to remove a "~/") */
22924 hasTilde = (**fnamep == '~');
22925 if (hasTilde)
22926 pbuf = tfname = expand_env_save(*fnamep);
22927 else
22928 pbuf = tfname = FullName_save(*fnamep, FALSE);
22929
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022930 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022932 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22933 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934
22935 if (len == 0)
22936 {
22937 /* Don't have a valid filename, so shorten the rest of the
22938 * path if we can. This CAN give us invalid 8.3 filenames, but
22939 * there's not a lot of point in guessing what it might be.
22940 */
22941 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022942 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 }
22945
22946 /* Count the paths backward to find the beginning of the desired string. */
22947 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022948 {
22949#ifdef FEAT_MBYTE
22950 if (has_mbyte)
22951 p -= mb_head_off(tfname, p);
22952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 if (vim_ispathsep(*p))
22954 {
22955 if (sepcount == 0 || (hasTilde && sepcount == 1))
22956 break;
22957 else
22958 sepcount --;
22959 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 if (hasTilde)
22962 {
22963 --p;
22964 if (p >= tfname)
22965 *p = '~';
22966 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 }
22969 else
22970 ++p;
22971
22972 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22973 vim_free(*bufp);
22974 *fnamelen = (int)STRLEN(p);
22975 *bufp = pbuf;
22976 *fnamep = p;
22977
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022978 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979}
22980#endif /* WIN3264 */
22981
22982/*
22983 * Adjust a filename, according to a string of modifiers.
22984 * *fnamep must be NUL terminated when called. When returning, the length is
22985 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022986 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 * When there is an error, *fnamep is set to NULL.
22988 */
22989 int
22990modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22991 char_u *src; /* string with modifiers */
22992 int *usedlen; /* characters after src that are used */
22993 char_u **fnamep; /* file name so far */
22994 char_u **bufp; /* buffer for allocated file name or NULL */
22995 int *fnamelen; /* length of fnamep */
22996{
22997 int valid = 0;
22998 char_u *tail;
22999 char_u *s, *p, *pbuf;
23000 char_u dirname[MAXPATHL];
23001 int c;
23002 int has_fullname = 0;
23003#ifdef WIN3264
23004 int has_shortname = 0;
23005#endif
23006
23007repeat:
23008 /* ":p" - full path/file_name */
23009 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23010 {
23011 has_fullname = 1;
23012
23013 valid |= VALID_PATH;
23014 *usedlen += 2;
23015
23016 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23017 if ((*fnamep)[0] == '~'
23018#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23019 && ((*fnamep)[1] == '/'
23020# ifdef BACKSLASH_IN_FILENAME
23021 || (*fnamep)[1] == '\\'
23022# endif
23023 || (*fnamep)[1] == NUL)
23024
23025#endif
23026 )
23027 {
23028 *fnamep = expand_env_save(*fnamep);
23029 vim_free(*bufp); /* free any allocated file name */
23030 *bufp = *fnamep;
23031 if (*fnamep == NULL)
23032 return -1;
23033 }
23034
23035 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023036 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 {
23038 if (vim_ispathsep(*p)
23039 && p[1] == '.'
23040 && (p[2] == NUL
23041 || vim_ispathsep(p[2])
23042 || (p[2] == '.'
23043 && (p[3] == NUL || vim_ispathsep(p[3])))))
23044 break;
23045 }
23046
23047 /* FullName_save() is slow, don't use it when not needed. */
23048 if (*p != NUL || !vim_isAbsName(*fnamep))
23049 {
23050 *fnamep = FullName_save(*fnamep, *p != NUL);
23051 vim_free(*bufp); /* free any allocated file name */
23052 *bufp = *fnamep;
23053 if (*fnamep == NULL)
23054 return -1;
23055 }
23056
23057 /* Append a path separator to a directory. */
23058 if (mch_isdir(*fnamep))
23059 {
23060 /* Make room for one or two extra characters. */
23061 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23062 vim_free(*bufp); /* free any allocated file name */
23063 *bufp = *fnamep;
23064 if (*fnamep == NULL)
23065 return -1;
23066 add_pathsep(*fnamep);
23067 }
23068 }
23069
23070 /* ":." - path relative to the current directory */
23071 /* ":~" - path relative to the home directory */
23072 /* ":8" - shortname path - postponed till after */
23073 while (src[*usedlen] == ':'
23074 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23075 {
23076 *usedlen += 2;
23077 if (c == '8')
23078 {
23079#ifdef WIN3264
23080 has_shortname = 1; /* Postpone this. */
23081#endif
23082 continue;
23083 }
23084 pbuf = NULL;
23085 /* Need full path first (use expand_env() to remove a "~/") */
23086 if (!has_fullname)
23087 {
23088 if (c == '.' && **fnamep == '~')
23089 p = pbuf = expand_env_save(*fnamep);
23090 else
23091 p = pbuf = FullName_save(*fnamep, FALSE);
23092 }
23093 else
23094 p = *fnamep;
23095
23096 has_fullname = 0;
23097
23098 if (p != NULL)
23099 {
23100 if (c == '.')
23101 {
23102 mch_dirname(dirname, MAXPATHL);
23103 s = shorten_fname(p, dirname);
23104 if (s != NULL)
23105 {
23106 *fnamep = s;
23107 if (pbuf != NULL)
23108 {
23109 vim_free(*bufp); /* free any allocated file name */
23110 *bufp = pbuf;
23111 pbuf = NULL;
23112 }
23113 }
23114 }
23115 else
23116 {
23117 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23118 /* Only replace it when it starts with '~' */
23119 if (*dirname == '~')
23120 {
23121 s = vim_strsave(dirname);
23122 if (s != NULL)
23123 {
23124 *fnamep = s;
23125 vim_free(*bufp);
23126 *bufp = s;
23127 }
23128 }
23129 }
23130 vim_free(pbuf);
23131 }
23132 }
23133
23134 tail = gettail(*fnamep);
23135 *fnamelen = (int)STRLEN(*fnamep);
23136
23137 /* ":h" - head, remove "/file_name", can be repeated */
23138 /* Don't remove the first "/" or "c:\" */
23139 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23140 {
23141 valid |= VALID_HEAD;
23142 *usedlen += 2;
23143 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023144 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023145 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 *fnamelen = (int)(tail - *fnamep);
23147#ifdef VMS
23148 if (*fnamelen > 0)
23149 *fnamelen += 1; /* the path separator is part of the path */
23150#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023151 if (*fnamelen == 0)
23152 {
23153 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23154 p = vim_strsave((char_u *)".");
23155 if (p == NULL)
23156 return -1;
23157 vim_free(*bufp);
23158 *bufp = *fnamep = tail = p;
23159 *fnamelen = 1;
23160 }
23161 else
23162 {
23163 while (tail > s && !after_pathsep(s, tail))
23164 mb_ptr_back(*fnamep, tail);
23165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 }
23167
23168 /* ":8" - shortname */
23169 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23170 {
23171 *usedlen += 2;
23172#ifdef WIN3264
23173 has_shortname = 1;
23174#endif
23175 }
23176
23177#ifdef WIN3264
23178 /* Check shortname after we have done 'heads' and before we do 'tails'
23179 */
23180 if (has_shortname)
23181 {
23182 pbuf = NULL;
23183 /* Copy the string if it is shortened by :h */
23184 if (*fnamelen < (int)STRLEN(*fnamep))
23185 {
23186 p = vim_strnsave(*fnamep, *fnamelen);
23187 if (p == 0)
23188 return -1;
23189 vim_free(*bufp);
23190 *bufp = *fnamep = p;
23191 }
23192
23193 /* Split into two implementations - makes it easier. First is where
23194 * there isn't a full name already, second is where there is.
23195 */
23196 if (!has_fullname && !vim_isAbsName(*fnamep))
23197 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023198 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 return -1;
23200 }
23201 else
23202 {
23203 int l;
23204
23205 /* Simple case, already have the full-name
23206 * Nearly always shorter, so try first time. */
23207 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023208 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 return -1;
23210
23211 if (l == 0)
23212 {
23213 /* Couldn't find the filename.. search the paths.
23214 */
23215 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023216 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 return -1;
23218 }
23219 *fnamelen = l;
23220 }
23221 }
23222#endif /* WIN3264 */
23223
23224 /* ":t" - tail, just the basename */
23225 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23226 {
23227 *usedlen += 2;
23228 *fnamelen -= (int)(tail - *fnamep);
23229 *fnamep = tail;
23230 }
23231
23232 /* ":e" - extension, can be repeated */
23233 /* ":r" - root, without extension, can be repeated */
23234 while (src[*usedlen] == ':'
23235 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23236 {
23237 /* find a '.' in the tail:
23238 * - for second :e: before the current fname
23239 * - otherwise: The last '.'
23240 */
23241 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23242 s = *fnamep - 2;
23243 else
23244 s = *fnamep + *fnamelen - 1;
23245 for ( ; s > tail; --s)
23246 if (s[0] == '.')
23247 break;
23248 if (src[*usedlen + 1] == 'e') /* :e */
23249 {
23250 if (s > tail)
23251 {
23252 *fnamelen += (int)(*fnamep - (s + 1));
23253 *fnamep = s + 1;
23254#ifdef VMS
23255 /* cut version from the extension */
23256 s = *fnamep + *fnamelen - 1;
23257 for ( ; s > *fnamep; --s)
23258 if (s[0] == ';')
23259 break;
23260 if (s > *fnamep)
23261 *fnamelen = s - *fnamep;
23262#endif
23263 }
23264 else if (*fnamep <= tail)
23265 *fnamelen = 0;
23266 }
23267 else /* :r */
23268 {
23269 if (s > tail) /* remove one extension */
23270 *fnamelen = (int)(s - *fnamep);
23271 }
23272 *usedlen += 2;
23273 }
23274
23275 /* ":s?pat?foo?" - substitute */
23276 /* ":gs?pat?foo?" - global substitute */
23277 if (src[*usedlen] == ':'
23278 && (src[*usedlen + 1] == 's'
23279 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23280 {
23281 char_u *str;
23282 char_u *pat;
23283 char_u *sub;
23284 int sep;
23285 char_u *flags;
23286 int didit = FALSE;
23287
23288 flags = (char_u *)"";
23289 s = src + *usedlen + 2;
23290 if (src[*usedlen + 1] == 'g')
23291 {
23292 flags = (char_u *)"g";
23293 ++s;
23294 }
23295
23296 sep = *s++;
23297 if (sep)
23298 {
23299 /* find end of pattern */
23300 p = vim_strchr(s, sep);
23301 if (p != NULL)
23302 {
23303 pat = vim_strnsave(s, (int)(p - s));
23304 if (pat != NULL)
23305 {
23306 s = p + 1;
23307 /* find end of substitution */
23308 p = vim_strchr(s, sep);
23309 if (p != NULL)
23310 {
23311 sub = vim_strnsave(s, (int)(p - s));
23312 str = vim_strnsave(*fnamep, *fnamelen);
23313 if (sub != NULL && str != NULL)
23314 {
23315 *usedlen = (int)(p + 1 - src);
23316 s = do_string_sub(str, pat, sub, flags);
23317 if (s != NULL)
23318 {
23319 *fnamep = s;
23320 *fnamelen = (int)STRLEN(s);
23321 vim_free(*bufp);
23322 *bufp = s;
23323 didit = TRUE;
23324 }
23325 }
23326 vim_free(sub);
23327 vim_free(str);
23328 }
23329 vim_free(pat);
23330 }
23331 }
23332 /* after using ":s", repeat all the modifiers */
23333 if (didit)
23334 goto repeat;
23335 }
23336 }
23337
23338 return valid;
23339}
23340
23341/*
23342 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23343 * "flags" can be "g" to do a global substitute.
23344 * Returns an allocated string, NULL for error.
23345 */
23346 char_u *
23347do_string_sub(str, pat, sub, flags)
23348 char_u *str;
23349 char_u *pat;
23350 char_u *sub;
23351 char_u *flags;
23352{
23353 int sublen;
23354 regmatch_T regmatch;
23355 int i;
23356 int do_all;
23357 char_u *tail;
23358 garray_T ga;
23359 char_u *ret;
23360 char_u *save_cpo;
23361
23362 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23363 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023364 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365
23366 ga_init2(&ga, 1, 200);
23367
23368 do_all = (flags[0] == 'g');
23369
23370 regmatch.rm_ic = p_ic;
23371 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23372 if (regmatch.regprog != NULL)
23373 {
23374 tail = str;
23375 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23376 {
23377 /*
23378 * Get some space for a temporary buffer to do the substitution
23379 * into. It will contain:
23380 * - The text up to where the match is.
23381 * - The substituted text.
23382 * - The text after the match.
23383 */
23384 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23385 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23386 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23387 {
23388 ga_clear(&ga);
23389 break;
23390 }
23391
23392 /* copy the text up to where the match is */
23393 i = (int)(regmatch.startp[0] - tail);
23394 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23395 /* add the substituted text */
23396 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23397 + ga.ga_len + i, TRUE, TRUE, FALSE);
23398 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 /* avoid getting stuck on a match with an empty string */
23400 if (tail == regmatch.endp[0])
23401 {
23402 if (*tail == NUL)
23403 break;
23404 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23405 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 }
23407 else
23408 {
23409 tail = regmatch.endp[0];
23410 if (*tail == NUL)
23411 break;
23412 }
23413 if (!do_all)
23414 break;
23415 }
23416
23417 if (ga.ga_data != NULL)
23418 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23419
23420 vim_free(regmatch.regprog);
23421 }
23422
23423 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23424 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023425 if (p_cpo == empty_option)
23426 p_cpo = save_cpo;
23427 else
23428 /* Darn, evaluating {sub} expression changed the value. */
23429 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430
23431 return ret;
23432}
23433
23434#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */